python webp Image Format Conversion Method
- 2021-11-02 01:28:19
- OfStack
In this paper, we share the specific code of python webp picture format conversion for your reference. The specific contents are as follows
1. Convert the local webp picture to jpg
2. Save the downloaded webp format picture directly as jpg
The code is as follows:
1. Convert the local webp picture 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')
2. Save the downloaded webp format picture directly as jpg
from io import BytesIO
from PIL import Image
import requests
url = 'http:xxxxx.JPG' # Address of pictures to be downloaded
headers = {} # Request header, add on demand
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')