Compare commits

...

2 Commits

Author SHA1 Message Date
xz-dev f7314484b4
Merge ca296b41c6 into 333567f481 2024-09-15 17:59:33 +03:00
xz-dev ca296b41c6
fix: is_uniform_animated_webp 2024-09-15 22:49:17 +08:00
1 changed files with 52 additions and 51 deletions

View File

@ -148,14 +148,15 @@ def webp_to_others(data: bytes, mimetype: str) -> bytes:
def is_uniform_animated_webp(data: bytes) -> bool:
img = Image.open(BytesIO(data))
with Image.open(BytesIO(data)) as img:
if img.n_frames <= 1:
return False
return True
first_frame = np.array(img)
for frame_number in range(1, img.n_frames):
img.seek(frame_number)
current_frame = np.array(img)
img_iter = ImageSequence.Iterator(img)
first_frame = np.array(img_iter[0].convert("RGBA"))
for frame in img_iter:
current_frame = np.array(frame.convert("RGBA"))
if not np.array_equal(first_frame, current_frame):
return False
@ -163,8 +164,8 @@ def is_uniform_animated_webp(data: bytes) -> bool:
def webp_to_gif_or_png(data: bytes) -> bytes:
with Image.open(BytesIO(data)) as image:
# check if the webp is animated
image: Image.Image = Image.open(BytesIO(data))
is_animated = getattr(image, "is_animated", False)
if is_animated and not is_uniform_animated_webp(data):
return webp_to_others(data, "image/gif")
@ -187,8 +188,8 @@ def opermize_gif(data: bytes) -> bytes:
def _convert_image(data: bytes, mimetype: str) -> (bytes, int, int):
image: Image.Image = Image.open(BytesIO(data))
new_file = BytesIO()
with Image.open(BytesIO(data)) as image:
with BytesIO() as new_file:
# Determine if the image is a GIF
is_animated = getattr(image, "is_animated", False)
if is_animated: