diff options
Diffstat (limited to 'square.py')
-rwxr-xr-x[-rw-r--r--] | square.py | 10 |
1 files changed, 2 insertions, 8 deletions
@@ -18,7 +18,7 @@ def insta_size_padding(img): | |||
18 | padding_percentage = 0.02 | 18 | padding_percentage = 0.02 |
19 | length = max(w, h) | 19 | length = max(w, h) |
20 | padding = int(padding_percentage * length) | 20 | padding = int(padding_percentage * length) |
21 | new = Image.new(img.mode, (length + 2 * padding, length + 2 * padding), 0xeeeeee) | 21 | new = Image.new(img.mode, (length + 2 * padding, length + 2 * padding), 0xFAFAFA) |
22 | if h >= w: | 22 | if h >= w: |
23 | new.paste(img, (int((length + 2 * padding - w) / 2), padding)) | 23 | new.paste(img, (int((length + 2 * padding - w) / 2), padding)) |
24 | else: | 24 | else: |
@@ -41,8 +41,6 @@ def drop_shadow(image, offset=(5, 5), background=0xffffff, shadow=0x444444, bord | |||
41 | produce a more blurred shadow, but increase processing time. | 41 | produce a more blurred shadow, but increase processing time. |
42 | """ | 42 | """ |
43 | 43 | ||
44 | # Create the backdrop image -- a box in the background colour with a | ||
45 | # shadow on it. | ||
46 | w, h = image.size | 44 | w, h = image.size |
47 | 45 | ||
48 | total_width = w + abs(offset[0]) + 2 * border | 46 | total_width = w + abs(offset[0]) + 2 * border |
@@ -50,19 +48,15 @@ def drop_shadow(image, offset=(5, 5), background=0xffffff, shadow=0x444444, bord | |||
50 | back = Image.new(image.mode, (total_width, total_height), background) | 48 | back = Image.new(image.mode, (total_width, total_height), background) |
51 | shadow_image = Image.new(image.mode, (w, h), shadow) | 49 | shadow_image = Image.new(image.mode, (w, h), shadow) |
52 | 50 | ||
53 | # Place the shadow, taking into account the offset from the image | ||
54 | shadow_left = border + max(offset[0], 0) | 51 | shadow_left = border + max(offset[0], 0) |
55 | shadow_top = border + max(offset[1], 0) | 52 | shadow_top = border + max(offset[1], 0) |
56 | back.paste(shadow_image, (shadow_left, shadow_top)) | 53 | back.paste(shadow_image, (shadow_left, shadow_top)) |
57 | 54 | ||
58 | # Apply the filter to blur the edges of the shadow. Since a small kernel | ||
59 | # is used, the filter must be applied repeatedly to get a decent blur. | ||
60 | n = 0 | 55 | n = 0 |
61 | while n < iterations: | 56 | while n < iterations: |
62 | back = back.filter(ImageFilter.BLUR) | 57 | back = back.filter(ImageFilter.BLUR) |
63 | n += 1 | 58 | n += 1 |
64 | 59 | ||
65 | # Paste the input image onto the shadow backdrop | ||
66 | image_left = border - min(offset[0], 0) | 60 | image_left = border - min(offset[0], 0) |
67 | image_top = border - min(offset[1], 0) | 61 | image_top = border - min(offset[1], 0) |
68 | back.paste(image, (image_left, image_top)) | 62 | back.paste(image, (image_left, image_top)) |
@@ -81,5 +75,5 @@ if __name__ == "__main__": | |||
81 | except Exception as e: | 75 | except Exception as e: |
82 | print(str(e)) | 76 | print(str(e)) |
83 | continue | 77 | continue |
84 | result = insta_size_padding(drop_shadow(im, background=0xeeeeee, shadow=0x444444, offset=(20, 20))) | 78 | result = insta_size_no_padding(drop_shadow(im, background=0xFAFAFA, shadow=0x444444, offset=(20, 20))) |
85 | result.save("{}-square-shadow.{}".format(names[0], names[1]), quality=100) | 79 | result.save("{}-square-shadow.{}".format(names[0], names[1]), quality=100) |