From 3fa43d14e43b0f49246067fe98712400cf242957 Mon Sep 17 00:00:00 2001 From: LinfanS Date: Wed, 28 Aug 2024 22:55:23 +0100 Subject: [PATCH 1/2] Initial commit, fixed overwriting of sprites with duplicate coords --- .gitignore | 3 ++- pokatlas.py | 43 +++++++++++++++++++++++++++++++++++++++++++ ui/mainwindow.py | 5 +++-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index eea9333..d0d1bd1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ -atlas/ \ No newline at end of file +atlas/ +.venv \ No newline at end of file diff --git a/pokatlas.py b/pokatlas.py index ce694ca..7a73d93 100644 --- a/pokatlas.py +++ b/pokatlas.py @@ -1,5 +1,7 @@ from PIL import Image import pathlib +import hashlib +from collections import Counter class Atlas(): @@ -11,6 +13,7 @@ def __init__(self, atlas_path: pathlib.Path, img_name: str, img_size: str, img_f self.img_filter = img_filter self.repeat = repeat self.sprites = {} + self.sprite_hashes = {} def add_sprite(self, name, attributes): self.sprites[name] = attributes @@ -18,6 +21,8 @@ def add_sprite(self, name, attributes): def get_sprites(self) -> dict: return self.sprites + def add_sprite_hash(self, name, hash): + self.sprite_hashes[name] = hash def get_atlas(path: pathlib.Path) -> Atlas: t = path.read_text().strip().split('\n') @@ -42,6 +47,10 @@ def get_atlas(path: pathlib.Path) -> Atlas: return atlas +def get_image_hash(image_path: pathlib.Path) -> str: + with open(image_path, "rb") as f: + return hashlib.md5(f.read()).hexdigest() + def decomp(atlas: Atlas): atlas_dir = atlas.atlas_path.parent atlas_img = Image.open(atlas_dir / atlas.img_name) @@ -58,6 +67,40 @@ def decomp(atlas: Atlas): sprite = atlas_img.crop((left, top, right, bottom)) sprite.save(sprites_dir / f'{sprite_name}.png') + atlas.add_sprite_hash(sprite_name, get_image_hash(sprites_dir / f'{sprite_name}.png')) + +def find_duplicates(atlas: Atlas) -> list: + sprites = atlas.get_sprites() + + attributes = sprites.values() + coord_counts = Counter(d['xy'] for d in attributes) + duplicate_coords = {coord for coord, + count in coord_counts.items() if count > 1} + + result = [sprite_name for sprite_name, + d in sprites.items() if d['xy'] in duplicate_coords] + + return result + +def check_duplicates(atlas: Atlas): + atlas_dir = atlas.atlas_path.parent + sprites_dir = atlas_dir / 'sprites' + duplicates = find_duplicates(atlas) + + modified_dupe_sprites = [] + + for sprite_name, attributes in atlas.get_sprites().items(): + if atlas.sprite_hashes[sprite_name] != get_image_hash(sprites_dir / f'{sprite_name}.png'): + if sprite_name in duplicates: + modified_dupe_sprites.append((sprite_name, attributes)) + + for sprite_name, attributes in modified_dupe_sprites: + + atlas.get_sprites().pop(sprite_name) + atlas.add_sprite(sprite_name, attributes) + + removed_hash = atlas.sprite_hashes.pop(sprite_name) + atlas.add_sprite_hash(sprite_name, removed_hash) def rebuild(atlas: Atlas): canvas = Image.new('RGBA', tuple(map(int, atlas.img_size.split(', '))), (255,255,255,0)) diff --git a/ui/mainwindow.py b/ui/mainwindow.py index 2284d6f..dcc9d0e 100644 --- a/ui/mainwindow.py +++ b/ui/mainwindow.py @@ -38,7 +38,7 @@ QMessageBox, ) -from pokatlas import decomp, rebuild, get_atlas +from pokatlas import decomp, check_duplicates, rebuild, get_atlas WINDOW_WIDTH = 800 WINDOW_HEIGHT = 400 @@ -273,6 +273,7 @@ def openAtlas(self): self.sprite_list.setCurrentIndex(self.sprite_list.indexAt(QPoint(0,0))) def saveAtlas(self): + check_duplicates(self.atlas) rebuild(self.atlas) self.openDirectory(self.output_dir) @@ -380,4 +381,4 @@ def openDirectory(self, path: pathlib.Path): if platform.system() == 'Windows': QProcess.startDetached(f'explorer', [path_str]) else: - QDesktopServices.openUrl(path_str) + QDesktopServices.openUrl(path_str) \ No newline at end of file From ba929691c890afb2ad087cd7a48c18623baa2ba3 Mon Sep 17 00:00:00 2001 From: LinfanS Date: Thu, 29 Aug 2024 16:44:42 +0100 Subject: [PATCH 2/2] implemented requested changes, simplified image hashing and moved order of add_sprite_hash --- pokatlas.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pokatlas.py b/pokatlas.py index 7a73d93..5f076f3 100644 --- a/pokatlas.py +++ b/pokatlas.py @@ -17,13 +17,13 @@ def __init__(self, atlas_path: pathlib.Path, img_name: str, img_size: str, img_f def add_sprite(self, name, attributes): self.sprites[name] = attributes - - def get_sprites(self) -> dict: - return self.sprites def add_sprite_hash(self, name, hash): self.sprite_hashes[name] = hash + def get_sprites(self) -> dict: + return self.sprites + def get_atlas(path: pathlib.Path) -> Atlas: t = path.read_text().strip().split('\n') @@ -48,8 +48,7 @@ def get_atlas(path: pathlib.Path) -> Atlas: return atlas def get_image_hash(image_path: pathlib.Path) -> str: - with open(image_path, "rb") as f: - return hashlib.md5(f.read()).hexdigest() + return hashlib.md5(image_path.read_bytes()).hexdigest() def decomp(atlas: Atlas): atlas_dir = atlas.atlas_path.parent