Last active
December 17, 2015 02:39
-
-
Save pixelrevision/5537226 to your computer and use it in GitHub Desktop.
Image cropping with coordinates in file name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Script to crop dead space in images and save out coordinates in the file name. Requires PIL to be installed: | |
http://www.pythonware.com/products/pil/ | |
usage: | |
python crop_images.py path/to/input/dir path/to/output/dir | |
''' | |
from PIL import Image | |
from PIL import ImageOps | |
import os | |
import sys | |
import glob | |
if len(sys.argv) < 3: | |
print "Not enough arguments. You need to supply a input and output directory" | |
exit(1) | |
inputDirectory = sys.argv[1] | |
outputDirectory = sys.argv[2] | |
processedFiles = 0 | |
fileList = os.listdir(inputDirectory) | |
for fileName in fileList: | |
if fileName.endswith(".png"): | |
# load the image | |
inputImage = Image.open(inputDirectory + "/" + fileName) | |
inputImage.load() | |
# setup an alpha image to create a boundbox from | |
alpha = inputImage.split()[-1] | |
alphaImage = Image.merge("RGB", (alpha, alpha, alpha)) | |
bounds = alphaImage.getbbox() | |
# crop and save the image | |
cropped = inputImage.crop(bounds) | |
outputSize = "(" + str(bounds[0]) + "," + str(bounds[1]) + "," + str(bounds[2] - bounds[0]) + "," + str(bounds[3] - bounds[1]) + ")" | |
outputFilename = fileName.replace(".png", outputSize + ".png") | |
cropped.save(outputDirectory + "/" + outputFilename) | |
print "saving: " + outputDirectory + "/" + outputFilename | |
processedFiles += 1 | |
if processedFiles == 0: | |
print "No images were processed" | |
else: | |
print "Completed processing " + str(processedFiles) + " images" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment