-
Notifications
You must be signed in to change notification settings - Fork 0
/
trim-images.py
39 lines (29 loc) · 957 Bytes
/
trim-images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
import sys
from PIL import Image
from os import listdir
from os.path import join
# from https://gist.github.com/odyniec/3470977
def autocrop_image(image, border = 0):
# Get the bounding box
bbox = image.getbbox()
# Crop the image to the contents of the bounding box
image = image.crop(bbox)
# Determine the width and height of the cropped image
(width, height) = image.size
# Add border
width += border * 2
height += border * 2
# Create a new image object for the output image
cropped_image = Image.new("RGBA", (width, height), (0,0,0,0))
# Paste the cropped image onto the new image
cropped_image.paste(image, (border, border))
# Done!
return cropped_image
directory = sys.argv[1]
images = [f for f in listdir(directory)]
for image in images:
file = join(directory, image)
img = Image.open(file)
img = autocrop_image(img, 0)
img.save(file)