Compare commits
1 Commits
f7314484b4
...
0550c0ad3e
Author | SHA1 | Date |
---|---|---|
xz-dev | 0550c0ad3e |
|
@ -148,15 +148,14 @@ def webp_to_others(data: bytes, mimetype: str) -> bytes:
|
|||
|
||||
|
||||
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:
|
||||
return True
|
||||
return False
|
||||
|
||||
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"))
|
||||
first_frame = np.array(img)
|
||||
for frame_number in range(1, img.n_frames):
|
||||
img.seek(frame_number)
|
||||
current_frame = np.array(img)
|
||||
if not np.array_equal(first_frame, current_frame):
|
||||
return False
|
||||
|
||||
|
@ -164,8 +163,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")
|
||||
|
@ -188,8 +187,8 @@ def opermize_gif(data: bytes) -> bytes:
|
|||
|
||||
|
||||
def _convert_image(data: bytes, mimetype: str) -> (bytes, int, int):
|
||||
with Image.open(BytesIO(data)) as image:
|
||||
with BytesIO() as new_file:
|
||||
image: Image.Image = Image.open(BytesIO(data))
|
||||
new_file = BytesIO()
|
||||
# Determine if the image is a GIF
|
||||
is_animated = getattr(image, "is_animated", False)
|
||||
if is_animated:
|
||||
|
|
Loading…
Reference in New Issue