-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseImages.py
33 lines (29 loc) · 1.02 KB
/
parseImages.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
from PIL import Image
import colorsys
import struct
filebase = 'test'
img = Image.open(filebase + ".png")
img_std = Image.open(filebase + "_std.png")
pix = img.load()
pix_std = img_std.load()
if img.size != img_std.size:
raise Exception("Images should have the same size")
print(img.size) #Get the width and hight of the image for iterating over
result = open(filebase+'.res', 'wb')
for y in range(img.size[1]):
print(y)
for x in range(img.size[0]):
# For std image we have to convert RGBA to a 4 byte int
rgba = pix_std[x, y]
bvalues = []
for val in rgba:
bvalues.append(struct.pack('>B', val))
for bvalue in reversed(bvalues):
result.write(bvalue)
# For normal image we have to convert RGB to HSV, but if A is deferent than
rgba = pix[x, y]
hue = colorsys.rgb_to_hsv(rgba[0]/255.0, rgba[1]/255.0, rgba[2]/255.0)[0]*360
#Need to convert to 2 byte
hue = struct.pack('>H', int(hue))
result.write(hue)
result.close()