Python3.4 tkinter,PIL圖片轉(zhuǎn)換
先給大家分享一下全部代碼
import os
from PIL import Image
import tkinter
import tkinter.filedialog
import tkinter.messagebox
class Window():
def __init__(self):
self.root = root = tkinter.Tk()
self.menu = tkinter.Menu(root)
self.submenu = tkinter.Menu(self.menu, tearoff=0)
self.submenu.add_command(label='作者: 王小濤同學(xué)')
root.config(menu=self.submenu)
self.Image = tkinter.StringVar()
self.Image.set('.bmp')
self.mstatus = tkinter.IntVar()
self.fstatus = tkinter.IntVar()
self.mstatus.set(0)
self.fstatus.set(0)
self.status = tkinter.StringVar()
self.label = tkinter.Label(root, text='輸入百分比')
self.label.place(x=5, y=5)
self.entryNew = tkinter.Entry(root)
self.entryNew.place(x=70, y=5)
self.checkM = tkinter.Checkbutton(self.root, text='批量轉(zhuǎn)換', command=self.OnCheckM, variable=self.mstatus, onvalue=1, offvalue=0)
self.checkM.place(x=5, y=30)
self.label = tkinter.Label(root, text='選擇文件')
self.label.place(x=5, y=55)
self.entryFile = tkinter.Entry(root)
self.entryFile.place(x=70, y=55)
self.BrowserFileButton = tkinter.Button(root, text='瀏覽', command=self.BrowserFile)
self.BrowserFileButton.place(x=220, y=55)
self.label = tkinter.Label(root, text='選擇目錄')
self.label.place(x=5, y=90)
self.entryDir = tkinter.Entry(root, state=tkinter.DISABLED)
self.entryDir.place(x=70, y=90)
self.BrowserDirButton = tkinter.Button(root, text='瀏覽', command=self.BrowserDir, state=tkinter.DISABLED)
self.BrowserDirButton.place(x=220, y=90)
self.checkF = tkinter.Checkbutton(root, text='改變文件格式', onvalue=1, offvalue=0, variable=self.fstatus, command=self.OnCheckF)
self.checkF.place(x=5, y=120)
frame = tkinter.Frame(root, )
frame.place(x=10, y=150)
self.rBmp = tkinter.Radiobutton(frame, variable=self.Image, value='.bmp', text='BMP', state=tkinter.DISABLED)
self.rBmp.pack(anchor='w')
self.rJpg = tkinter.Radiobutton(frame, variable=self.Image, value='.jpg', text='JPG', state=tkinter.DISABLED)
self.rJpg.pack(anchor='w')
self.rPng = tkinter.Radiobutton(frame, variable=self.Image, value='.png', text='PNG', state=tkinter.DISABLED)
self.rPng.pack(anchor='w')
self.rGif = tkinter.Radiobutton(frame, variable=self.Image, value='.gif', text='GIF', state=tkinter.DISABLED)
self.rGif.pack(anchor='w')
self.ButtonCov = tkinter.Button(root, text='轉(zhuǎn)換格式', command=self.Conv, )
self.ButtonCov.place(x=120, y=180)
self.statusLabel = tkinter.Label(root, textvariable=self.status, fg='red')
self.statusLabel.place(x=80, y=220)
def OnCheckM(self):
if not self.mstatus.get():
self.entryDir.config(state=tkinter.DISABLED)
self.entryFile.config(state=tkinter.NORMAL)
self.BrowserFileButton.config(state=tkinter.NORMAL)
self.BrowserDirButton.config(state=tkinter.DISABLED)
else:
self.entryDir.config(state=tkinter.NORMAL)
self.entryFile.config(state=tkinter.DISABLED)
self.BrowserFileButton.config(state=tkinter.DISABLED)
self.BrowserDirButton.config(state=tkinter.NORMAL)
def OnCheckF(self):
if not self.fstatus.get():
self.rBmp.config(state=tkinter.DISABLED)
self.rPng.config(state=tkinter.DISABLED)
self.rJpg.config(state=tkinter.DISABLED)
self.rGif.config(state=tkinter.DISABLED)
else:
self.rBmp.config(state=tkinter.NORMAL)
self.rPng.config(state=tkinter.NORMAL)
self.rJpg.config(state=tkinter.NORMAL)
self.rGif.config(state=tkinter.NORMAL)
def BrowserFile(self):
file = tkinter.filedialog.askopenfilename(title='Python player', filetypes=[('JPG', '*.jpg'), ('BMP', '*.bmp'), ('GIF', '*.gif'), ('PNG', '*.png')])
if file:
self.entryFile.delete(0, tkinter.END)
self.entryFile.insert(tkinter.END, file)
def BrowserDir(self):
directory = tkinter.filedialog.askdirectory(title='Python')
if directory:
self.entryDir.delete(0, tkinter.END)
self.entryDir.insert(tkinter.END, directory)
def make(self, file, format=None):
im = Image.open(file)
mode = im.mode
if mode not in('L', 'RGB'):
im = im.convert('RGB')
width, height = im.size
s = self.entryNew.get()
if s == '':
tkinter.messagebox.showerror('出錯(cuò)啦', '請輸入百分比')
return
else:
n = int(s)
nwidth = int(width*n/100)
nheight = int(height*n/100)
thumb = im.resize((nwidth, nheight), Image.ANTIALIAS)
if format:
thumb.save(file[:(len(file)-4)] + '_thumb' + format)
else:
thumb.save(file[:(len(file)-4)] + '_thumb' + file[-4:])
def Conv(self):
n = 0
if self.mstatus.get():
path = self.entryDir.get()
if path == "":
tkinter.messagebox.showerror('出錯(cuò)啦', '請選擇路徑')
return
filenames = os.listdir(path)
if self.fstatus.get():
f = self.Image.get()
print(f)
for filename in filenames:
if filename[-3:] in ('bmp', 'jpg', 'gif', 'png'):
self.make(path+'/'+filename, f)
n += 1
else:
for filename in filenames:
if filename[-3:] in ('bmp', 'jpg', 'gif', 'png'):
self.make(path+'/'+filename)
n += 1
else:
file = self.entryFile.get()
if file == '':
tkinter.messagebox.showerror('出錯(cuò)啦', '請選擇文件')
return
if self.fstatus.get():
f = self.Image.get()
self.make(file, f)
n += 1
else:
self.make(file)
n += 1
self.status.set('成功轉(zhuǎn)換 %d 張圖片' % n)
def mainloop(self):
self.root.minsize(280, 270)
self.root.maxsize(280, 250)
self.root.title('圖片轉(zhuǎn)換')
self.root.mainloop()
if __name__ == "__main__":
window = Window()
window.mainloop()
運(yùn)行后的實(shí)際效果如下

感謝大家對(duì)腳本之家的支持,歡迎提出寶貴意見。
- Python離線安裝PIL 模塊的方法
- python使用PIL模塊獲取圖片像素點(diǎn)的方法
- 解決win64 Python下安裝PIL出錯(cuò)問題(圖解)
- python使用PIL給圖片添加文字生成海報(bào)示例
- Python 使用PIL中的resize進(jìn)行縮放的實(shí)例講解
- Python實(shí)現(xiàn)基于PIL和tesseract的驗(yàn)證碼識(shí)別功能示例
- Python3用tkinter和PIL實(shí)現(xiàn)看圖工具
- Python切片工具pillow用法示例
- python使用pil庫實(shí)現(xiàn)圖片合成實(shí)例代碼
- Python 調(diào)用PIL庫失敗的解決方法
相關(guān)文章
python開發(fā)之thread線程基礎(chǔ)實(shí)例入門
這篇文章主要介紹了python開發(fā)之thread線程基礎(chǔ),以三個(gè)實(shí)例形式分析了Python中thread線程的基本使用方法,涉及串行與并行程序的執(zhí)行原理及線程的操作技巧,需要的朋友可以參考下2015-11-11
Python開發(fā)之身份證驗(yàn)證庫id_validator驗(yàn)證身份證號(hào)合法性及根據(jù)身份證號(hào)返回住址年齡等信息
這篇文章主要介紹了Python開發(fā)之身份證驗(yàn)證庫id_validator驗(yàn)證身份證號(hào)合法性及Python解析身份證號(hào),根據(jù)身份證號(hào)返回住址年齡等信息,需要的朋友可以參考下2020-03-03
Python使用selenium實(shí)現(xiàn)網(wǎng)頁用戶名 密碼 驗(yàn)證碼自動(dòng)登錄功能
這篇文章主要介紹了Python使用selenium實(shí)現(xiàn)網(wǎng)頁用戶名 密碼 驗(yàn)證碼自動(dòng)登錄功能,實(shí)現(xiàn)思路很簡單,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05
掌握python polars庫進(jìn)行高效高速的數(shù)據(jù)處理。
這篇文章主要介紹了python polars庫進(jìn)行高效高速的數(shù)據(jù)處理技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
python numpy linspace函數(shù)使用詳解
本文介紹了Python Numpy庫中的linspace函數(shù),該函數(shù)用于生成均勻分布的數(shù)值序列,通過示例和詳細(xì)參數(shù)解釋,幫助讀者理解如何使用linspace函數(shù),最后,對(duì)比了linspace和arange函數(shù)之間的主要差異,感興趣的朋友跟隨小編一起看看吧2024-12-12
Python Numpy運(yùn)行報(bào)錯(cuò):IndexError: too many in
在使用Numpy進(jìn)行數(shù)組操作時(shí),經(jīng)常會(huì)遇到各種錯(cuò)誤,其中,IndexError: too many indices for array是一種常見的錯(cuò)誤,它通常發(fā)生在嘗試使用一個(gè)過多維度的索引來訪問一個(gè)較低維度的數(shù)組時(shí),本文介紹了Python Numpy報(bào)錯(cuò)的解決辦法,需要的朋友可以參考下2024-07-07

