commit fb590d8d76d0f293e312466ec2ffd35c9c171baa Author: Nicolas Duhamel Date: Mon Mar 25 12:23:10 2019 +0100 Initial diff --git a/README.md b/README.md new file mode 100644 index 0000000..f153739 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Crappy code, for rapid scripting, modify it on the fly for your purpose + +Keep it simple and stupid diff --git a/crop.py b/crop.py new file mode 100644 index 0000000..f792c74 --- /dev/null +++ b/crop.py @@ -0,0 +1,35 @@ +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)) diff --git a/rename.py b/rename.py new file mode 100644 index 0000000..a96e0d9 --- /dev/null +++ b/rename.py @@ -0,0 +1,39 @@ +########## +# Rename fonctions for scripting on the fly +# +import os +import math +from pathlib import Path + +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 rename(entries, reverse=False): + """Rename file""" + entries = sorted(entries, key=lambda entry: entry.name, reverse=reverse) + digit_length = int(math.log10(len(entries)) + 1) + renamed = {} + for i, entry in enumerate(entries): + renamed[entry] = '{number:0{width}d}{ext}'.format(number=i,width=digit_length,ext=Path(entry.path).suffix) + for src, dst in renamed.items(): + os.rename(src, dst) + +if __name__ == '__main__': + entries = get_pictures('/home/nicolas/tmp') + rename(entries, reverse=True)