-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaint-to-hex-MSX.py
126 lines (102 loc) · 4.91 KB
/
paint-to-hex-MSX.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
from PIL import Image
from tkinter import filedialog
import struct
from math import floor
pal_file = ""
pal_file = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select Palette Image", filetype=(('PNG file', '*.png'),('BMP file', '*.bmp'),("ALL file",'*.*')))
if len(pal_file)!=0:
list_colors = []
openpal = Image.open(pal_file).convert("RGB")
wp,hp = openpal.size
short_pal_name = ""
n=len(pal_file)-1
while n!= 0 and pal_file[n] != '.':
n -= 1
n -= 1
while n!= 0 and pal_file[n] != '/':
short_pal_name = pal_file[n] + short_pal_name
n -= 1
BGR_pal = bytearray()
if wp * hp == 16:
R, G, B = 0, 0, 0
for y in range(0,hp):
for x in range (0,wp):
R, G, B = openpal.getpixel((x,y))
list_colors.append((R, G, B))
R = floor(R // 16)
if R > 8:
R -= 1
G = floor(G // 16)
if G > 8:
G -= 1
B = floor(B // 16)
if B > 8:
B -= 1
BGR_pal += struct.pack(">L", (B * 16 * 16) + (G * 16) + R )[2:]
if list_colors.count(list_colors[0]) != 16:
bit_paint = bytearray()
targ_file = ""
targ_file = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select Picture", filetype=(('PNG file', '*.png'),('BMP file', '*.bmp'),("ALL file",'*.*')))
new_bin_file = ""
if len(targ_file) != 0 :
openpic = Image.open(targ_file).convert("RGB")
w,h = openpic.size
if w % 8 == 0 and h % 8 == 0:
n=len(targ_file)-1
while n!= 0 and targ_file[n] != '.':
n -= 1
n -= 1
while n!= 0 and targ_file[n] != '/':
new_bin_file = targ_file[n] + new_bin_file
n -= 1
display_MODE = ""
while display_MODE == "":
display_MODE = str(input("It is displayed vertically or horizontally? v/h")).lower()
if display_MODE != "v" and display_MODE != "h":
display_MODE = ""
b1, b2 = 0 , 0
if display_MODE == "v":
for y in range(0, h, 8):
for x in range (0, w, 8):
for iz in range(0, 8):
for ix in range(0, 8, 2):
b1, b2 = 0 , 0
RGB_1, RGB_2 = openpic.getpixel((x+ix,y+iz)) , openpic.getpixel((x+ix+1,y+iz))
if RGB_1 in list_colors:
b1 = list_colors.index(RGB_1)
if RGB_2 in list_colors:
b2 = list_colors.index(RGB_2)
bin_color = struct.pack("B", b1 * 16 + b2)
bit_paint += bin_color
else:
for x in range(0, w, 8):
for y in range (0, h, 8):
for iz in range(0, 8):
for ix in range(0, 8, 2):
b1, b2 = 0 , 0
RGB_1, RGB_2 = openpic.getpixel((x+ix,y+iz)) , openpic.getpixel((x+ix+1,y+iz))
if RGB_1 in list_colors:
b1 = list_colors.index(RGB_1)
if RGB_2 in list_colors:
b2 = list_colors.index(RGB_2)
bin_color = struct.pack("B", b1 * 16 + b2)
bit_paint += bin_color
out_file = open(short_pal_name + " MSX Pal.bin", "wb+")
out_file.write(BGR_pal)
out_file.close()
out_file = open(new_bin_file + " MSX Image.bin", "wb+")
out_file.write(bit_paint)
out_file.close()
print("Pic and Pal Bin file done!")
else:
print("Both Image Dimensions isn't a divisible of 8")
else:
print("Palette File Empty")
else:
if wp * hp > 16:
print("The palette Picture exceed 16 colors")
else:
print("The palette Picture is too small")
else:
print("No Palette Picture Selected")