forked from R2UZA/china-place-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (62 loc) · 2.11 KB
/
main.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
import os.path
import re
from PIL import Image
from typing import List, Tuple
from os import walk
CONTAINER_SIZE_X = 6000
CONTAINER_SIZE_Y = 6000
CONTAINER_DEFAULT_COLOR = (0, 0, 0, 0)
PIXEL_GROUP_SIZE = 3
IMAGE_DEFAULT_PATH = "./images"
COORD_MATCHER = re.compile(r"\(([0-9]+)\,([0-9]+)\)\.png$")
class PixelImage:
data: List[List[Tuple[int, int, int, int]]]
startX: int
startY: int
def __init__(self):
self.data = []
self.startX = 0
self.startY = 0
def write(self, image: Image):
maxWidth = len(self.data)
maxHeight = len(self.data[0])
for w in range(0, maxWidth):
for h in range(0, maxHeight):
pixel = self.data[w][h]
drawX = self.startX + w * PIXEL_GROUP_SIZE + 1
drawY = self.startY + h * PIXEL_GROUP_SIZE + 1
image.putpixel((drawX, drawY), pixel)
def main():
container = Image.new("RGBA", (CONTAINER_SIZE_X, CONTAINER_SIZE_Y), CONTAINER_DEFAULT_COLOR)
images = findAvailableImages()
for img in images:
img.write(container)
container.save("output.png")
container.save("china_place_template.png")
def findAvailableImages() -> List[PixelImage]:
images = []
for (root, _, files) in walk(IMAGE_DEFAULT_PATH):
for filename in files:
findResult = COORD_MATCHER.findall(str(filename))
if len(findResult) != 0:
(w, h) = findResult[0]
path = os.path.join(root, filename)
image = createImage(path)
image.startX = int(w) * PIXEL_GROUP_SIZE
image.startY = int(h) * PIXEL_GROUP_SIZE
images.append(image)
return images
def createImage(path: str) -> PixelImage:
print(f"Creating image from {path}")
im = Image.open(path)
maxWidth, maxHeight = im.size
result = PixelImage()
for w in range(0, maxWidth):
row = []
result.data.append(row)
for h in range(0, maxHeight):
value = im.getpixel((w, h))
row.append(value)
return result
if __name__ == '__main__':
main()