-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
50 lines (41 loc) · 1.42 KB
/
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
from PIL import Image
import struct
import sys
def convert_png_to_custom(png_path, custom_path):
try:
# Open the image and get its size and pixels
image = Image.open(png_path)
width, height = image.size
pixels = list(image.getdata())
# Write the custom format file
with open(custom_path, 'wb') as file:
file.write(f"{width} {height}\n".encode('utf-8'))
# Convert pixel data to RGB and write to file
for pixel in pixels:
r, g, b = pixel[:3]
file.write(struct.pack('3B', r, g, b))
print(f"Conversion successful: {png_path} -> {custom_path}")
return True
except Exception as e:
print(f"Error during conversion: {e}")
return False
def main(original_path, custom_path):
print(f"Found Image at {original_path}")
print(f"Converting to {custom_path}")
success = convert_png_to_custom(original_path, custom_path)
if success:
print("Conversion Complete.")
else:
print("Failed to convert due to some error.")
return success
def validate_args(args):
if len(args) < 3:
print("Usage: python script.py <original_path> <custom_path>")
return False
return True
if __name__ == "__main__":
if not validate_args(sys.argv):
sys.exit(1)
original_path = sys.argv[1]
custom_path = sys.argv[2]
main(original_path, custom_path)