png轉(zhuǎn)webp格式

需求,圖片格式轉(zhuǎn)換,從png轉(zhuǎn)webp。擴展:用戶可選縮放率。

核心方法在于:im.resize((w, h),Image.ANTIALIAS)

代碼如下:

#!/usr/bin/python

# -*- coding: utf-8 -*-

import glob

import os

import threading

import sys

from Tkinter import *

from tkMessageBox import *

from PIL import Image

path = os.path.abspath(os.path.dirname(sys.argv[0]))

def select10():

? successSelect(10)

def select20():

? successSelect(20)

def select30():

? successSelect(30)

def select40():

? successSelect(40)

def select50():

? successSelect(50)

def select60():

? successSelect(60)

def select70():

? successSelect(70)

def select80():

? successSelect(80)

def select90():

? successSelect(90)

def selectInput():

? str = raw_input("請輸入0~100的整數(shù): ")

? if str != "":

? ? print str

? ? if str.isdigit():?

? ? ? ratio = int(str)

? ? ? successSelect(ratio)

? ? ? selectInput()

? ? else:

? ? ? print "輸入錯誤!"

? ? ? selectInput()

? else:

? ? print "輸入錯誤!"

? ? selectInput()

def selectExit():

? os._exit(0)

def successSelect(ratio):

? if os.path.exists(path+'/img_bigger_%d' % ratio) == False:

? ? os.mkdir(path+"/img_bigger_%d" % ratio)

? if os.path.exists(path+'/img_smaller_%d' % ratio) == False:

? ? os.mkdir(path+"/img_smaller_%d" % ratio)

? if ratio < 0:

? ? ratio = 0

? if ratio > 100:

? ? ratio = 100

? index = 0

? for infile in glob.glob(path+"/img/*.png"):

? ? t = threading.Thread(target=create_image, args=(infile, ratio, index,))

? ? t.start()

? ? t.join()

? ? index += 1

def create_image(infile, ratio, index):

? os.path.splitext(infile)

? im = Image.open(infile)

? w,h = im.size

? w = w*ratio//100

? h = h*ratio//100

? im = im.resize((w, h),Image.ANTIALIAS)

? (filepath,tempfilename) = os.path.split(infile)

? (filename,extension) = os.path.splitext(tempfilename)

? fullpath = path+"/img/"+filename + ".webp"

? im.save(fullpath, "WEBP")

? sizeinfile = os.path.getsize(infile)

? sizefullpath = os.path.getsize(fullpath)

? if sizeinfile > sizefullpath:

? ? im.save(path+"/img_smaller_%d/" % ratio+filename + ".webp", "WEBP")

? else:

? ? im.save(path+"/img_bigger_%d/" % ratio+filename + ".webp", "WEBP")

def start():

? Button(text='10', command=select10).pack(fill=X)

? Button(text='20', command=select20).pack(fill=X)

? Button(text='30', command=select30).pack(fill=X)

? Button(text='40', command=select40).pack(fill=X)

? Button(text='50', command=select50).pack(fill=X)

? Button(text='60', command=select60).pack(fill=X)

? Button(text='70', command=select70).pack(fill=X)

? Button(text='80', command=select80).pack(fill=X)

? Button(text='90', command=select90).pack(fill=X)

? Button(text='鍵盤輸入', command=selectInput).pack(fill=X)

? Button(text='退出', command=selectExit).pack(fill=X)

? mainloop()

if __name__ == "__main__":

? ? start()


demo 鏈接:https://github.com/licl19/pngToWebp.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容