Skip to content

Instantly share code, notes, and snippets.

@joeyklee
Created June 3, 2019 16:22
Show Gist options
  • Save joeyklee/9608dad7b8d2e9746a7785caa48d706e to your computer and use it in GitHub Desktop.
Save joeyklee/9608dad7b8d2e9746a7785caa48d706e to your computer and use it in GitHub Desktop.
simple script to crop images
from PIL import Image
import os
# Getting the current work directory (cwd)
thisdir = os.getcwd() + "/images/santiago_urban_200/"
crop_rectangle = (36, 36, 164, 164)
# r=root, d=directories, f = files
for r, d, f in os.walk(thisdir):
for file in f:
if ".png" in file:
print(os.path.join(r, file))
imPath = os.path.join(r, file)
im = Image.open(imPath)
cropped_im = im.crop(crop_rectangle)
outName = os.path.splitext(file)
outFile = os.path.join(r, outName[0] + "_cropped" + outName[1])
# cropped_im.show()
cropped_im.save(outFile) # Saves the modified pixels to image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment