-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
58 lines (44 loc) · 1.67 KB
/
convert.py
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
import glob
import os
import pathlib
from PIL import Image, ImageFilter, ImageSequence
import numpy as np
from tqdm import tqdm
def make_border(f, border_size):
gf = f.convert('LA')
edge = gf.filter(ImageFilter.FIND_EDGES).filter(ImageFilter.SMOOTH)
l, a = edge.split()
_l, _a = np.full_like(a, 255), np.array(a)
img_array = np.stack([_l, _a], 2)
border = Image.fromarray(np.uint8(img_array), "LA")
border_color = border.convert('RGBA')
diff = [-border_size, border_size]
res = f
for xd in range(-border_size, border_size + 1):
for yd in range(-border_size, border_size + 1):
b = border_color.rotate(0, translate=(xd, yd))
res = Image.alpha_composite(b, res)
return res
def main():
BORDER_SIZE = 2
INPUT_DIR = pathlib.Path('./original/')
OUTPUT_DIR = pathlib.Path('./edged/')
os.makedirs(OUTPUT_DIR, exist_ok=True)
files = INPUT_DIR.glob('*')
for file in tqdm(files):
# RGBAで保存できないため、エラーになるのでスキップ
if file.suffix == '.jpg':
continue
f = Image.open(file)
duration, loop = f.info.get('duration', 0), f.info.get('loop', 0)
frames = []
for frame in ImageSequence.Iterator(f):
bf = make_border(frame.convert('RGBA'), BORDER_SIZE)
frames.append(bf)
if len(frames) > 1:
frames[0].save(OUTPUT_DIR / file.name, save_all=True,
append_images=frames[1:], optimize=False, duration=duration, loop=loop, transparency=255, disposal=2)
else:
frames[0].save(OUTPUT_DIR / file.name)
if __name__ == "__main__":
main()