Skip to content

Instantly share code, notes, and snippets.

@sonic74
Forked from klange/image-to-ansi.py
Created March 2, 2019 00:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonic74/b270f5575c985ee0a1b94aa9d34691ec to your computer and use it in GitHub Desktop.
Save sonic74/b270f5575c985ee0a1b94aa9d34691ec to your computer and use it in GitHub Desktop.
Convert images to ANSI escape sequences
# Palette
# Mode: RGB
0 0 0 0
1 170 0 0
2 0 170 0
3 170 85 0
4 0 0 170
5 170 0 170
6 0 170 170
7 170 170 170
8 85 85 85
9 255 85 85
10 85 255 85
11 255 255 85
12 85 85 255
13 255 85 255
14 85 255 255
15 255 255 255
16 0 0 0
17 0 0 0
18 0 0 0
19 0 0 0
20 0 0 0
21 0 0 0
22 0 0 0
23 0 0 0
24 0 0 0
25 0 0 0
26 0 0 0
27 0 0 0
28 0 0 0
29 0 0 0
30 0 0 0
31 0 0 0
32 0 0 0
33 0 0 0
34 0 0 0
35 0 0 0
36 0 0 0
37 0 0 0
38 0 0 0
39 0 0 0
40 0 0 0
41 0 0 0
42 0 0 0
43 0 0 0
44 0 0 0
45 0 0 0
46 0 0 0
47 0 0 0
48 0 0 0
49 0 0 0
50 0 0 0
51 0 0 0
52 0 0 0
53 0 0 0
54 0 0 0
55 0 0 0
56 0 0 0
57 0 0 0
58 0 0 0
59 0 0 0
60 0 0 0
61 0 0 0
62 0 0 0
63 0 0 0
64 0 0 0
65 0 0 0
66 0 0 0
67 0 0 0
68 0 0 0
69 0 0 0
70 0 0 0
71 0 0 0
72 0 0 0
73 0 0 0
74 0 0 0
75 0 0 0
76 0 0 0
77 0 0 0
78 0 0 0
79 0 0 0
80 0 0 0
81 0 0 0
82 0 0 0
83 0 0 0
84 0 0 0
85 0 0 0
86 0 0 0
87 0 0 0
88 0 0 0
89 0 0 0
90 0 0 0
91 0 0 0
92 0 0 0
93 0 0 0
94 0 0 0
95 0 0 0
96 0 0 0
97 0 0 0
98 0 0 0
99 0 0 0
100 0 0 0
101 0 0 0
102 0 0 0
103 0 0 0
104 0 0 0
105 0 0 0
106 0 0 0
107 0 0 0
108 0 0 0
109 0 0 0
110 0 0 0
111 0 0 0
112 0 0 0
113 0 0 0
114 0 0 0
115 0 0 0
116 0 0 0
117 0 0 0
118 0 0 0
119 0 0 0
120 0 0 0
121 0 0 0
122 0 0 0
123 0 0 0
124 0 0 0
125 0 0 0
126 0 0 0
127 0 0 0
128 0 0 0
129 0 0 0
130 0 0 0
131 0 0 0
132 0 0 0
133 0 0 0
134 0 0 0
135 0 0 0
136 0 0 0
137 0 0 0
138 0 0 0
139 0 0 0
140 0 0 0
141 0 0 0
142 0 0 0
143 0 0 0
144 0 0 0
145 0 0 0
146 0 0 0
147 0 0 0
148 0 0 0
149 0 0 0
150 0 0 0
151 0 0 0
152 0 0 0
153 0 0 0
154 0 0 0
155 0 0 0
156 0 0 0
157 0 0 0
158 0 0 0
159 0 0 0
160 0 0 0
161 0 0 0
162 0 0 0
163 0 0 0
164 0 0 0
165 0 0 0
166 0 0 0
167 0 0 0
168 0 0 0
169 0 0 0
170 0 0 0
171 0 0 0
172 0 0 0
173 0 0 0
174 0 0 0
175 0 0 0
176 0 0 0
177 0 0 0
178 0 0 0
179 0 0 0
180 0 0 0
181 0 0 0
182 0 0 0
183 0 0 0
184 0 0 0
185 0 0 0
186 0 0 0
187 0 0 0
188 0 0 0
189 0 0 0
190 0 0 0
191 0 0 0
192 0 0 0
193 0 0 0
194 0 0 0
195 0 0 0
196 0 0 0
197 0 0 0
198 0 0 0
199 0 0 0
200 0 0 0
201 0 0 0
202 0 0 0
203 0 0 0
204 0 0 0
205 0 0 0
206 0 0 0
207 0 0 0
208 0 0 0
209 0 0 0
210 0 0 0
211 0 0 0
212 0 0 0
213 0 0 0
214 0 0 0
215 0 0 0
216 0 0 0
217 0 0 0
218 0 0 0
219 0 0 0
220 0 0 0
221 0 0 0
222 0 0 0
223 0 0 0
224 0 0 0
225 0 0 0
226 0 0 0
227 0 0 0
228 0 0 0
229 0 0 0
230 0 0 0
231 0 0 0
232 0 0 0
233 0 0 0
234 0 0 0
235 0 0 0
236 0 0 0
237 0 0 0
238 0 0 0
239 0 0 0
240 0 0 0
241 0 0 0
242 0 0 0
243 0 0 0
244 0 0 0
245 0 0 0
246 0 0 0
247 0 0 0
248 0 0 0
249 0 0 0
250 0 0 0
251 0 0 0
252 0 0 0
253 0 0 0
254 0 0 0
255 0 0 0
# Palette
# Mode: RGB
0 1 1 1
1 222 56 43
2 57 181 74
3 255 199 6
4 0 111 184
5 118 38 113
6 44 181 233
7 204 204 204
8 128 128 128
9 255 0 0
10 0 255 0
11 255 255 0
12 0 0 255
13 255 0 255
14 0 255 255
15 255 255 255
16 0 0 0
17 0 0 0
18 0 0 0
19 0 0 0
20 0 0 0
21 0 0 0
22 0 0 0
23 0 0 0
24 0 0 0
25 0 0 0
26 0 0 0
27 0 0 0
28 0 0 0
29 0 0 0
30 0 0 0
31 0 0 0
32 0 0 0
33 0 0 0
34 0 0 0
35 0 0 0
36 0 0 0
37 0 0 0
38 0 0 0
39 0 0 0
40 0 0 0
41 0 0 0
42 0 0 0
43 0 0 0
44 0 0 0
45 0 0 0
46 0 0 0
47 0 0 0
48 0 0 0
49 0 0 0
50 0 0 0
51 0 0 0
52 0 0 0
53 0 0 0
54 0 0 0
55 0 0 0
56 0 0 0
57 0 0 0
58 0 0 0
59 0 0 0
60 0 0 0
61 0 0 0
62 0 0 0
63 0 0 0
64 0 0 0
65 0 0 0
66 0 0 0
67 0 0 0
68 0 0 0
69 0 0 0
70 0 0 0
71 0 0 0
72 0 0 0
73 0 0 0
74 0 0 0
75 0 0 0
76 0 0 0
77 0 0 0
78 0 0 0
79 0 0 0
80 0 0 0
81 0 0 0
82 0 0 0
83 0 0 0
84 0 0 0
85 0 0 0
86 0 0 0
87 0 0 0
88 0 0 0
89 0 0 0
90 0 0 0
91 0 0 0
92 0 0 0
93 0 0 0
94 0 0 0
95 0 0 0
96 0 0 0
97 0 0 0
98 0 0 0
99 0 0 0
100 0 0 0
101 0 0 0
102 0 0 0
103 0 0 0
104 0 0 0
105 0 0 0
106 0 0 0
107 0 0 0
108 0 0 0
109 0 0 0
110 0 0 0
111 0 0 0
112 0 0 0
113 0 0 0
114 0 0 0
115 0 0 0
116 0 0 0
117 0 0 0
118 0 0 0
119 0 0 0
120 0 0 0
121 0 0 0
122 0 0 0
123 0 0 0
124 0 0 0
125 0 0 0
126 0 0 0
127 0 0 0
128 0 0 0
129 0 0 0
130 0 0 0
131 0 0 0
132 0 0 0
133 0 0 0
134 0 0 0
135 0 0 0
136 0 0 0
137 0 0 0
138 0 0 0
139 0 0 0
140 0 0 0
141 0 0 0
142 0 0 0
143 0 0 0
144 0 0 0
145 0 0 0
146 0 0 0
147 0 0 0
148 0 0 0
149 0 0 0
150 0 0 0
151 0 0 0
152 0 0 0
153 0 0 0
154 0 0 0
155 0 0 0
156 0 0 0
157 0 0 0
158 0 0 0
159 0 0 0
160 0 0 0
161 0 0 0
162 0 0 0
163 0 0 0
164 0 0 0
165 0 0 0
166 0 0 0
167 0 0 0
168 0 0 0
169 0 0 0
170 0 0 0
171 0 0 0
172 0 0 0
173 0 0 0
174 0 0 0
175 0 0 0
176 0 0 0
177 0 0 0
178 0 0 0
179 0 0 0
180 0 0 0
181 0 0 0
182 0 0 0
183 0 0 0
184 0 0 0
185 0 0 0
186 0 0 0
187 0 0 0
188 0 0 0
189 0 0 0
190 0 0 0
191 0 0 0
192 0 0 0
193 0 0 0
194 0 0 0
195 0 0 0
196 0 0 0
197 0 0 0
198 0 0 0
199 0 0 0
200 0 0 0
201 0 0 0
202 0 0 0
203 0 0 0
204 0 0 0
205 0 0 0
206 0 0 0
207 0 0 0
208 0 0 0
209 0 0 0
210 0 0 0
211 0 0 0
212 0 0 0
213 0 0 0
214 0 0 0
215 0 0 0
216 0 0 0
217 0 0 0
218 0 0 0
219 0 0 0
220 0 0 0
221 0 0 0
222 0 0 0
223 0 0 0
224 0 0 0
225 0 0 0
226 0 0 0
227 0 0 0
228 0 0 0
229 0 0 0
230 0 0 0
231 0 0 0
232 0 0 0
233 0 0 0
234 0 0 0
235 0 0 0
236 0 0 0
237 0 0 0
238 0 0 0
239 0 0 0
240 0 0 0
241 0 0 0
242 0 0 0
243 0 0 0
244 0 0 0
245 0 0 0
246 0 0 0
247 0 0 0
248 0 0 0
249 0 0 0
250 0 0 0
251 0 0 0
252 0 0 0
253 0 0 0
254 0 0 0
255 0 0 0
#! /usr/bin/env python3
""" Convert an image to an (ANSI) text string
Modification by sven@killig.de:
* use DOS block character for double resolution
* size
* 1-4/8/24 bitdepth option
* let Image.convert() do the palette matching
* shades
Original readme from colortrans.py follows:
Nice long listing of all 256 colors and their codes. Useful for
developing console color themes, or even script output schemes.
Resources:
* http://en.wikipedia.org/wiki/8-bit_color
* http://en.wikipedia.org/wiki/ANSI_escape_code
* /usr/share/X11/rgb.txt
I'm not sure where this script was inspired from. I think I must have
written it from scratch, though it's been several years now.
"""
__author__ = 'Micah Elliott http://MicahElliott.com; Kevin Lange <k@dakko.us>; Sven Killig <sven@killig.de>'
__version__ = '0.3'
__copyright__ = 'Copyright (C) 2011 Micah Elliott. All rights reserved.'
__license__ = 'WTFPL http://sam.zoy.org/wtfpl/'
#---------------------------------------------------------------------
import sys, re
# [python -m] pip install Image
from PIL import Image, ImagePalette
import shutil
import os
import argparse
from datetime import datetime
CLUT = [ # color look-up table
# 8-bit, RGB hex
# Primary 3-bit (8 colors). Unique representation!
('00', '000000'),
('01', '800000'),
('02', '008000'),
('03', '808000'),
('04', '000080'),
('05', '800080'),
('06', '008080'),
('07', 'c0c0c0'),
# Equivalent "bright" versions of original 8 colors.
('08', '808080'),
('09', 'ff0000'),
('10', '00ff00'),
('11', 'ffff00'),
('12', '0000ff'),
('13', 'ff00ff'),
('14', '00ffff'),
('15', 'ffffff'),
# Strictly ascending.
('16', '000000'),
('17', '00005f'),
('18', '000087'),
('19', '0000af'),
('20', '0000d7'),
('21', '0000ff'),
('22', '005f00'),
('23', '005f5f'),
('24', '005f87'),
('25', '005faf'),
('26', '005fd7'),
('27', '005fff'),
('28', '008700'),
('29', '00875f'),
('30', '008787'),
('31', '0087af'),
('32', '0087d7'),
('33', '0087ff'),
('34', '00af00'),
('35', '00af5f'),
('36', '00af87'),
('37', '00afaf'),
('38', '00afd7'),
('39', '00afff'),
('40', '00d700'),
('41', '00d75f'),
('42', '00d787'),
('43', '00d7af'),
('44', '00d7d7'),
('45', '00d7ff'),
('46', '00ff00'),
('47', '00ff5f'),
('48', '00ff87'),
('49', '00ffaf'),
('50', '00ffd7'),
('51', '00ffff'),
('52', '5f0000'),
('53', '5f005f'),
('54', '5f0087'),
('55', '5f00af'),
('56', '5f00d7'),
('57', '5f00ff'),
('58', '5f5f00'),
('59', '5f5f5f'),
('60', '5f5f87'),
('61', '5f5faf'),
('62', '5f5fd7'),
('63', '5f5fff'),
('64', '5f8700'),
('65', '5f875f'),
('66', '5f8787'),
('67', '5f87af'),
('68', '5f87d7'),
('69', '5f87ff'),
('70', '5faf00'),
('71', '5faf5f'),
('72', '5faf87'),
('73', '5fafaf'),
('74', '5fafd7'),
('75', '5fafff'),
('76', '5fd700'),
('77', '5fd75f'),
('78', '5fd787'),
('79', '5fd7af'),
('80', '5fd7d7'),
('81', '5fd7ff'),
('82', '5fff00'),
('83', '5fff5f'),
('84', '5fff87'),
('85', '5fffaf'),
('86', '5fffd7'),
('87', '5fffff'),
('88', '870000'),
('89', '87005f'),
('90', '870087'),
('91', '8700af'),
('92', '8700d7'),
('93', '8700ff'),
('94', '875f00'),
('95', '875f5f'),
('96', '875f87'),
('97', '875faf'),
('98', '875fd7'),
('99', '875fff'),
('100', '878700'),
('101', '87875f'),
('102', '878787'),
('103', '8787af'),
('104', '8787d7'),
('105', '8787ff'),
('106', '87af00'),
('107', '87af5f'),
('108', '87af87'),
('109', '87afaf'),
('110', '87afd7'),
('111', '87afff'),
('112', '87d700'),
('113', '87d75f'),
('114', '87d787'),
('115', '87d7af'),
('116', '87d7d7'),
('117', '87d7ff'),
('118', '87ff00'),
('119', '87ff5f'),
('120', '87ff87'),
('121', '87ffaf'),
('122', '87ffd7'),
('123', '87ffff'),
('124', 'af0000'),
('125', 'af005f'),
('126', 'af0087'),
('127', 'af00af'),
('128', 'af00d7'),
('129', 'af00ff'),
('130', 'af5f00'),
('131', 'af5f5f'),
('132', 'af5f87'),
('133', 'af5faf'),
('134', 'af5fd7'),
('135', 'af5fff'),
('136', 'af8700'),
('137', 'af875f'),
('138', 'af8787'),
('139', 'af87af'),
('140', 'af87d7'),
('141', 'af87ff'),
('142', 'afaf00'),
('143', 'afaf5f'),
('144', 'afaf87'),
('145', 'afafaf'),
('146', 'afafd7'),
('147', 'afafff'),
('148', 'afd700'),
('149', 'afd75f'),
('150', 'afd787'),
('151', 'afd7af'),
('152', 'afd7d7'),
('153', 'afd7ff'),
('154', 'afff00'),
('155', 'afff5f'),
('156', 'afff87'),
('157', 'afffaf'),
('158', 'afffd7'),
('159', 'afffff'),
('160', 'd70000'),
('161', 'd7005f'),
('162', 'd70087'),
('163', 'd700af'),
('164', 'd700d7'),
('165', 'd700ff'),
('166', 'd75f00'),
('167', 'd75f5f'),
('168', 'd75f87'),
('169', 'd75faf'),
('170', 'd75fd7'),
('171', 'd75fff'),
('172', 'd78700'),
('173', 'd7875f'),
('174', 'd78787'),
('175', 'd787af'),
('176', 'd787d7'),
('177', 'd787ff'),
('178', 'd7af00'),
('179', 'd7af5f'),
('180', 'd7af87'),
('181', 'd7afaf'),
('182', 'd7afd7'),
('183', 'd7afff'),
('184', 'd7d700'),
('185', 'd7d75f'),
('186', 'd7d787'),
('187', 'd7d7af'),
('188', 'd7d7d7'),
('189', 'd7d7ff'),
('190', 'd7ff00'),
('191', 'd7ff5f'),
('192', 'd7ff87'),
('193', 'd7ffaf'),
('194', 'd7ffd7'),
('195', 'd7ffff'),
('196', 'ff0000'),
('197', 'ff005f'),
('198', 'ff0087'),
('199', 'ff00af'),
('200', 'ff00d7'),
('201', 'ff00ff'),
('202', 'ff5f00'),
('203', 'ff5f5f'),
('204', 'ff5f87'),
('205', 'ff5faf'),
('206', 'ff5fd7'),
('207', 'ff5fff'),
('208', 'ff8700'),
('209', 'ff875f'),
('210', 'ff8787'),
('211', 'ff87af'),
('212', 'ff87d7'),
('213', 'ff87ff'),
('214', 'ffaf00'),
('215', 'ffaf5f'),
('216', 'ffaf87'),
('217', 'ffafaf'),
('218', 'ffafd7'),
('219', 'ffafff'),
('220', 'ffd700'),
('221', 'ffd75f'),
('222', 'ffd787'),
('223', 'ffd7af'),
('224', 'ffd7d7'),
('225', 'ffd7ff'),
('226', 'ffff00'),
('227', 'ffff5f'),
('228', 'ffff87'),
('229', 'ffffaf'),
('230', 'ffffd7'),
('231', 'ffffff'),
# Gray-scale range.
('232', '080808'),
('233', '121212'),
('234', '1c1c1c'),
('235', '262626'),
('236', '303030'),
('237', '3a3a3a'),
('238', '444444'),
('239', '4e4e4e'),
('240', '585858'),
('241', '626262'),
('242', '6c6c6c'),
('243', '767676'),
('244', '808080'),
('245', '8a8a8a'),
('246', '949494'),
('247', '9e9e9e'),
('248', 'a8a8a8'),
('249', 'b2b2b2'),
('250', 'bcbcbc'),
('251', 'c6c6c6'),
('252', 'd0d0d0'),
('253', 'dadada'),
('254', 'e4e4e4'),
('255', 'eeeeee'),
]
def _create_pal(bitdepth):
pal = []
for short, rgb in CLUT:
pal += [ int(h, 16) for h in re.split(r'(..)(..)(..)', rgb)[1:4] ]
pal_image = Image.new("P", (1,1))
pal_image.putpalette(pal[:(2**bitdepth)*3] + [0,0,0]*(256-2**bitdepth))
return pal_image
def _create_pal_sh(fillfactor):
"""
bg:
0, 1/2
fg:
0, 1/2, 1
shades:
bg fg sum
3/4* 0= 0 1/4* 0= 0 0 -
3/4* 0= 0 1/4*1/2=1/8 1/8
3/4* 0= 0 1/4* 1=1/4 1/4 *
3/4*1/2=3/8 1/4* 0= 0 3/8 *
3/4*1/2=3/8 1/4*1/2=1/8 1/2 -
3/4*1/2=3/8 1/4* 1=1/4 5/8 *
1/2* 0= 0 1/2* 0= 0 0 -
1/2* 0= 0 1/2*1/2=1/4 1/4 -
1/2* 0= 0 1/2* 1=1/2 1/2 -
1/2*1/2=1/4 1/2* 0= 0 1/4 -
1/2*1/2=1/4 1/2*1/2=1/4 1/2 -
1/2*1/2=1/4 1/2* 1=1/2 3/4 *
0 1* 0= 0 0 *
0 1*1/2=1/2 1/2 *
0 1* 1= 1 1 *
0, 1/8, 2/8, 3/8, 4/8, 5/8, 6/8, 1
"""
pal = []
for i in range(2):
for j in range(8):
shortj, rgbj = CLUT[j]
bg = [ int(h, 16) for h in re.split(r'(..)(..)(..)', rgbj)[1:4] ]
for k in range(8 if i==1 else 0, 16):
shortk, rgbk = CLUT[k]
fg = [ int(h, 16) for h in re.split(r'(..)(..)(..)', rgbk)[1:4] ]
pal += [ int(bg[h]*(1-fillfactor[i])+fg[h]*(fillfactor[i])) for h in range(3) ]
for j in range(16):
shortj, rgbj = CLUT[j]
pal += [ int(h, 16) for h in re.split(r'(..)(..)(..)', rgbj)[1:4] ]
pal_image = Image.new("P", (1,1))
pal_image.putpalette(pal + [0,0,0]*(256-(8*8+8*16+16)))
return pal_image
def quantizetopalette(silf, palette, dither=False):
"""Convert an RGB or L mode image to use a given P image's palette."""
# https://stackoverflow.com/a/29438149/9717882
silf.load()
# use palette from reference image
palette.load()
if palette.mode != "P":
raise ValueError("bad mode for palette image")
if silf.mode != "RGB" and silf.mode != "L":
raise ValueError(
"only RGB or L mode images can be quantized to a palette"
)
im = silf.im.convert("P", 1 if dither else 0, palette.im)
# the 0 above means turn OFF dithering
# Later versions of Pillow (4.x) rename _makeself to _new
try:
return silf._new(im)
except AttributeError:
return silf._makeself(im)
#---------------------------------------------------------------------
if __name__ == '__main__':
#print (os.environ)
if os.name=='nt':
# https://bugs.python.org/msg291732
# does seem to disable VT mode after exit
os.system('')
terminal_size = shutil.get_terminal_size((80, 24))
columns = terminal_size.columns
lines = terminal_size.lines
if os.getenv('COLORTERM')=='truecolor' or os.name=='nt':
bitdepth = 24
elif 'xterm-256color' in os.getenv('TERM',''):
bitdepth = 8
elif os.getenv('TERM')=='linux':
bitdepth = 3
elif os.getenv('TERM')=='dumb':
bitdepth = 1
else:
bitdepth = 4
threshold = int(256/2)
if os.getenv('TERM') in {'pc3', 'ansi-bbs'}:
# Synchronet BBS
encoding='cp437'
else:
encoding=sys.stdout.encoding
#fillfactor = [.125, .25]
fillfactor = [.25, .5]
thisyear = datetime.today().year
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description="small history lesson: MDA (-b 1 [-s -i]) and CGA (-b 3/-b 4 -s) were introduced by IBM %i years ago." % ((thisyear-1981)))
parser.add_argument("image")
parser.add_argument("-c", "--columns", type=int, default=columns, help=" ")
parser.add_argument("-l", "--lines", type=int, default=lines, help=" ")
parser.add_argument("-b", "--bitdepth", type=int, choices=[1,2,3,4,8,24], default=bitdepth, help=" ")
parser.add_argument("-t", "--threshold", type=int, default=threshold, help="for bitdepth 1")
parser.add_argument("-e", "--encoding", default=encoding, help=" ")
parser.add_argument("-s", "--shades", action="store_true", help="for bitdepths 1,4")
parser.add_argument("-i", "--intensity", action="store_true", help="for bitdepth 1 and shades")
parser.add_argument("-f", "--fillfactor", type=float, nargs=2, default=fillfactor, help="for bitdepth 4 and shades")
parser.add_argument("-p", "--palette", help="GIMP or teragon-style palette/gradient")
parser.add_argument("-cb", "--colourbar", action="store_true", help="for creation of screenshot for palette")
args = parser.parse_args()
if args.colourbar:
for i in range(16):
sys.stdout.buffer.write(bytes("\033[38;5;%im█" % i, args.encoding))
print("")
sys.exit()
shades = [" ","░","▒","█"]
shades_intensity = [" ","\033[0m░\033[1m","░","▒","█"]
size = args.columns, args.lines*2
im = Image.open(args.image)
im.thumbnail(size, resample=Image.LANCZOS)
if args.shades:
im=im.resize(size=(im.size[0], int(im.size[1]/2)), resample=Image.LANCZOS)
if args.bitdepth==1:
im=im.convert("L")
sys.stdout.buffer.write(b"\033[1m")
if args.intensity:
sh=shades_intensity
else:
sh=shades
elif args.bitdepth<24:
if args.palette:
pal, rawmode = ImagePalette.load(args.palette)
#print(CLUT)
for i in range(16):
CLUT[i]=("%02i" % i, "%02.x%02.x%02.x" % (pal[i*3], pal[i*3+1], pal[i*3+2]))
#print(CLUT)
if args.shades:
pal = _create_pal_sh(args.fillfactor)
else:
pal = _create_pal(args.bitdepth)
im=quantizetopalette(im, pal)
#im.save("/test.png", "PNG")
#print(im.getpalette())
for y in range(0, im.size[1], 1 if args.shades else 2):
for x in range(im.size[0]):
pu = im.getpixel((x,y))
if y+1<im.size[1]:
pd = im.getpixel((x,y+1))
if args.bitdepth==1:
if args.shades:
sys.stdout.buffer.write(bytes(sh[int(pu/256*len(sh))], args.encoding))
else:
if y+1<im.size[1]:
if pd>args.threshold:
if pu>args.threshold:
sys.stdout.buffer.write(bytes("█", args.encoding))
else:
sys.stdout.buffer.write(bytes("▄", args.encoding))
else:
sys.stdout.buffer.write(bytes(" ", args.encoding))
else:
if pu>args.threshold:
sys.stdout.buffer.write(bytes("▀", args.encoding))
else:
sys.stdout.buffer.write(bytes(" ", args.encoding))
elif args.bitdepth<24:
if args.bitdepth==4 and args.shades:
if pu>=8*8+8*16:
shade="█"
fg=pu-(8*8+8*16)
bg=0
else:
shade=shades[((pu & 0b10000000)>>7)+1]
if shade=="░":
fg=((pu & 0b00001111)>>0)
bg=((pu & 0b01110000)>>4)
elif shade=="▒":
fg=((pu & 0b00000111)>>0)+8
bg=((pu & 0b00111000)>>3)
sys.stdout.buffer.write(b"\033[48;5;%im" % bg)
sys.stdout.buffer.write(b"\033[38;5;%im" % fg)
sys.stdout.buffer.write(bytes(shade, args.encoding))
else:
if y+1<im.size[1]:
sys.stdout.buffer.write(b"\033[48;5;%im" % pd)
sys.stdout.buffer.write(b"\033[38;5;%im" % pu)
sys.stdout.buffer.write(bytes("▀", args.encoding))
else:
if y+1<im.size[1]:
sys.stdout.buffer.write(b"\033[48;2;%i;%i;%im" % (pd[0],pd[1],pd[2]))
sys.stdout.buffer.write(b"\033[38;2;%i;%i;%im" % (pu[0],pu[1],pu[2]))
sys.stdout.buffer.write(bytes("▀", args.encoding))
sys.stdout.buffer.write(b"\033[0m")
sys.stdout.buffer.write(b"\n")
# Palette
# Mode: RGB
0 0 0 0
1 153 0 0
2 0 166 0
3 153 153 0
4 0 0 178
5 178 0 178
6 0 166 178
7 191 191 191
8 102 102 102
9 230 0 0
10 0 217 0
11 230 230 0
12 0 0 255
13 230 0 230
14 0 230 230
15 230 230 230
16 0 0 0
17 0 0 0
18 0 0 0
19 0 0 0
20 0 0 0
21 0 0 0
22 0 0 0
23 0 0 0
24 0 0 0
25 0 0 0
26 0 0 0
27 0 0 0
28 0 0 0
29 0 0 0
30 0 0 0
31 0 0 0
32 0 0 0
33 0 0 0
34 0 0 0
35 0 0 0
36 0 0 0
37 0 0 0
38 0 0 0
39 0 0 0
40 0 0 0
41 0 0 0
42 0 0 0
43 0 0 0
44 0 0 0
45 0 0 0
46 0 0 0
47 0 0 0
48 0 0 0
49 0 0 0
50 0 0 0
51 0 0 0
52 0 0 0
53 0 0 0
54 0 0 0
55 0 0 0
56 0 0 0
57 0 0 0
58 0 0 0
59 0 0 0
60 0 0 0
61 0 0 0
62 0 0 0
63 0 0 0
64 0 0 0
65 0 0 0
66 0 0 0
67 0 0 0
68 0 0 0
69 0 0 0
70 0 0 0
71 0 0 0
72 0 0 0
73 0 0 0
74 0 0 0
75 0 0 0
76 0 0 0
77 0 0 0
78 0 0 0
79 0 0 0
80 0 0 0
81 0 0 0
82 0 0 0
83 0 0 0
84 0 0 0
85 0 0 0
86 0 0 0
87 0 0 0
88 0 0 0
89 0 0 0
90 0 0 0
91 0 0 0
92 0 0 0
93 0 0 0
94 0 0 0
95 0 0 0
96 0 0 0
97 0 0 0
98 0 0 0
99 0 0 0
100 0 0 0
101 0 0 0
102 0 0 0
103 0 0 0
104 0 0 0
105 0 0 0
106 0 0 0
107 0 0 0
108 0 0 0
109 0 0 0
110 0 0 0
111 0 0 0
112 0 0 0
113 0 0 0
114 0 0 0
115 0 0 0
116 0 0 0
117 0 0 0
118 0 0 0
119 0 0 0
120 0 0 0
121 0 0 0
122 0 0 0
123 0 0 0
124 0 0 0
125 0 0 0
126 0 0 0
127 0 0 0
128 0 0 0
129 0 0 0
130 0 0 0
131 0 0 0
132 0 0 0
133 0 0 0
134 0 0 0
135 0 0 0
136 0 0 0
137 0 0 0
138 0 0 0
139 0 0 0
140 0 0 0
141 0 0 0
142 0 0 0
143 0 0 0
144 0 0 0
145 0 0 0
146 0 0 0
147 0 0 0
148 0 0 0
149 0 0 0
150 0 0 0
151 0 0 0
152 0 0 0
153 0 0 0
154 0 0 0
155 0 0 0
156 0 0 0
157 0 0 0
158 0 0 0
159 0 0 0
160 0 0 0
161 0 0 0
162 0 0 0
163 0 0 0
164 0 0 0
165 0 0 0
166 0 0 0
167 0 0 0
168 0 0 0
169 0 0 0
170 0 0 0
171 0 0 0
172 0 0 0
173 0 0 0
174 0 0 0
175 0 0 0
176 0 0 0
177 0 0 0
178 0 0 0
179 0 0 0
180 0 0 0
181 0 0 0
182 0 0 0
183 0 0 0
184 0 0 0
185 0 0 0
186 0 0 0
187 0 0 0
188 0 0 0
189 0 0 0
190 0 0 0
191 0 0 0
192 0 0 0
193 0 0 0
194 0 0 0
195 0 0 0
196 0 0 0
197 0 0 0
198 0 0 0
199 0 0 0
200 0 0 0
201 0 0 0
202 0 0 0
203 0 0 0
204 0 0 0
205 0 0 0
206 0 0 0
207 0 0 0
208 0 0 0
209 0 0 0
210 0 0 0
211 0 0 0
212 0 0 0
213 0 0 0
214 0 0 0
215 0 0 0
216 0 0 0
217 0 0 0
218 0 0 0
219 0 0 0
220 0 0 0
221 0 0 0
222 0 0 0
223 0 0 0
224 0 0 0
225 0 0 0
226 0 0 0
227 0 0 0
228 0 0 0
229 0 0 0
230 0 0 0
231 0 0 0
232 0 0 0
233 0 0 0
234 0 0 0
235 0 0 0
236 0 0 0
237 0 0 0
238 0 0 0
239 0 0 0
240 0 0 0
241 0 0 0
242 0 0 0
243 0 0 0
244 0 0 0
245 0 0 0
246 0 0 0
247 0 0 0
248 0 0 0
249 0 0 0
250 0 0 0
251 0 0 0
252 0 0 0
253 0 0 0
254 0 0 0
255 0 0 0
# Palette
# Mode: RGB
0 0 0 0
1 187 0 0
2 0 187 0
3 187 187 0
4 0 0 187
5 187 0 187
6 0 187 187
7 187 187 187
8 85 85 85
9 255 85 85
10 85 255 85
11 255 255 85
12 85 85 255
13 255 85 255
14 85 255 255
15 255 255 255
16 0 0 0
17 0 0 0
18 0 0 0
19 0 0 0
20 0 0 0
21 0 0 0
22 0 0 0
23 0 0 0
24 0 0 0
25 0 0 0
26 0 0 0
27 0 0 0
28 0 0 0
29 0 0 0
30 0 0 0
31 0 0 0
32 0 0 0
33 0 0 0
34 0 0 0
35 0 0 0
36 0 0 0
37 0 0 0
38 0 0 0
39 0 0 0
40 0 0 0
41 0 0 0
42 0 0 0
43 0 0 0
44 0 0 0
45 0 0 0
46 0 0 0
47 0 0 0
48 0 0 0
49 0 0 0
50 0 0 0
51 0 0 0
52 0 0 0
53 0 0 0
54 0 0 0
55 0 0 0
56 0 0 0
57 0 0 0
58 0 0 0
59 0 0 0
60 0 0 0
61 0 0 0
62 0 0 0
63 0 0 0
64 0 0 0
65 0 0 0
66 0 0 0
67 0 0 0
68 0 0 0
69 0 0 0
70 0 0 0
71 0 0 0
72 0 0 0
73 0 0 0
74 0 0 0
75 0 0 0
76 0 0 0
77 0 0 0
78 0 0 0
79 0 0 0
80 0 0 0
81 0 0 0
82 0 0 0
83 0 0 0
84 0 0 0
85 0 0 0
86 0 0 0
87 0 0 0
88 0 0 0
89 0 0 0
90 0 0 0
91 0 0 0
92 0 0 0
93 0 0 0
94 0 0 0
95 0 0 0
96 0 0 0
97 0 0 0
98 0 0 0
99 0 0 0
100 0 0 0
101 0 0 0
102 0 0 0
103 0 0 0
104 0 0 0
105 0 0 0
106 0 0 0
107 0 0 0
108 0 0 0
109 0 0 0
110 0 0 0
111 0 0 0
112 0 0 0
113 0 0 0
114 0 0 0
115 0 0 0
116 0 0 0
117 0 0 0
118 0 0 0
119 0 0 0
120 0 0 0
121 0 0 0
122 0 0 0
123 0 0 0
124 0 0 0
125 0 0 0
126 0 0 0
127 0 0 0
128 0 0 0
129 0 0 0
130 0 0 0
131 0 0 0
132 0 0 0
133 0 0 0
134 0 0 0
135 0 0 0
136 0 0 0
137 0 0 0
138 0 0 0
139 0 0 0
140 0 0 0
141 0 0 0
142 0 0 0
143 0 0 0
144 0 0 0
145 0 0 0
146 0 0 0
147 0 0 0
148 0 0 0
149 0 0 0
150 0 0 0
151 0 0 0
152 0 0 0
153 0 0 0
154 0 0 0
155 0 0 0
156 0 0 0
157 0 0 0
158 0 0 0
159 0 0 0
160 0 0 0
161 0 0 0
162 0 0 0
163 0 0 0
164 0 0 0
165 0 0 0
166 0 0 0
167 0 0 0
168 0 0 0
169 0 0 0
170 0 0 0
171 0 0 0
172 0 0 0
173 0 0 0
174 0 0 0
175 0 0 0
176 0 0 0
177 0 0 0
178 0 0 0
179 0 0 0
180 0 0 0
181 0 0 0
182 0 0 0
183 0 0 0
184 0 0 0
185 0 0 0
186 0 0 0
187 0 0 0
188 0 0 0
189 0 0 0
190 0 0 0
191 0 0 0
192 0 0 0
193 0 0 0
194 0 0 0
195 0 0 0
196 0 0 0
197 0 0 0
198 0 0 0
199 0 0 0
200 0 0 0
201 0 0 0
202 0 0 0
203 0 0 0
204 0 0 0
205 0 0 0
206 0 0 0
207 0 0 0
208 0 0 0
209 0 0 0
210 0 0 0
211 0 0 0
212 0 0 0
213 0 0 0
214 0 0 0
215 0 0 0
216 0 0 0
217 0 0 0
218 0 0 0
219 0 0 0
220 0 0 0
221 0 0 0
222 0 0 0
223 0 0 0
224 0 0 0
225 0 0 0
226 0 0 0
227 0 0 0
228 0 0 0
229 0 0 0
230 0 0 0
231 0 0 0
232 0 0 0
233 0 0 0
234 0 0 0
235 0 0 0
236 0 0 0
237 0 0 0
238 0 0 0
239 0 0 0
240 0 0 0
241 0 0 0
242 0 0 0
243 0 0 0
244 0 0 0
245 0 0 0
246 0 0 0
247 0 0 0
248 0 0 0
249 0 0 0
250 0 0 0
251 0 0 0
252 0 0 0
253 0 0 0
254 0 0 0
255 0 0 0
# Palette
# Mode: RGB
0 46 52 54
1 204 0 0
2 78 154 6
3 196 160 0
4 52 101 164
5 117 80 123
6 6 152 154
7 211 215 207
8 85 87 83
9 239 41 41
10 138 226 52
11 252 233 79
12 114 159 207
13 173 127 168
14 52 226 226
15 238 238 236
16 0 0 0
17 0 0 0
18 0 0 0
19 0 0 0
20 0 0 0
21 0 0 0
22 0 0 0
23 0 0 0
24 0 0 0
25 0 0 0
26 0 0 0
27 0 0 0
28 0 0 0
29 0 0 0
30 0 0 0
31 0 0 0
32 0 0 0
33 0 0 0
34 0 0 0
35 0 0 0
36 0 0 0
37 0 0 0
38 0 0 0
39 0 0 0
40 0 0 0
41 0 0 0
42 0 0 0
43 0 0 0
44 0 0 0
45 0 0 0
46 0 0 0
47 0 0 0
48 0 0 0
49 0 0 0
50 0 0 0
51 0 0 0
52 0 0 0
53 0 0 0
54 0 0 0
55 0 0 0
56 0 0 0
57 0 0 0
58 0 0 0
59 0 0 0
60 0 0 0
61 0 0 0
62 0 0 0
63 0 0 0
64 0 0 0
65 0 0 0
66 0 0 0
67 0 0 0
68 0 0 0
69 0 0 0
70 0 0 0
71 0 0 0
72 0 0 0
73 0 0 0
74 0 0 0
75 0 0 0
76 0 0 0
77 0 0 0
78 0 0 0
79 0 0 0
80 0 0 0
81 0 0 0
82 0 0 0
83 0 0 0
84 0 0 0
85 0 0 0
86 0 0 0
87 0 0 0
88 0 0 0
89 0 0 0
90 0 0 0
91 0 0 0
92 0 0 0
93 0 0 0
94 0 0 0
95 0 0 0
96 0 0 0
97 0 0 0
98 0 0 0
99 0 0 0
100 0 0 0
101 0 0 0
102 0 0 0
103 0 0 0
104 0 0 0
105 0 0 0
106 0 0 0
107 0 0 0
108 0 0 0
109 0 0 0
110 0 0 0
111 0 0 0
112 0 0 0
113 0 0 0
114 0 0 0
115 0 0 0
116 0 0 0
117 0 0 0
118 0 0 0
119 0 0 0
120 0 0 0
121 0 0 0
122 0 0 0
123 0 0 0
124 0 0 0
125 0 0 0
126 0 0 0
127 0 0 0
128 0 0 0
129 0 0 0
130 0 0 0
131 0 0 0
132 0 0 0
133 0 0 0
134 0 0 0
135 0 0 0
136 0 0 0
137 0 0 0
138 0 0 0
139 0 0 0
140 0 0 0
141 0 0 0
142 0 0 0
143 0 0 0
144 0 0 0
145 0 0 0
146 0 0 0
147 0 0 0
148 0 0 0
149 0 0 0
150 0 0 0
151 0 0 0
152 0 0 0
153 0 0 0
154 0 0 0
155 0 0 0
156 0 0 0
157 0 0 0
158 0 0 0
159 0 0 0
160 0 0 0
161 0 0 0
162 0 0 0
163 0 0 0
164 0 0 0
165 0 0 0
166 0 0 0
167 0 0 0
168 0 0 0
169 0 0 0
170 0 0 0
171 0 0 0
172 0 0 0
173 0 0 0
174 0 0 0
175 0 0 0
176 0 0 0
177 0 0 0
178 0 0 0
179 0 0 0
180 0 0 0
181 0 0 0
182 0 0 0
183 0 0 0
184 0 0 0
185 0 0 0
186 0 0 0
187 0 0 0
188 0 0 0
189 0 0 0
190 0 0 0
191 0 0 0
192 0 0 0
193 0 0 0
194 0 0 0
195 0 0 0
196 0 0 0
197 0 0 0
198 0 0 0
199 0 0 0
200 0 0 0
201 0 0 0
202 0 0 0
203 0 0 0
204 0 0 0
205 0 0 0
206 0 0 0
207 0 0 0
208 0 0 0
209 0 0 0
210 0 0 0
211 0 0 0
212 0 0 0
213 0 0 0
214 0 0 0
215 0 0 0
216 0 0 0
217 0 0 0
218 0 0 0
219 0 0 0
220 0 0 0
221 0 0 0
222 0 0 0
223 0 0 0
224 0 0 0
225 0 0 0
226 0 0 0
227 0 0 0
228 0 0 0
229 0 0 0
230 0 0 0
231 0 0 0
232 0 0 0
233 0 0 0
234 0 0 0
235 0 0 0
236 0 0 0
237 0 0 0
238 0 0 0
239 0 0 0
240 0 0 0
241 0 0 0
242 0 0 0
243 0 0 0
244 0 0 0
245 0 0 0
246 0 0 0
247 0 0 0
248 0 0 0
249 0 0 0
250 0 0 0
251 0 0 0
252 0 0 0
253 0 0 0
254 0 0 0
255 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment