TGA Image Splitter HOW-TO

This project provides a Python script, split_tga.py, which reads a user-specified .in file to split a TGA image into evenly sized 24-bit tiles to use in creating a panorama flying site.

Download the script file and example files here: TGA-Image-Splitter.zip. Use this page for documentation.

Introduction

This TGA image splitter script automates the process of dividing a TGA image into uniformly sized tiles. It reads parameters—such as the TGA filename, the number of rows, and the number of columns from an input file ending with .in. This ensures all tiles share the same dimensions and that none of the process is manual, i.e., no photo editting software is needed.

Dependencies

  • Python 3.7+ (Recommended)

  • Pillow (PIL) library for image handling.

    pip install Pillow
    

Usage

  1. Prepare a .in file (e.g., tiles.in) with the following three lines:

    <TGAFilename>.tga
    rows = <integer>
    columns = <integer>
    

    Any lines beyond the first three are ignored, allowing for optional comments or notes.

  2. Run the Python script from a command line or terminal:

    python split_tga.py tiles.in
    

    Here, split_tga.py is the script, and tiles.in is your .in file.

    Make sure that:

    • split_tga.py is in the same directory or in your PATH.

    • The .in file exists and follows the correct format.

Behavior & Features

TGA File Requirements

  • The TGA file must be an RGB or RGBA file.

  • If it is RGBA (32-bit), the alpha channel is discarded and tiles are saved as 24 bit/channel.

  • The script will prompt the user for confirmation if the TGA width exceeds 32,768 pixels wide.

Rows and Columns

  • The script reads two integers, rows and columns, from lines 2 and 3 of the .in file.

  • The product rows × columns must be ≤ 999. Otherwise, the script exits with an error.

  • The script checks if the TGA pixel width is evenly divisible by columns and if the pixel height is evenly divisible by rows. If not, it stops.

Output Folder

  • The script creates a new folder named Tiles-XXX, where XXX starts at 001 and increments if the folder already exists.

  • All output tiles and the logging.txt file will be placed inside this newly created folder.

Logging

  • A file named logging.txt is created within the Tiles-XXX folder.

  • All console output (e.g., progress, errors, prompts) is appended to the log file.

  • The file includes:

    • A start timestamp and end timestamp.

    • Any error messages, if encountered.

    • Image details (width, height, tile count, tile dimensions, color mode).

    • Elapsed time once processing finishes.

Example

Consider an input file named tiles.in:

example.tga
rows = 2
columns = 4
# Additional comments below this line...
# Will be ignored by the script

Running:

python split_tga.py tiles.in
  • Assumes example.tga exists in the same folder (or you used a path, such as C:\images\example.tga).

  • Splits example.tga into 2 rows × 4 columns = 8 tiles.

  • Creates a folder (e.g. Tiles-001/) and logs all activity in Tiles-001/logging.txt.

  • Saves the tile images within Tiles-001/ as Image_001.tga, Image_002.tga, … up to Image_008.tga.

  • If Tiles-001/ already exists, it checks Tiles-002/, Tiles-003/, etc.

Script Overview

File: split_tga.py

Below is a summary of the script’s workflow:

  1. Read Command-Line Argument:

    • Validates the .in file extension.

  2. Parse `.in` File:

    • Reads three lines: TGA path, rows, columns.

    • Ignores subsequent lines.

  3. Open and Validate the TGA:

    • Confirms the path ends with .tga.

    • Ensures the file is in RGB or RGBA mode.

    • If the width exceeds 32,768 pixels, prompts the user in the console.

  4. Check Row/Column Constraints:

    • Ensures rows × columns ≤ 999. The value is the number of tiles to be created.

    • Checks for even divisibility by rows and columns.

  5. Create Output Folder:

    • Looks for Tiles-001/, Tiles-002/, … until it finds one that does not exist.

  6. Set Up Logging:

    • Creates logging.txt in the new folder.

    • Captures timestamps, progress messages, etc.

  7. Split into Tiles:

    • Crops and saves each tile in 24-bit TGA format.

    • Numbered sequentially from 1 up to rows * columns, with zero padding (e.g., Image_001.tga).

  8. Finish:

    • Logs the total tile count, tile size, color format, end timestamp, and elapsed time.

  9. DOM File:

    • To use the tiles in SeligSIM, specify these values in the .dom file for the pano site:

    [Images]
    NumHorizontal_High = <integer>   ; use columns integer
    NumVertical_High   = <integer>   ; use rows integer
    

MIT License

The software is provided under the MIT License:

Copyright (c) 2025 Michael Selig

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.