diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | exif.py | 54 | ||||
-rw-r--r-- | requirements.txt | 1 | ||||
-rw-r--r-- | square.py | 85 |
4 files changed, 141 insertions, 0 deletions
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 | |||
@@ -0,0 +1,54 @@ | |||
1 | import os | ||
2 | import pathlib | ||
3 | import subprocess | ||
4 | import sys | ||
5 | |||
6 | |||
7 | def rename(month): | ||
8 | # base_directory = pathlib.Path("/mnt/nas/Photo/RAW/2022/") | ||
9 | # base_directory = pathlib.Path("/run/user/1000/kio-fuse-rQvVbo/smb/192.168.1.77/NAS/Photo/RAW/2022/") | ||
10 | base_directory = pathlib.Path("/mnt/c/Users/clarkzjw/Desktop/") | ||
11 | directory = str(base_directory.joinpath(month).absolute()) + "/" | ||
12 | |||
13 | files = os.listdir(directory) | ||
14 | for file in files: | ||
15 | if file.startswith("IMG_") and (file.endswith(".CR2") or file.endswith(".CR3")): | ||
16 | filename = directory + file | ||
17 | old_filename = filename.split("/")[-1].split(".")[0] | ||
18 | img = filename | ||
19 | |||
20 | if file.endswith(".CR3"): | ||
21 | result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Time Stamp'".format(img)], | ||
22 | stdout=subprocess.PIPE) | ||
23 | timestamp = result.stdout.decode('utf-8').strip("\n").replace(".", "-").strip("Time Stamp")[2:] \ | ||
24 | .replace(":", "-").strip("\n") | ||
25 | filename = directory + timestamp + "-{}.CR3".format(old_filename) | ||
26 | |||
27 | elif file.endswith(".CR2"): | ||
28 | result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], | ||
29 | stdout=subprocess.PIPE) | ||
30 | timestamp = result.stdout.decode('utf-8').split('\n')[1].replace(".", "-").strip("Date/Time Original")[ | ||
31 | 2:].replace(":", "-"). \ | ||
32 | strip("\n") | ||
33 | filename = directory + timestamp + "-{}.CR2".format(old_filename) | ||
34 | print(filename) | ||
35 | os.rename(img, filename) | ||
36 | |||
37 | elif file.startswith("PXL_") and file.endswith(".dng"): | ||
38 | filename = directory + file | ||
39 | old_filename = filename.split("/")[-1].split(".")[0] | ||
40 | img = filename | ||
41 | result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], | ||
42 | stdout=subprocess.PIPE) | ||
43 | timestamp = \ | ||
44 | result.stdout.decode('utf-8').strip("\n").split('\n')[1].strip("Date/Time Original")[2:].split('-')[ | ||
45 | 0][:-4].replace(".", "-").replace(":", "-").strip("\n") | ||
46 | filename = directory + timestamp + "-{}.dng".format(old_filename) | ||
47 | print(filename) | ||
48 | os.rename(img, filename) | ||
49 | |||
50 | |||
51 | if __name__ == "__main__": | ||
52 | if len(sys.argv) <= 1: | ||
53 | exit(0) | ||
54 | 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 @@ | |||
1 | import sys | ||
2 | |||
3 | from PIL import Image, ImageFilter | ||
4 | |||
5 | |||
6 | def insta_size_no_padding(img): | ||
7 | w, h = img.size | ||
8 | new = Image.new(img.mode, (max(w, h), max(w, h)), (255, 255, 255)) | ||
9 | if h >= w: | ||
10 | new.paste(img, (int((max(w, h) - w) / 2), 0)) | ||
11 | else: | ||
12 | new.paste(img, (0, int((max(w, h) - h) / 2))) | ||
13 | return new | ||
14 | |||
15 | |||
16 | def insta_size_padding(img): | ||
17 | w, h = img.size | ||
18 | padding_percentage = 0.02 | ||
19 | length = max(w, h) | ||
20 | padding = int(padding_percentage * length) | ||
21 | new = Image.new(img.mode, (length + 2 * padding, length + 2 * padding), 0xeeeeee) | ||
22 | if h >= w: | ||
23 | new.paste(img, (int((length + 2 * padding - w) / 2), padding)) | ||
24 | else: | ||
25 | new.paste(img, (padding, int((length + 2 * padding - h) / 2))) | ||
26 | return new | ||
27 | |||
28 | |||
29 | def drop_shadow(image, offset=(5, 5), background=0xffffff, shadow=0x444444, border=8, iterations=50): | ||
30 | """ | ||
31 | Add a gaussian blur drop shadow to an image. | ||
32 | |||
33 | image - The image to overlay on top of the shadow. | ||
34 | offset - Offset of the shadow from the image as an (x,y) tuple. Can be | ||
35 | positive or negative. | ||
36 | background - Background colour behind the image. | ||
37 | shadow - Shadow colour (darkness). | ||
38 | border - Width of the border around the image. This must be wide | ||
39 | enough to account for the blurring of the shadow. | ||
40 | iterations - Number of times to apply the filter. More iterations | ||
41 | produce a more blurred shadow, but increase processing time. | ||
42 | """ | ||
43 | |||
44 | # Create the backdrop image -- a box in the background colour with a | ||
45 | # shadow on it. | ||
46 | total_width = image.size[0] + abs(offset[0]) + 2 * border | ||
47 | total_height = image.size[1] + abs(offset[1]) + 2 * border | ||
48 | back = Image.new(image.mode, (total_width, total_height), background) | ||
49 | |||
50 | shadow_image = Image.new(image.mode, (image.size[0], image.size[1]), shadow) | ||
51 | |||
52 | # Place the shadow, taking into account the offset from the image | ||
53 | shadow_left = border + max(offset[0], 0) | ||
54 | shadow_top = border + max(offset[1], 0) | ||
55 | back.paste(shadow_image, (shadow_left, shadow_top)) | ||
56 | |||
57 | # Apply the filter to blur the edges of the shadow. Since a small kernel | ||
58 | # is used, the filter must be applied repeatedly to get a decent blur. | ||
59 | n = 0 | ||
60 | while n < iterations: | ||
61 | back = back.filter(ImageFilter.BLUR) | ||
62 | n += 1 | ||
63 | |||
64 | # Paste the input image onto the shadow backdrop | ||
65 | image_left = border - min(offset[0], 0) | ||
66 | image_top = border - min(offset[1], 0) | ||
67 | back.paste(image, (image_left, image_top)) | ||
68 | |||
69 | return back | ||
70 | |||
71 | |||
72 | if __name__ == "__main__": | ||
73 | if len(sys.argv) <= 1: | ||
74 | exit(0) | ||
75 | for file in sys.argv[1:]: | ||
76 | names = file.split(".") | ||
77 | if len(names) != 2: | ||
78 | continue | ||
79 | try: | ||
80 | im = Image.open(file) | ||
81 | except Exception as e: | ||
82 | print(str(e)) | ||
83 | continue | ||
84 | result = insta_size_padding(drop_shadow(im, background=0xeeeeee, shadow=0x444444, offset=(20, 20))) | ||
85 | result.save("{}-square-shadow.{}".format(names[0], names[1]), quality=100) | ||