You can create a Python script using the tkinter library for the simple UI, PIL (Pillow) for handling images, and os for file operations. Below is a basic implementation:
import os
from tkinter import Tk, filedialog, Label, Button, Entry, IntVar, StringVar
from tkinter.ttk import Progressbar
from PIL import Image
def select_file():
file_path = filedialog.askopenfilename(
filetypes=[("Image files", "*.png *.jpg *.bmp *.gif")])
if file_path:
img = Image.open(file_path)
filepath_var.set(file_path)
width, height = img.size
dimensions_var.set(f"Dimensions: {width}x{height}")
img.close()
def process_sprites():
file_path = filepath_var.get()
if not file_path:
return
x_tiles = x_tiles_var.get()
y_tiles = y_tiles_var.get()
try:
img = Image.open(file_path)
img_width, img_height = img.size
tile_width = img_width // x_tiles
tile_height = img_height // y_tiles
base_name = os.path.basename(file_path)
name_part = os.path.splitext(base_name)[0]
dest_folder = f"{name_part}_XTiles{x_tiles}YTiles{y_tiles}_TotalImages{x_tiles*y_tiles}"
if not os.path.exists(dest_folder):
os.mkdir(dest_folder)
total_images = x_tiles * y_tiles
for y in range(y_tiles):
for x in range(x_tiles):
left = x * tile_width
upper = y * tile_height
right = left + tile_width
lower = upper + tile_height
sprite = img.crop((left, upper, right, lower))
sprite_filename = f"{name_part}_{x}_{y}.png"
sprite.save(os.path.join(dest_folder, sprite_filename))
progress_bar["value"] = ((y * x_tiles) + x + 1) / total_images * 100
root.update_idletasks()
img.close()
except Exception as e:
print(f"Error: {e}")
root = Tk()
root.title("Sprite Sheet Extractor")
filepath_var = StringVar()
dimensions_var = StringVar()
x_tiles_var = IntVar(value=5)
y_tiles_var = IntVar(value=5)
Label(root, text="Selected File:").grid(row=0, column=0)
Label(root, textvariable=filepath_var).grid(row=0, column=1)
Button(root, text="Select File", command=select_file).grid(row=1, column=0, columnspan=2)
Label(root, textvariable=dimensions_var).grid(row=2, column=0, columnspan=2)
Label(root, text="X Tiles:").grid(row=3, column=0)
Entry(root, textvariable=x_tiles_var).grid(row=3, column=1)
Label(root, text="Y Tiles:").grid(row=4, column=0)
Entry(root, textvariable=y_tiles_var).grid(row=4, column=1)
progress_bar = Progressbar(root, orient="horizontal", mode="determinate")
progress_bar.grid(row=5, column=0, columnspan=2, sticky='ew')
Button(root, text="Process", command=process_sprites).grid(row=6, column=0, columnspan=2)
root.mainloop()
Explanation
- File Selection: Uses
tkinter.filedialog.askopenfilename to select an image file.
- UI Elements: Built with
tkinter to gather user input and display file information.
- Image Processing: Utilizes
PIL.Image to handle sprite extraction by iterating over each tile and saving it.
- Progress Bar: Updates as each sprite is saved.
Installation
You need to have Pillow installed:
pip install pillow
Use this script as a starting point and modify it according to your specific requirements or preferences.