fix: is_uniform_animated_webp

This commit is contained in:
xz-dev 2024-09-15 22:49:17 +08:00
parent 86cb2edcfa
commit ca296b41c6
No known key found for this signature in database
GPG Key ID: A20912F811313E36
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: