-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (49 loc) · 1.71 KB
/
main.py
File metadata and controls
55 lines (49 loc) · 1.71 KB
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
import time
from PIL import Image
# encode image
def makefile(pixelSkip):
with open("seq.txt", "w") as output:
output.write("")
img = Image.open("input.jpg")
w, h = img.size
pix = 0
# get each pixel and store its data in seq.txt
with open("seq.txt", "a") as output:
for x in range(0, w, pixelSkip):
for y in range(0, h, pixelSkip):
color = img.getpixel((x, y))
output.write(f"|{x}, {y}, {color[0]}, {color[1]}, {color[2]}")
pix += 1
print(f"{x}/{w - 1}")
# decode image
def readfile():
pixels = []
# parse data from seq.txt
with open("seq.txt") as file:
for line in file:
seq = line.split("|")
for i in range(1, len(seq)):
pixels.append(tuple(map(int, seq[i].split(', '))))
# create image from parsed data
img = Image.new("RGB", (pixels[-1][0] + 1, pixels[-1][1] + 1))
for pixel in pixels:
img.putpixel((pixel[0], pixel[1]), (pixel[2], pixel[3], pixel[4]))
img.save("output.png")
# run functions and prompt user input
if __name__ == "__main__":
choice = input("[1] Make input.jpg into seq.txt\n[2] Make seq.txt into output.png\nChoice: ")
if choice == "1":
pixelSkip = input("Pixel scan interval (enter nothing for default 1): ")
if pixelSkip:
makefile(int(pixelSkip))
else:
makefile(1)
print("Process finished!")
time.sleep(1)
elif choice == "2":
readfile()
print("Process finished!")
time.sleep(1)
else:
print("Please choose a valid selection next time!")
time.sleep(1)