1 from PIL
import Image
,ImageFilter
2 from functools
import partial
3 from os
.path
import join
, dirname
, getmtime
, exists
, expanduser
8 library
= expanduser('~/.image.so')
9 sauce
= join(dirname(__file__
), 'image.c')
10 if not exists(library
) or getmtime(sauce
) > getmtime(library
):
11 build
= "gcc -fPIC -shared -o %s %s" % (library
, sauce
)
12 assert os
.system(build
+ " >/dev/null 2>&1") == 0
13 image_c
= ctypes
.cdll
.LoadLibrary(library
)
15 return image_c
.rgb_to_ansi
19 def pixel_print(ansicolor
):
20 sys
.stdout
.write('\033[48;5;%sm \033[0m' % (ansicolor
))
22 def image_to_display(path
):
27 rows
, columns
= os
.popen('stty size', 'r').read().split()
28 width
= min(w
, int(columns
)-2*6)
29 height
= int(float(h
) * (float(width
) / float(w
)))
31 i
= i
.resize((width
, height
), Image
.BICUBIC
)
32 height
= min(height
,30)
34 for y
in xrange(height
):
36 for x
in xrange(width
):
39 pixel_print(rgb2short(r
,g
,b
))
42 if __name__
== '__main__':
43 image_to_display(sys
.argv
[1])