From a0d23d7e03823330a3857ad316d6447699847cd1 Mon Sep 17 00:00:00 2001 From: Nicolas Duhamel Date: Mon, 25 Mar 2019 16:04:03 +0100 Subject: [PATCH] reorganize --- src/crop.py | 10 ++++++++++ src/files.py | 20 ++++++++++++++++++++ src/rename.py | 15 +++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/crop.py create mode 100644 src/files.py create mode 100644 src/rename.py diff --git a/src/crop.py b/src/crop.py new file mode 100644 index 0000000..0bfc237 --- /dev/null +++ b/src/crop.py @@ -0,0 +1,10 @@ +from PIL import Image + +def crop(image_path, coords): + """ coordinates as tuple (x1, y1, x2, y2,), + get it from gimp selection tool + crop image, in place + """ + image_obj = Image.open(image_path) + cropped_image = image_obj.crop(coords) + cropped_image.save(image_path) diff --git a/src/files.py b/src/files.py new file mode 100644 index 0000000..4c5c95a --- /dev/null +++ b/src/files.py @@ -0,0 +1,20 @@ +import os + +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 diff --git a/src/rename.py b/src/rename.py new file mode 100644 index 0000000..749f24e --- /dev/null +++ b/src/rename.py @@ -0,0 +1,15 @@ +import os +import math +from pathlib import Path + +from files import get_pictures + +def rename_num(entries, reverse=False): + """Rename file, on the fly""" + 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)