-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtile_splitter.py
53 lines (43 loc) · 1.93 KB
/
tile_splitter.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
from PIL import Image, ImageDraw
import numpy as np
import os
def get_tile_variants(tile):
"""Generate all unique variants of a tile: original, flipped horizontally, and flipped vertically."""
tile_array = np.array(tile)
variants = [
tile_array,
np.fliplr(tile_array), # Flip horizontally
np.flipud(tile_array) # Flip vertically
]
# Create hashes for each variant
return [hash(variant.tobytes()) for variant in variants]
def identify_unique_tiles(image_path, tile_size=8):
image = Image.open(image_path)
width, height = image.size
tiles = {}
positions = {}
for y in range(0, height, tile_size):
for x in range(0, width, tile_size):
box = (x, y, x + tile_size, y + tile_size)
tile = image.crop(box)
tile_hashes = get_tile_variants(tile)
if not any(tile_hash in tiles for tile_hash in tile_hashes):
tiles[tile_hashes[0]] = tile
positions[(x, y)] = tile_hashes[0]
return tiles, positions, width, height
def create_image_with_unique_tiles(image_path, tiles, positions, width, height, tile_size=8):
image = Image.open(image_path)
new_image = Image.new("RGBA", (width, height), (0, 0, 0, 0)) # Create a transparent image
for (x, y), tile_hash in positions.items():
tile = tiles[tile_hash]
new_image.paste(tile, (x, y))
return new_image
def main(image_path, output_path):
tiles, positions, width, height = identify_unique_tiles(image_path)
new_image = create_image_with_unique_tiles(image_path, tiles, positions, width, height)
new_image.save(output_path)
print(f"Processed image saved to {output_path}")
if __name__ == "__main__":
image_path = r"C:\Users\Celia Dawn\Documents\Roms\Decomps\celias-stupid-repository\your_image.png"
output_path = r"C:\Users\Celia Dawn\Desktop\output\processed_image.png"
main(image_path, output_path)