The following python script should save each sprite as a new image file from selected…
- User
The following python script should save each sprite as a new image file from selected image, but it does not work. Can you please fix it? 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()
- Assistant
- User
It says Image dimensions must be divisible by the number of tiles specified. But provided image is already a sprite sheet by 4x4.
- Assistant
- User
Now nothing is happening, no UI or no errors showing
- Assistant
- User
It again gave this warning: Image dimensions must be divisible by the number of tiles specified. Can we round numbers so it can run the script. Also we should add total number of images that will be generated to the UI
- Assistant
- User
Script should save image folder into the same folder where image is selected. Total images should be shown in UI.
- Assistant
- User
That worked, can you open newly created folder after script terminates
- Assistant
- User
Is there a way to detect tile amount automatically?
- Assistant
- User
Unfortunately auto detect tile algorithm did not work. Can you try something more advanced?
- Assistant
- User
No it did not work. It detects thousands of tiles which is wrong. Can we try threshold way?
- Assistant
- User
We should keep threshold up to 240 and also we should avoid to count inner pixels.
- Assistant
- User
It did not work. I found this answer on web. May be it would help you to find another way. Here is the answer: es! This is an ideal use case for the Fourier transform. If you squish the image down to 1 pixel height, take just the alpha channel, and do a Fourier transform on it, you will see a frequency peak at a wavelength corresponding to the number of pixels per sprite. You can do the same thing vertically. This will be robust even with closely packed sprites, as long as they have some amount of outline to extract.
If the sprites have irregular width/height but are separated by transparent regions and are in distinct bounding boxes, you can instead pick a random non-transparent pixel and greedily grow a bounding box outward from it until the bounding box has only transient pixels at its outline, and then mark those pixels as “visited” and do the same with an unvisited pixels until you’ve visited all non-transparent pixels.
- Assistant
- User
Now I need another script. Imagine we have a folder with images. These are animation sequence images. We would like to create a sprite sheet. Tile amount should be calculated automatically and in a smart way to leave less empty tile in a document. UI should include browse folder, single frame resolution (all frames will be at the same size), total number of frames, final sprite sheet document's resolution, sprite sheet file should be saved in the folder. But we need to add X,Y tile and total frame number to the document's name
- Assistant
- User
Script should automatically calculate optimum final image width and height and X,Y Tiles and populate UI (with less empty area)
Final image width should be image width x image amount Final image height should be image height x image amount
UI should show final image dimensions.
- Assistant
- User
X and Y Tiles should be automatically calculated for an optimum layout according to total image amount. There is no need for Frame Width and Frame Height options.
- Assistant
- User
Traceback (most recent call last): File "C:\Users\97460\Desktop\Python_Test\Sprite_Sheet_Generator.py", line 80, in total_frames_var = IntVar() NameError: name 'IntVar' is not defined
- Assistant
- User
Now that worked but can you please beautify UI? Add progress bar, widen it. Make it better please
- Assistant
- User
It is better but script runs itself without confirmation. There should be ok and cancel buttons. There should be Project Name options (which will define File Name rest can stay the same like tile and total image numbers) UI can be opened in the center of Windows
- Assistant
- User
- Assistant