|
|
@ -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 |
|
|
|
|
|
|
|
img_iter = ImageSequence.Iterator(img) |
|
|
|
first_frame = np.array(img_iter[0].convert("RGBA")) |
|
|
|
|
|
|
|
first_frame = np.array(img) |
|
|
|
for frame_number in range(1, img.n_frames): |
|
|
|
img.seek(frame_number) |
|
|
|
current_frame = np.array(img) |
|
|
|
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: |
|
|
|