Trim Images Script

trim-images-script

Script to trim transparent borders from PNG images while keeping the center
aligned. Can process a single file or a whole directory tree.

Requirements

  • Python 3.8+
  • Pillow

Install Pillow:

pip install Pillow

Supported formats

  • PNG (alpha channel required)
  • JPEG is detected, but it has no alpha channel, so it will be skipped

Usage

Trim one file:

python trim_images.py path/to/image.png

Trim a folder recursively with 1px padding:

python trim_images.py path/to/folder 1

Padding is recommended (typically 1) to avoid edge flicker during animations.

Windows batch example

The included trim_images.bat runs the script on .\images with 1px padding.

Output

  • Each trimmed image is overwritten in place as PNG.
  • For every trimmed image, the script prints:
    Trim image:(w:<px> h:<px> <absolute_path>)
  • Total time is printed as: TIME:<ms>

Notes

  • Only images with transparent borders are trimmed.
  • If the content touches all sides or the image has no alpha channel, it is skipped.
8 Likes