toolbox/crop.py
2019-03-25 12:23:10 +01:00

36 lines
1008 B
Python

import os
from PIL import Image
""" Images cropping, in place"""
def is_picture(entry):
"""Return True if DirEntry is a picture"""
extension = ("jpg","png")
for ext in extension:
if entry.name.endswith('.'+ext):
return True
return False
def get_pictures(path):
"""Scan directory for pictures return DirEntry list of files"""
pictures = []
with os.scandir(path) as it:
for entry in it:
if not entry.name.startswith('.') \
and entry.is_file(follow_symlinks=False) \
and is_picture(entry):
pictures.append(entry)
return pictures
def crop(image_path, coords):
image_obj = Image.open(image_path)
cropped_image = image_obj.crop(coords)
cropped_image.save(image_path)
if __name__ == "__main__":
""" coordinates x1 y1 x2 y2,
get it from gimp selection tool"""
pics = get_pictures('/home/nicolas/tmp')
for p in pics:
crop(p.path, (19,122,499,460))