e4d450564d203a10ab92a03fe6e87d949dc81ae3
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 os
.system(build
+ " >/dev/null 2>&1")
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 block_print(lower
, higher
):
36 Print two pixels arranged above each other with Ansi color.
37 Abuses Unicode to print two pixels in the space of one terminal block.
39 sys
.stdout
.write('\033[38;5;%sm\033[48;5;%sm▄\033[0m' % (higher
, lower
))
42 def image_to_display(path
, start
=None, length
=None):
46 rows
, columns
= os
.popen('stty size', 'r').read().split()
48 start
= c
['IMAGE_SHIFT']
50 length
= int(columns
) - 2 * start
55 width
= min(w
, length
)
56 height
= int(float(h
) * (float(width
) / float(w
)))
57 i
= i
.resize((width
, height
), Image
.ANTIALIAS
)
58 height
= min(height
, c
['IMAGE_MAX_HEIGHT'])
60 for real_y
in xrange(height
// 2):
61 sys
.stdout
.write(' ' * start
)
62 for x
in xrange(width
):
64 p0
= i
.getpixel((x
, y
))
65 p1
= i
.getpixel((x
, y
+1))
68 block_print(rgb2short(r0
, g0
, b0
), rgb2short(r1
, g1
, b1
))
69 sys
.stdout
.write('\n')
73 For direct using purpose
75 if __name__
== '__main__':
76 image_to_display(sys
.argv
[1])