2 from os
.path
import join
, dirname
, getmtime
, exists
, expanduser
12 Call the C program for converting RGB to Ansi colors
14 library
= expanduser('~/.image.so')
15 sauce
= join(dirname(__file__
), 'image.c')
16 if not exists(library
) or getmtime(sauce
) > getmtime(library
):
17 build
= "gcc -fPIC -shared -o %s %s" % (library
, sauce
)
18 assert os
.system(build
+ " >/dev/null 2>&1") == 0
19 image_c
= ctypes
.cdll
.LoadLibrary(library
)
21 return image_c
.rgb_to_ansi
26 def pixel_print(ansicolor
):
28 Print a pixel with given Ansi color
30 sys
.stdout
.write('\033[48;5;%sm \033[0m' % (ansicolor
))
33 def image_to_display(path
, start
=None, length
=None):
37 rows
, columns
= os
.popen('stty size', 'r').read().split()
39 start
= c
['IMAGE_SHIFT']
41 length
= int(columns
) - 2 * start
46 width
= min(w
, length
)
47 height
= int(float(h
) * (float(width
) / float(w
)))
49 i
= i
.resize((width
, height
), Image
.ANTIALIAS
)
50 height
= min(height
, c
['IMAGE_MAX_HEIGHT'])
52 for y
in range(height
):
53 sys
.stdout
.write(' ' * start
)
54 for x
in range(width
):
55 p
= i
.getpixel((x
, y
))
57 pixel_print(rgb2short(r
, g
, b
))
58 sys
.stdout
.write('\n')
62 For direct using purpose
64 if __name__
== '__main__':
65 image_to_display(sys
.argv
[1])