2 from functools
import partial
3 from os
.path
import join
, dirname
, getmtime
, exists
, expanduser
12 library
= expanduser('~/.image.so')
13 sauce
= join(dirname(__file__
), 'image.c')
14 if not exists(library
) or getmtime(sauce
) > getmtime(library
):
15 build
= "gcc -fPIC -shared -o %s %s" % (library
, sauce
)
16 assert os
.system(build
+ " >/dev/null 2>&1") == 0
17 image_c
= ctypes
.cdll
.LoadLibrary(library
)
19 return image_c
.rgb_to_ansi
24 def pixel_print(ansicolor
):
25 sys
.stdout
.write('\033[48;5;%sm \033[0m' % (ansicolor
))
28 def image_to_display(path
):
33 rows
, columns
= os
.popen('stty size', 'r').read().split()
34 width
= min(w
, int(columns
) - 2 * IMAGE_SHIFT
)
35 height
= int(float(h
) * (float(width
) / float(w
)))
37 i
= i
.resize((width
, height
), Image
.BICUBIC
)
38 height
= min(height
, IMAGE_MAX_HEIGHT
)
40 for y
in xrange(height
):
41 print ' ' * IMAGE_SHIFT
,
42 for x
in xrange(width
):
43 p
= i
.getpixel((x
, y
))
45 pixel_print(rgb2short(r
, g
, b
))
48 if __name__
== '__main__':
49 image_to_display(sys
.argv
[1])