From 6f6fc0c91b6064c96043b6eadc60de11b69175f8 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Fri, 30 Sep 2022 22:28:39 -0700 Subject: + add padding and drop shadow --- README.md | 1 + exif.py | 54 +++++++++++++++++++++++++++++++++++ requirements.txt | 1 + square.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 README.md create mode 100644 exif.py create mode 100644 requirements.txt create mode 100644 square.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..30cf731 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Sqaure \ No newline at end of file diff --git a/exif.py b/exif.py new file mode 100644 index 0000000..15fd059 --- /dev/null +++ b/exif.py @@ -0,0 +1,54 @@ +import os +import pathlib +import subprocess +import sys + + +def rename(month): + # base_directory = pathlib.Path("/mnt/nas/Photo/RAW/2022/") + # base_directory = pathlib.Path("/run/user/1000/kio-fuse-rQvVbo/smb/192.168.1.77/NAS/Photo/RAW/2022/") + base_directory = pathlib.Path("/mnt/c/Users/clarkzjw/Desktop/") + directory = str(base_directory.joinpath(month).absolute()) + "/" + + files = os.listdir(directory) + for file in files: + if file.startswith("IMG_") and (file.endswith(".CR2") or file.endswith(".CR3")): + filename = directory + file + old_filename = filename.split("/")[-1].split(".")[0] + img = filename + + if file.endswith(".CR3"): + result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Time Stamp'".format(img)], + stdout=subprocess.PIPE) + timestamp = result.stdout.decode('utf-8').strip("\n").replace(".", "-").strip("Time Stamp")[2:] \ + .replace(":", "-").strip("\n") + filename = directory + timestamp + "-{}.CR3".format(old_filename) + + elif file.endswith(".CR2"): + result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], + stdout=subprocess.PIPE) + timestamp = result.stdout.decode('utf-8').split('\n')[1].replace(".", "-").strip("Date/Time Original")[ + 2:].replace(":", "-"). \ + strip("\n") + filename = directory + timestamp + "-{}.CR2".format(old_filename) + print(filename) + os.rename(img, filename) + + elif file.startswith("PXL_") and file.endswith(".dng"): + filename = directory + file + old_filename = filename.split("/")[-1].split(".")[0] + img = filename + result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], + stdout=subprocess.PIPE) + timestamp = \ + result.stdout.decode('utf-8').strip("\n").split('\n')[1].strip("Date/Time Original")[2:].split('-')[ + 0][:-4].replace(".", "-").replace(":", "-").strip("\n") + filename = directory + timestamp + "-{}.dng".format(old_filename) + print(filename) + os.rename(img, filename) + + +if __name__ == "__main__": + if len(sys.argv) <= 1: + exit(0) + rename(sys.argv[1]) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a6d4d60 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Pillow==9.2.0 diff --git a/square.py b/square.py new file mode 100644 index 0000000..de4d678 --- /dev/null +++ b/square.py @@ -0,0 +1,85 @@ +import sys + +from PIL import Image, ImageFilter + + +def insta_size_no_padding(img): + w, h = img.size + new = Image.new(img.mode, (max(w, h), max(w, h)), (255, 255, 255)) + if h >= w: + new.paste(img, (int((max(w, h) - w) / 2), 0)) + else: + new.paste(img, (0, int((max(w, h) - h) / 2))) + return new + + +def insta_size_padding(img): + w, h = img.size + padding_percentage = 0.02 + length = max(w, h) + padding = int(padding_percentage * length) + new = Image.new(img.mode, (length + 2 * padding, length + 2 * padding), 0xeeeeee) + if h >= w: + new.paste(img, (int((length + 2 * padding - w) / 2), padding)) + else: + new.paste(img, (padding, int((length + 2 * padding - h) / 2))) + return new + + +def drop_shadow(image, offset=(5, 5), background=0xffffff, shadow=0x444444, border=8, iterations=50): + """ + Add a gaussian blur drop shadow to an image. + + image - The image to overlay on top of the shadow. + offset - Offset of the shadow from the image as an (x,y) tuple. Can be + positive or negative. + background - Background colour behind the image. + shadow - Shadow colour (darkness). + border - Width of the border around the image. This must be wide + enough to account for the blurring of the shadow. + iterations - Number of times to apply the filter. More iterations + produce a more blurred shadow, but increase processing time. + """ + + # Create the backdrop image -- a box in the background colour with a + # shadow on it. + total_width = image.size[0] + abs(offset[0]) + 2 * border + total_height = image.size[1] + abs(offset[1]) + 2 * border + back = Image.new(image.mode, (total_width, total_height), background) + + shadow_image = Image.new(image.mode, (image.size[0], image.size[1]), shadow) + + # Place the shadow, taking into account the offset from the image + shadow_left = border + max(offset[0], 0) + shadow_top = border + max(offset[1], 0) + back.paste(shadow_image, (shadow_left, shadow_top)) + + # Apply the filter to blur the edges of the shadow. Since a small kernel + # is used, the filter must be applied repeatedly to get a decent blur. + n = 0 + while n < iterations: + back = back.filter(ImageFilter.BLUR) + n += 1 + + # Paste the input image onto the shadow backdrop + image_left = border - min(offset[0], 0) + image_top = border - min(offset[1], 0) + back.paste(image, (image_left, image_top)) + + return back + + +if __name__ == "__main__": + if len(sys.argv) <= 1: + exit(0) + for file in sys.argv[1:]: + names = file.split(".") + if len(names) != 2: + continue + try: + im = Image.open(file) + except Exception as e: + print(str(e)) + continue + result = insta_size_padding(drop_shadow(im, background=0xeeeeee, shadow=0x444444, offset=(20, 20))) + result.save("{}-square-shadow.{}".format(names[0], names[1]), quality=100) -- cgit v1.2.3