Create a gist now

Instantly share code, notes, and snippets.

Trim transparency of all PNG files in a folder
import Image
import sys
import glob
# Trim all png images with alpha in a folder
# Usage "python ../someFolder"
folderName = sys.argv[1]
except :
print "Usage: python ../someFolder"
filePaths = glob.glob(folderName + "/*.png") #search for all png images in the folder
for filePath in filePaths:
imageSize = image.size
imageBox = image.getbbox()
imageComponents = image.split()
if len(imageComponents) < 4: continue #don't process images without alpha
rgbImage ="RGB", imageSize, (0,0,0))
rgbImage.paste(image, mask=imageComponents[3])
croppedBox = rgbImage.getbbox()
if imageBox != croppedBox:
print filePath, "Size:", imageSize, "New Size:",croppedBox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment