2. Save the downloaded WebP images as JPG directly

The code is as follows:

1. Convert the local WebP image to JPG

from PIL import Image
 
 
filename = 'xxxxxxxxxx.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')
Copy the code

2. Save the downloaded WebP image as JPG directly

from io import BytesIO
from PIL import Image
import requests
 
 
url = 'http:xxxxx.JPG'Headers = {} Add 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]) 
    
 
im.save('xxx.jpg'.'JPEG')

Copy the code