首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python中将.webp转换为.apng?

如何在python中将.webp转换为.apng?
EN

Stack Overflow用户
提问于 2022-03-14 00:10:33
回答 2查看 395关注 0票数 0

我正在尝试将.webp中的动画图像转换为.apng;我尝试了以下方法:

代码语言:javascript
复制
from PIL import Image, ImageSequence
from apng import APNG
im = Image.open('/content/animate_w.webp')
#im.save('/content/animate_w.apng', 'apng', save_all = True, optimize = True, background=0) # not work
im.save('/content/animate_w.png', 'png', save_all = True, optimize = True, background=0)
im = Image.open('/content/animate_w.png')

i = 0
for frame in ImageSequence.Iterator(im):
  ex_command = f'frame_{i} = frame'
  exec(ex_command)
  i += 1


# not really sure what's next
files = [
  ("frame_1.png", 100),
  ("frame_2.png", 200),
  ("frame_3.png", 300)
]

im = APNG()
for file, delay in files:
  im.append_file(file, delay=delay)
im.save("result.apng")

单独保存的框架部分不工作,我不知道下一步如何进行。有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-14 03:29:46

您有正确的方向,只需添加步骤从webp文件中提取框架。

我希望下面的代码可以添加更多关于如何实现它的想法。

我使用网络工具提取帧

代码语言:javascript
复制
from webptools import webpmux_getframe
from PIL import Image, ImageSequence
from apng import APNG

# Load the webp file
# Downloaded from https://pullzone1-corydowdywebdesi.netdna-ssl.com/assets/blog/apngwebp/squirrel.q70.m6.mixed.webp
im = Image.open('squirrel.q70.m6.mixed.webp')

# Get the number of frames
num_of_frame = 0
for frame in ImageSequence.Iterator(im):
    ex_command = f'frame_{num_of_frame} = frame'
    exec(ex_command)
    num_of_frame += 1

# Extract the frames
list_of_files = []
for i in range(num_of_frame):
    webpmux_getframe(input_image='squirrel.q70.m6.mixed.webp', output_image=f'output_frame{i}.png', frame_number=i)
    list_of_files.append(f'output_frame{i}.png')

# Save to APNG
im = APNG()
for filename in list_of_files:
    im.append_file(filename)
im.save('result.apng')

# Load frame from APNG file
im = APNG.open('result.apng')
for i, (png, control) in enumerate(im.frames):
    png.save(f'apng_frame_{i}.png')

另一个不使用webptools的解决方案是使用PIL中的WebPimageFile。

代码语言:javascript
复制
from PIL import WebPImagePlugin
from apng import APNG

# Load webp and extract the frames
imwebp = WebPImagePlugin.WebPImageFile('squirrel.q70.m6.mixed.webp')
nframes = 0
list_of_files = []
while imwebp:
    imwebp.seek(nframes)
    imwebp.save(f'output_frame{nframes}.png', 'PNG')
    list_of_files.append(f'output_frame{nframes}.png')
    nframes += 1
    try:
        imwebp.seek(nframes)
    except EOFError:
        break

# Save to APNG
im = APNG()
for filename in list_of_files:
    im.append_file(filename)
im.save('result.apng')

# Load frame from APNG file
im = APNG.open('result.apng')
for i, (png, control) in enumerate(im.frames):
    png.save(f'apng_frame_{i}.png')
票数 1
EN

Stack Overflow用户

发布于 2022-11-10 04:27:11

代码语言:javascript
复制
from PIL import WebPImagePlugin
from PIL.PngImagePlugin import Disposal

webp_file = WebPImagePlugin.WebPImageFile("file.webp")
frame_idx = 0
frames = []
while True:
    try:
        webp_file.seek(frame_idx)
        frames.append(webp_file.copy())
        frame_idx += 1
    except EOFError:
        break
frames[0].save("file.apng", save_all=True, append_images=frames[1:], duration=webp_file.info["duration"], disposal=Disposal.OP_BACKGROUND)

不需要pyAPNG。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71461852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档