2 from os
.path
import join
, dirname
, getmtime
, exists
, expanduser
4 from .py3patch
import *
13 Call the C program for converting RGB to Ansi colors
15 library
= expanduser('~/.image.so')
16 sauce
= join(dirname(__file__
), 'image.c')
17 if not exists(library
) or getmtime(sauce
) > getmtime(library
):
18 build
= "cc -fPIC -shared -o %s %s" % (library
, sauce
)
19 assert os
.system(build
+ " >/dev/null 2>&1") == 0
20 image_c
= ctypes
.cdll
.LoadLibrary(library
)
22 return image_c
.rgb_to_ansi
27 def pixel_print(ansicolor
):
29 Print a pixel with given Ansi color
31 sys
.stdout
.write('\033[48;5;%sm \033[0m' % (ansicolor
))
34 def image_to_display(path
, start
=None, length
=None):
38 rows
, columns
= os
.popen('stty size', 'r').read().split()
40 start
= c
['IMAGE_SHIFT']
42 length
= int(columns
) - 2 * start
47 width
= min(w
, length
)
48 height
= int(float(h
) * (float(width
) / float(w
)))
50 i
= i
.resize((width
, height
), Image
.ANTIALIAS
)
51 height
= min(height
, c
['IMAGE_MAX_HEIGHT'])
53 for y
in xrange(height
):
54 sys
.stdout
.write(' ' * start
)
55 for x
in xrange(width
):
56 p
= i
.getpixel((x
, y
))
58 pixel_print(rgb2short(r
, g
, b
))
59 sys
.stdout
.write('\n')
63 For direct using purpose
65 if __name__
== '__main__':
66 image_to_display(sys
.argv
[1])