-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_ascii_img.py
69 lines (56 loc) · 1.77 KB
/
save_ascii_img.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from PIL import Image
import numpy as np
import pandas as pd
ROW, COL = 8, 5
MAX_SIZE = 300
C = 5
def my_func(n, max_val, x):
y = int(x / max_val * n)
return round(y / n, 3)
src_path = "./images/"
src_name = "C293E467-474C-4E3F-B9AF-C38D83901796.jpeg"
img = Image.open(src_path+src_name, mode="r")
n_rows, n_cols = img.size[1] // ROW, img.size[0] // COL
print(img.size) # width, height
size = (n_cols*C, n_rows*C)
scale = 1
if size[0] > MAX_SIZE:
scale = MAX_SIZE / size[0]
size = (int(size[0]*scale), int(size[1]*scale))
if size[1] > MAX_SIZE:
scale = MAX_SIZE / size[1]
size = (int(size[0]*scale), int(size[1]*scale))
print(scale)
img = img.resize(size)
print(img.size)
img = img.convert("L")
img = np.asarray(img)
print(img.shape) # row, column
df = pd.read_csv("brightness_2.csv", index_col=0)
brightness = dict()
for v, s in zip(df['1'], df['0']):
brightness[round(v, 3)] = str(s)
# brightness = list(zip(df['1'], df['0']))
print(brightness)
n = len(brightness)
print(n)
max_val = np.max(img)
n_rows, n_cols = img.shape[0], img.shape[1]
ascii_img = np.full((n_rows*16, n_cols*10), True, dtype=bool)
print(ascii_img.shape)
for i in range(n_rows):
up, bottom = i*16, (i+1)*16
for j in range(n_cols):
left, right = j*10, (j+1)*10
px_val = my_func(n-1, max_val, img[i, j])
ascii_fname = "./char_imgs/img_" + brightness[px_val] + ".png"
char_img = Image.open(ascii_fname, mode="r")
char_img = np.asarray(char_img)
# if i == 100 and 109< j < 111:
# print(char_img)
# print(ascii_fname)
ascii_img[up:bottom, left:right] = char_img
ascii_img = Image.fromarray(ascii_img).convert("L")
res_path = "./results/"
img_name = "ascii_" + src_name
ascii_img.save(res_path + img_name)