diff options
-rwxr-xr-x | square.py | 45 |
1 files changed, 29 insertions, 16 deletions
@@ -1,9 +1,10 @@ | |||
1 | import sys | 1 | import glob |
2 | import argparse | ||
2 | 3 | ||
3 | from PIL import Image, ImageFilter | 4 | from PIL import Image, ImageFilter |
4 | 5 | ||
5 | 6 | ||
6 | def insta_size_no_padding(img): | 7 | def square_size_no_padding(img): |
7 | w, h = img.size | 8 | w, h = img.size |
8 | new = Image.new(img.mode, (max(w, h), max(w, h)), (255, 255, 255)) | 9 | new = Image.new(img.mode, (max(w, h), max(w, h)), (255, 255, 255)) |
9 | if h >= w: | 10 | if h >= w: |
@@ -13,7 +14,7 @@ def insta_size_no_padding(img): | |||
13 | return new | 14 | return new |
14 | 15 | ||
15 | 16 | ||
16 | def insta_size_padding(img): | 17 | def square_size_padding(img): |
17 | w, h = img.size | 18 | w, h = img.size |
18 | padding_percentage = 0.02 | 19 | padding_percentage = 0.02 |
19 | length = max(w, h) | 20 | length = max(w, h) |
@@ -64,16 +65,28 @@ def drop_shadow(image, offset=(5, 5), background=0xffffff, shadow=0x444444, bord | |||
64 | 65 | ||
65 | 66 | ||
66 | if __name__ == "__main__": | 67 | if __name__ == "__main__": |
67 | if len(sys.argv) <= 1: | 68 | parser = argparse.ArgumentParser(description="Options") |
68 | exit(0) | 69 | parser.add_argument('--padding', action=argparse.BooleanOptionalAction) |
69 | for file in sys.argv[1:]: | 70 | |
70 | names = file.split(".") | 71 | args = parser.parse_args() |
71 | if len(names) != 2: | 72 | |
72 | continue | 73 | files = glob.glob("*") |
73 | try: | 74 | for file in files: |
74 | im = Image.open(file) | 75 | if (file.endswith(".jpg") or file.endswith(".png")) \ |
75 | except Exception as e: | 76 | and (str.find(file, "result") == -1): |
76 | print(str(e)) | 77 | |
77 | continue | 78 | names = file.split(".") |
78 | result = insta_size_no_padding(drop_shadow(im, background=0xFAFAFA, shadow=0x444444, offset=(20, 20))) | 79 | if len(names) != 2: |
79 | result.save("{}-square-shadow.{}".format(names[0], names[1]), quality=100) | 80 | continue |
81 | try: | ||
82 | im = Image.open(file) | ||
83 | except Exception as e: | ||
84 | print(str(e)) | ||
85 | continue | ||
86 | |||
87 | print("Processing {}".format(file)) | ||
88 | if args.padding is True: | ||
89 | result = square_size_padding(drop_shadow(im, background=0xFAFAFA, shadow=0x444444, offset=(20, 20))) | ||
90 | else: | ||
91 | result = square_size_no_padding(drop_shadow(im, background=0xFAFAFA, shadow=0x444444, offset=(20, 20))) | ||
92 | result.save("{}-result.{}".format(names[0], names[1]), quality=100) | ||