-
Notifications
You must be signed in to change notification settings - Fork 5
/
apng_square_and_optimize.py
141 lines (98 loc) · 3.37 KB
/
apng_square_and_optimize.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import apng
import struct
import sys
from PIL import Image
from io import BytesIO
# How to use:
# import this file, and give to resize() an APNG image in bytes
# Change this color if there is pure green in the images
#
# TODO choose a random color and make sure it is not present in the image
ALPHA_COLOR = (0, 255, 0)
def p_to_rgba(img):
palette = img.palette.tobytes()
fmt = "c" * len(palette)
struct_list = list(struct.unpack(fmt, palette))
all_rbg = [int.from_bytes(i, byteorder='big') for i in struct_list]
for i, j in enumerate(range(0, len(all_rbg), 3)):
color_rgb = tuple(all_rbg[j:j + 3])
if color_rgb == ALPHA_COLOR:
index_alpha = i
break
try:
# Set the `index_alpha`th entry in the palette to opacity '\x00'
img.info["transparency"] = b'\xff' * index_alpha + b'\x00'
except UnboundLocalError:
pass
return img
def rgba_to_rgb(img):
new_image_data = []
for item in img.getdata():
if item[3] < 50:
new_image_data.append(ALPHA_COLOR)
else:
new_image_data.append(item[:3])
new_image = Image.new("RGB", img.size)
new_image.putdata(new_image_data)
return new_image
def to_square(img):
offsets = {
"top": 0,
"left": 0,
}
x, y = img.size
if x == y:
# already square
return img, offsets
size = max(x, y)
if size == x:
offsets["top"] = int((x - y)/2)
else:
offsets["left"] = int((x - y)/2)
new_im = Image.new('RGB', (size, size), ALPHA_COLOR)
new_im.paste(img, (offsets["left"], offsets["top"],))
return new_im, offsets
def resize(f_bytes):
"""
Take a APNG file in bytes in input, and returns a APNG object
"""
new_apng = apng.APNG()
master_palette = None
for i, (png, control) in enumerate(apng.APNG.from_bytes(f_bytes).frames):
pil_frame = Image.open(BytesIO(png.to_bytes()))
if pil_frame.mode == "RGBA":
pil_frame = rgba_to_rgb(pil_frame)
elif pil_frame.mode == "P":
pil_frame = rgba_to_rgb(pil_frame.convert("RGBA"))
else:
print("Error: mode non handled", pil_frame.mode)
sys.exit(1)
if i == 0:
assert control.x_offset == 0
assert control.y_offset == 0
assert control.height != control.width
pil_frame, offsets = to_square(pil_frame)
pil_frame = pil_frame.quantize(colors=256, method=1)
master_palette = pil_frame
control.width = pil_frame.size[0]
control.height = pil_frame.size[0]
else:
pil_frame = pil_frame.quantize(palette=master_palette)
control.x_offset += offsets["left"]
control.y_offset += offsets["top"]
pil_frame = p_to_rgba(pil_frame)
output = BytesIO()
pil_frame.save(output, format="PNG")
png_frame = apng.PNG.from_bytes(output.getvalue())
fc = {
"width": control.width,
"height": control.height,
"x_offset": control.x_offset,
"y_offset": control.y_offset,
"delay": control.delay,
"delay_den": control.delay_den,
"depose_op": control.depose_op,
"blend_op": control.blend_op,
}
new_apng.append(png_frame, **fc)
return new_apng