# Trim Images Script

**URL:** <https://forum.defold.com/t/trim-images-script/82128>\
**Category:** The Defoldmine\
**Created:** [December 22, 2025, 12:18pm UTC](https://forum.defold.com/t/trim-images-script/82128 "2025-12-22T12:18:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [December 22, 2025, 12:18pm UTC](https://forum.defold.com/t/trim-images-script/82128/1 "2025-12-22T12:18:13Z")

</div>

# trim-images-script

> **[GitHub - d954mas/trim-alpha-script](https://github.com/d954mas/trim-alpha-script)**
>
> Contribute to d954mas/trim-alpha-script development by creating an account on GitHub.

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:

```bash
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:

```bash
python trim_images.py path/to/image.png

```

Trim a folder recursively with 1px padding:

```bash
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.
