简介:python将webp转png、将webp转jpg、图片转GIF
webp转png
from PIL import Image, ImageSequence
from PIL import Image
import glob
import os
im_path = '图片路径'
# 加载WebP图片
webp_image = Image.open(im_path)
# 转换并保存为PNG格式
output_dir = 'D:/www/output'
webp_image.save(os.path.join(output_dir, '1.png'), 'PNG')
webp转jpg
from PIL import Image, ImageSequence
from PIL import Image
import glob
import os
im_path = '图片路径'
# 加载WebP图片
webp_image = Image.open(im_path)
# 转换并保存为PNG格式
output_dir = 'D:/www/output'
webp_image.save(os.path.join(output_dir, '1.jpg'), 'JPG')
png转jpg
from PIL import Image, ImageSequence
from PIL import Image
import glob
import os
im_path = '图片路径'
# 加载WebP图片
png_image= Image.open(im_path)
# 如果图片模式是P(即索引色或调色板模式),缺少下面这一步或导致转换失败
png_image = png_image.convert('RGB')
# 转换并保存为PNG格式
output_dir = 'D:/www/output'
png_image.save(os.path.join(output_dir, '1.jpg'), 'JPG')
图片转gif
from PIL import Image, ImageSequence
from PIL import Image
import glob
import os
directory = 'D:/www/source'
image_files = glob.glob(os.path.join(directory, '*.png'))
gif_output_path = os.path.join(directory, 'output.gif')
# 读取图片列表
images = [Image.open(x) for x in image_files]
# 确保所有图片具有相同的模式和尺寸
images = [x.convert('RGB') for x in images]
width, height = images[0].size
for img in images[1:]:
assert img.size == (
width, height), "Error: Images do not have the same dimensions."
# 保存为GIF,duration参数指定了每帧之间的时间(以毫秒为单位)
images[0].save(gif_output_path, save_all=True,
append_images=images[1:], optimize=False, duration=200, loop=0)
print('GIF动画已创建。')
有遗漏或者不对的可以在我的公众号留言哦