python webp圖片格式轉化的方法
本文實例為大家分享了python webp圖片格式轉化的具體代碼,供大家參考,具體內容如下
1、將本地的webp圖片轉換為jpg2、將下載的webp格式圖片直接保存為jpg
代碼如下:
1、將本地的webp圖片轉換為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、將下載的webp格式圖片直接保存為jpg
from io import BytesIOfrom PIL import Imageimport requests url = ’http:xxxxx.JPG’ # 需要下載的圖片地址headers = {} # 請求頭,按需添加 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’)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
