aboutsummaryrefslogtreecommitdiff
blob: 6cf3b4835d2f02ecb4fe5b2592221d1781cf02eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import glob
import argparse

from PIL import Image, ImageFilter


def square_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 square_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), 0xFAFAFA)
    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.
    """

    w, h = image.size

    total_width = w + abs(offset[0]) + 2 * border
    total_height = h + abs(offset[1]) + 2 * border
    back = Image.new(image.mode, (total_width, total_height), background)
    shadow_image = Image.new(image.mode, (w, h), shadow)

    shadow_left = border + max(offset[0], 0)
    shadow_top = border + max(offset[1], 0)
    back.paste(shadow_image, (shadow_left, shadow_top))

    n = 0
    while n < iterations:
        back = back.filter(ImageFilter.BLUR)
        n += 1

    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__":
    parser = argparse.ArgumentParser(description="Options")
    parser.add_argument('--padding', action=argparse.BooleanOptionalAction)

    args = parser.parse_args()

    files = glob.glob("*")
    for file in files:
        if (file.endswith(".jpg") or file.endswith(".png")) \
                and (str.find(file, "result") == -1):

            names = file.split(".")
            if len(names) != 2:
                continue
            try:
                im = Image.open(file)
            except Exception as e:
                print(str(e))
                continue

            print("Processing {}".format(file))
            if args.padding is True:
                result = square_size_padding(drop_shadow(im, background=0xFAFAFA, shadow=0x444444, offset=(20, 20)))
            else:
                result = square_size_no_padding(drop_shadow(im, background=0xFAFAFA, shadow=0x444444, offset=(20, 20)))
            result.save("{}-result.{}".format(names[0], names[1]), quality=100)
Powered by cgit v1.2.3 (git 2.41.0)