diff options
author | clarkzjw <[email protected]> | 2022-09-30 22:28:39 -0700 |
---|---|---|
committer | clarkzjw <[email protected]> | 2022-09-30 22:28:39 -0700 |
commit | 6f6fc0c91b6064c96043b6eadc60de11b69175f8 (patch) | |
tree | 0b50ed61432760717c0edbb8d8700d94a95a76a2 /square.py | |
parent | cfbabd9eb7d99f55854804ccb9e22d154e0d35e2 (diff) | |
download | Square-6f6fc0c91b6064c96043b6eadc60de11b69175f8.tar.gz |
+ add padding and drop shadow
Diffstat (limited to 'square.py')
-rw-r--r-- | square.py | 85 |
1 files changed, 85 insertions, 0 deletions
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) | ||