webp格式图片转换为jpg

1.将本地的webp图片转换为jpg

from PIL import Image

filename = ‘0bb521255dc1eb1830579959afff9407.webp‘
im = Image.open(filename)
if im.mode == "RGBA":
    im.load()  # required for png.split()
    background = Image.new("RGB", im.size, (255, 255, 255))
    background.paste(im, mask=im.split()[3]) 
save_name = filename.replace(‘webp‘, ‘jpg‘)
im.save(‘{}‘.format(save_name), ‘JPEG‘)

2. 将下载的webp格式图片直接保存为jpg

from io import BytesIO
from PIL import Image
import requests

# 这里url是你需要下载的图片地址
resp = requests.get(url, headers=headers)
byte_stream = BytesIO(resp.content)
im = Image.open(byte_stream)
# im.show()
if im.mode == "RGBA":
    im.load()  # required for png.split()
    background = Image.new("RGB", im.size, (255, 255, 255))
    background.paste(im, mask=im.split()[3])  
name = md5_(url)
im.save(‘./photo2/{}.jpg‘.format(name), ‘JPEG‘)

相关推荐