-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpngtosus.py
More file actions
43 lines (29 loc) · 1.14 KB
/
pngtosus.py
File metadata and controls
43 lines (29 loc) · 1.14 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
from PIL import Image
def png_to_sus(image_path, sus_path):
try:
image = Image.open(image_path)
except IOError:
print(f"Unable to open image {image_path}")
return
image = image.convert("RGBA")
width, height = image.size
with open(sus_path, 'w') as sus_file:
sus_file.write(f"width={width}\n")
sus_file.write(f"height={height}\n")
sus_file.write("pixel_data=\n")
sus_file.write("{\n")
pixels = list(image.getdata())
for i, pixel in enumerate(pixels):
if i % width == 0 and i != 0:
sus_file.write("\n")
sus_file.write(f"{{{pixel[0]}, {pixel[1]}, {pixel[2]}, {pixel[3]}}}")
if (i + 1) % width != 0:
sus_file.write(", ")
else:
sus_file.write(" ")
sus_file.write("\n}")
print(f"SUS file saved to {sus_path}")
if __name__ == "__main__":
input_png = "Untitled.png"
output_sus = "output_image.sus"
png_to_sus(input_png, output_sus)