1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
import sys
from PIL import Image
from urllib import request
def half_block_image(url, width, height):
return "".join(generate_half_block_image(url, width, height))
def generate_half_block_image(url, width, height):
image = load_image(url)
image.thumbnail((width, height))
px = image.load()
for row in range(image.height // 2):
y1 = row * 2
y2 = row * 2 + 1
for x in range(image.width):
yield half_block_char(px[x, y1], px[x, y2])
yield "\n"
yield "\033[0m"
def load_image(url):
with request.urlopen(url) as f:
return Image.open(f)
def half_block_char(top_color, bottom_color):
bg = f"\033[48;2;{ansi_color(top_color)}m"
fg = f"\033[38;2;{ansi_color(bottom_color)}m"
return f"{fg}{bg}\N{lower half block}"
def ansi_color(color):
r, b, g = color
return f"{r};{b};{g}"
if __name__ == "__main__":
url = sys.argv[1]
print(half_block_image(url, 100, 100))