Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed overwriting of sprites with duplicate coords #2

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
atlas/
atlas/
.venv
44 changes: 43 additions & 1 deletion pokatlas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from PIL import Image
import pathlib
import hashlib
from collections import Counter


class Atlas():
Expand All @@ -11,13 +13,16 @@ 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

def add_sprite_hash(self, name, hash):
self.sprite_hashes[name] = hash
LinfanS marked this conversation as resolved.
Show resolved Hide resolved

def get_sprites(self) -> dict:
return self.sprites


def get_atlas(path: pathlib.Path) -> Atlas:
t = path.read_text().strip().split('\n')
Expand All @@ -42,6 +47,9 @@ def get_atlas(path: pathlib.Path) -> Atlas:

return atlas

def get_image_hash(image_path: pathlib.Path) -> str:
return hashlib.md5(image_path.read_bytes()).hexdigest()

def decomp(atlas: Atlas):
atlas_dir = atlas.atlas_path.parent
atlas_img = Image.open(atlas_dir / atlas.img_name)
Expand All @@ -58,6 +66,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))
Expand Down
5 changes: 3 additions & 2 deletions ui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)