Compare commits

..

1 Commits

Author SHA1 Message Date
xz-dev 0550c0ad3e
Merge 86cb2edcfa into 333567f481 2024-09-15 16:29:03 +08:00
1 changed files with 51 additions and 52 deletions

View File

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