-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractpixel.py
64 lines (46 loc) · 1.79 KB
/
extractpixel.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
from PIL import Image
# open folder, load all images, for each image, output to image_name_r.mem, image_name_g.mem, image_name_b.mem
# open folder
import os
path = os.getcwd()
print(path)
TARGET_DIR = path + '/small_assets/'
OUTPUT_DIR = path + '/small_bin_assets/'
MASTER_FILE = OUTPUT_DIR + 'master'
# load all images
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(TARGET_DIR) if isfile(join(TARGET_DIR, f))]
onlyfiles.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
print(onlyfiles)
# open master file
m_red = open(MASTER_FILE + "_r.mem", 'w')
m_green = open(MASTER_FILE + "_g.mem", 'w')
m_blue = open(MASTER_FILE + "_b.mem", 'w')
# for each image, output to image_name_r.mem, image_name_g.mem, image_name_b.mem
for f in onlyfiles:
img = Image.open(TARGET_DIR + f)
# change image to RGB
img = img.convert('RGB')
pixels = img.load()
width, height = img.size
red = open(OUTPUT_DIR + f[:-4] + '_r.mem', 'w')
green = open(OUTPUT_DIR + f[:-4] + '_g.mem', 'w')
blue = open(OUTPUT_DIR + f[:-4] + '_b.mem', 'w')
print("width: %d, height: %d" % (width, height))
for y in range(height): # this row
for x in range(width): # and this row was exchanged
r, g, b = pixels[x, y]
# in case your image has an alpha channel
# r, g, b, a = pixels[x, y]
# write hex to file
# file.write("%02x %02x %02x " % (r//32, g//32, b//32))
red.write("%02x " % (r//16))
green.write("%02x " % (g//16))
blue.write("%02x " % (b//16))
m_red.write("%02x " % (r//16))
m_green.write("%02x " % (g//16))
m_blue.write("%02x " % (b//16))
# red.write('\n')
# green.write('\n')
# blue.write('\n')