top of page
  • Cihan Toraman

Automating Texture Atlas Splitting with Python

Subtle Power of Automation in Game Design

In the world of game design, managing assets efficiently is key to both creativity and performance. Today, we delve into a Python-based solution for splitting texture atlases into individual assets. This approach not only streamlines the asset management process but also enhances the flexibility and reusability of textures in game development.


Sample image for auto texture atlas split.

Breaking Down the Code
  1. Setting Up the Environment: The script begins by importing the necessary libraries: tkinter for the GUI, filedialog for file operations, and PIL (Python Imaging Library) for image processing. These tools lay the groundwork for a user-friendly and efficient image splitting tool.

  2. Defining the Splitting Function: The split_image function is the core of this script. It takes an image and a grid size (width x height) as input. By calculating the dimensions of each grid cell, it crops the original image into smaller parts. This method ensures that each segment of the texture atlas is precisely extracted.

  3. Saving the Split Images: The save_images function handles the storage of the cropped images. Each piece of the original image is saved separately in a designated output folder, making it easier for game developers to organize and access individual textures.

  4. Building a User Interface: The script utilizes Tkinter to create a simple yet functional GUI. This includes input fields for the image path, grid dimensions, and output folder, along with 'Browse' and 'Split and Save' buttons. This GUI design empowers users to easily operate the tool without delving into the command line.

  5. Executing the Splitting Process: The split_and_save function ties everything together. It retrieves user inputs, invokes the image splitting and saving functions, and updates the GUI with the status of the operation. This function epitomizes the script's utility, making the process of splitting a texture atlas a matter of a few clicks.


Auto texture atlas split GUI

import tkinter as tk
from tkinter import filedialog
from PIL import Image

def split_image(image_path, grid_size):
    image = Image.open(image_path)
    cell_width = image.width // grid_size[0]
    cell_height = image.height // grid_size[1]
    images = []
    for i in range(grid_size[1]):
        for j in range(grid_size[0]):
            left = j * cell_width
            upper = i * cell_height
            right = (j + 1) * cell_width
            lower = (i + 1) * cell_height
            bbox = (left, upper, right, lower)
            cell_image = image.crop(bbox)
            images.append(cell_image)
    return images

def save_images(images, grid_size, output_folder):
    for i, img in enumerate(images):
        img.save(f'{output_folder}/image_part_{i}.png')

def select_image():
    file_path = filedialog.askopenfilename()
    entry_image_path.delete(0, tk.END)
    entry_image_path.insert(0, file_path)

def split_and_save():
    image_path = entry_image_path.get()
    grid_x = int(entry_grid_x.get())
    grid_y = int(entry_grid_y.get())
    output_folder = entry_output_folder.get()
    images = split_image(image_path, (grid_x, grid_y))
    save_images(images, (grid_x, grid_y), output_folder)
    label_info.config(text=f"Image split into {grid_x}x{grid_y} and saved in '{output_folder}'.")

# Set up the GUI
root = tk.Tk()
root.title('Image Splitter')

# Image path entry
label_image_path = tk.Label(root, text="Image Path:")
label_image_path.grid(row=0, column=0, padx=5, pady=5)

entry_image_path = tk.Entry(root, width=50)
entry_image_path.grid(row=0, column=1, padx=5, pady=5)

button_browse = tk.Button(root, text="Browse", command=select_image)
button_browse.grid(row=0, column=2, padx=5, pady=5)

# Grid size entries
label_grid_x = tk.Label(root, text="Grid Width:")
label_grid_x.grid(row=1, column=0, padx=5, pady=5)

entry_grid_x = tk.Entry(root, width=5)
entry_grid_x.grid(row=1, column=1, padx=5, pady=5, sticky='w')

label_grid_y = tk.Label(root, text="Grid Height:")
label_grid_y.grid(row=1, column=1, padx=5, pady=5)

entry_grid_y = tk.Entry(root, width=5)
entry_grid_y.grid(row=1, column=1, padx=5, pady=5, sticky='e')

# Output folder entry
label_output_folder = tk.Label(root, text="Output Folder:")
label_output_folder.grid(row=2, column=0, padx=5, pady=5)

entry_output_folder = tk.Entry(root, width=50)
entry_output_folder.grid(row=2, column=1, padx=5, pady=5)

button_output_folder = tk.Button(root, text="Browse", command=lambda: entry_output_folder.insert(0, filedialog.askdirectory()))
button_output_folder.grid(row=2, column=2, padx=5, pady=5)

# Split and save button
button_split_save = tk.Button(root, text="Split and Save", command=split_and_save)
button_split_save.grid(row=3, column=1, padx=5, pady=5, sticky='w')

# Info label
label_info = tk.Label(root, text="")
label_info.grid(row=4, column=1, padx=5, pady=5)

# Run the application
root.mainloop()

Summary

This Python script represents a significant step towards optimizing asset management in game design. Automating the process of splitting texture atlases, saves time and reduces the margin for error, allowing game designers and developers to focus more on the creative aspects of their projects. The integration of a GUI further enhances its accessibility, making this tool a valuable addition to any game developer's toolkit.


Stay tuned for more insights and tools that can revolutionize your game design workflow.



Comments


bottom of page