-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-convert.py
79 lines (63 loc) · 1.84 KB
/
image-convert.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
70
71
72
73
74
75
76
77
78
79
from PIL import Image
from sys import argv
def imageFormat(file):
img = Image.open(file)
max_size = 128, 64
img.thumbnail(max_size)
new_image = Image.new("RGBA", max_size)
box = tuple((n - o) // 2 for n, o in zip(max_size, img.size))
# noinspection PyTypeChecker
new_image.paste(img, box)
new_image = new_image.convert("1")
return new_image
def imageToArray(img):
imgW, imgH = img.size
img_array = []
line_string = ""
for i in range(1, imgW):
for j in range(1, imgH):
pix_val = img.getpixel((i, j))
if pix_val == 255:
line_string += "0"
else:
line_string += "1"
encoded_list = []
i = 0
while i <= len(line_string) - 1:
count = 1
ch = line_string[i]
j = i
while j < len(line_string):
try:
similar = line_string[j] == line_string[j + 1]
except IndexError:
similar = False
if similar:
count += 1
j += 1
else:
break
encoded_list.append((count, ch))
i = j + 1
img_array.append(encoded_list)
line_string = ""
return img_array
def arrayToPython(array, file_location):
file_name = file_location.split(".")[0]
file = open(f"{file_location}", 'a')
file.write(f"{file_name} = ")
print(array, file=file)
file.close()
if __name__ == "__main__":
arguments = argv
arguments.pop(0)
if len(arguments) > 1:
raise Exception()
elif len(arguments) == 0:
file = "convert.png"
else:
file = arguments[0]
image = imageFormat(file)
imgArray = imageToArray(image)
arrayToPython(imgArray, file)
image.show()