Skip to content

Instantly share code, notes, and snippets.

@thomastweets
Last active February 8, 2024 18:53
Show Gist options
  • Save thomastweets/c7680e41ed88452d3c63401bb35116ed to your computer and use it in GitHub Desktop.
Save thomastweets/c7680e41ed88452d3c63401bb35116ed to your computer and use it in GitHub Desktop.
Python script to trim all png images with white background in a folder
import Image
import sys
import glob
import ImageOps
# Trim all png images with white background in a folder
# Usage "python PNGWhiteTrim.py ../someFolder"
try:
folderName = sys.argv[1]
except :
print "Usage: python PNGWhiteTrim.py ../someFolder"
sys.exit(1)
filePaths = glob.glob(folderName + "/*.png") #search for all png images in the folder
for filePath in filePaths:
image=Image.open(filePath)
image.load()
imageSize = image.size
# remove alpha channel
invert_im = image.convert("RGB")
# invert image (so that white is 0)
invert_im = ImageOps.invert(invert_im)
imageBox = invert_im.getbbox()
cropped=image.crop(imageBox)
print filePath, "Size:", imageSize, "New Size:", imageBox
cropped.save(filePath)
@JeremyFisher
Copy link

Hey, thanks for the code, it was helpful! Ive modified it a tiny bit so you can use padding:

from PIL import Image # pip install Pillow
import sys
import glob
from PIL import ImageOps
import numpy as np


# Trim all png images with white background in a folder
# Usage "python PNGWhiteTrim.py ../someFolder padding"



try:
    folderName = sys.argv[1]
    padding = int(sys.argv[2])
    padding = np.asarray([-1*padding, -1*padding, padding, padding])
except :
    print("Usage: python PNGWhiteTrim.py ../someFolder padding")
    sys.exit(1)

filePaths = glob.glob(folderName + "/*.png") #search for all png images in the folder

for filePath in filePaths:
    image=Image.open(filePath)
    image.load()
    imageSize = image.size

    # remove alpha channel
    invert_im = image.convert("RGB")

    # invert image (so that white is 0)
    invert_im = ImageOps.invert(invert_im)
    imageBox = invert_im.getbbox()
    imageBox = tuple(np.asarray(imageBox)+padding)

    cropped=image.crop(imageBox)
    print(filePath, "Size:", imageSize, "New Size:", imageBox)
    cropped.save(filePath)

So if you want to trim it but leave some padding (lets say of 10), you just do:
python PNGWhiteTrim.py ../someFolder 10
also added the brackets for python3 and added comment for installing Image module :)

@Grayson3455
Copy link

Thanks!

@rfernand2
Copy link

Well done, thanks!

@nate178dawg
Copy link

Great script! Thanks!

@kmaeda16
Copy link

kmaeda16 commented Jan 6, 2022

Thanks! This is great!

@lgvelasco
Copy link

Code not working for the following image:
air com beachbumgammon

Any ideas why?

@dangmanhtruong1995
Copy link

Thanks a lot, it works great for my case.

@TomLucidor
Copy link

TomLucidor commented Oct 18, 2023

Has anyone considered this answer as well? Just checking if there is a unified answer between sites

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment