Python實(shí)現(xiàn)圖片格式轉(zhuǎn)換小程序
基于Python實(shí)現(xiàn)圖片格式轉(zhuǎn)換的小程序,供大家參考,具體內(nèi)容如下
特點(diǎn):
1.批量處理圖片
2.轉(zhuǎn)換常見的4種圖片格式
運(yùn)行窗口
運(yùn)行窗口-1
選擇圖片(可批量選擇)-2
假設(shè)選中4張JEPG格式的圖片
格式選擇窗口-3
假設(shè)選擇目標(biāo)格式PNG
結(jié)束窗口-4
結(jié)果展示-5
可以發(fā)現(xiàn)4個JEPG目標(biāo)圖片成功轉(zhuǎn)換為PNG格式的圖片
代碼
import tkinter as tk import tkinter.messagebox from tkinter import filedialog from PIL import Image def main(): ?? ? ? window1 = tk.Tk() ? ? window1.title('') ? ? window1.geometry('200x100') ? ? l1 = tk.Label(window1, bg = 'green', font = ('宋體', 12), width = 50, text = '圖片轉(zhuǎn)換精靈(v1.3)') ? ? l1.pack() ? ? def select_image(): ? ? ? ? image = tk.filedialog.askopenfilenames(title = '選擇圖片') ? ? ? ? num = len(image) ? ? ? ? types = ['.jpg', '.png', '.tif', '.gif'] ? ? ? ? image_list = list(image) ? ? ? ?? ? ? ? ? window2 = tk.Tk() ? ? ? ? window2.title('') ? ? ? ? window2.geometry('200x250') ? ? ? ?? ? ? ? ? l2_1 = tk.Label(window2, bg = 'green', font = ('宋體', 12), width = 50, text = '圖片轉(zhuǎn)換精靈(v1.3)') ? ? ? ? l2_1.pack() ? ? ? ? l2_2 = tk.Label(window2, text = '') ? ? ? ? l2_2.pack() ? ? ? ?? ? ? ? ? l2_3 = tk.Label(window2, font = ('宋體', 12), width = 50, text = '') ? ? ? ? l2_3.pack() ? ? ? ? l2_3.config(text = '已選擇%d張圖片' % num) ? ? ? ?? ? ? ? ? l2_4 = tk.Label(window2, font = ('宋體', 12), width = 50, text = '目標(biāo)格式【點(diǎn)擊即開始】') ? ? ? ? l2_4.pack() ? ? ? ? l2_5 = tk.Label(window2, text = '') ? ? ? ? l2_5.pack() ? ? ? ? def jpg_type(): ? ? ? ? ? ? image_type = types[0] ? ? ? ? ? ? for img in image_list: ? ? ? ? ? ? ? ? f = Image.open(img) ? ? ? ? ? ? ? ? img_name = img[:-4] ? ? ? ? ? ? ? ? try: ? ? ? ? ? ? ? ? ? ? f.save(img_name + image_type) ? ? ? ? ? ? ? ? except OSError: ? ? ? ? ? ? ? ? ? ? tkinter.messagebox.showerror(title='', message='%s轉(zhuǎn)換出錯' % img) ? ? ? ? ? ? tkinter.messagebox.showinfo(title='', message='轉(zhuǎn)換完成') ? ? ?? ? ? ? ? def png_type(): ? ? ? ? ? ? image_type = types[1] ? ? ? ? ? ? for img in image_list: ? ? ? ? ? ? ? ? f = Image.open(img) ? ? ? ? ? ? ? ? img_name = img[:-4] ? ? ? ? ? ? ? ? try: ? ? ? ? ? ? ? ? ? ? f.save(img_name + image_type) ? ? ? ? ? ? ? ? except OSError: ? ? ? ? ? ? ? ? ? ? tkinter.messagebox.showerror(title='', message='%s轉(zhuǎn)換出錯' % img) ? ? ? ? ? ? tkinter.messagebox.showinfo(title='', message='轉(zhuǎn)換完成') ? ? ?? ? ? ? ? def tif_type(): ? ? ? ? ? ? image_type = types[2] ? ? ? ? ? ? for img in image_list: ? ? ? ? ? ? ? ? f = Image.open(img) ? ? ? ? ? ? ? ? img_name = img[:-4] ? ? ? ? ? ? ? ? try: ? ? ? ? ? ? ? ? ? ? f.save(img_name + image_type) ? ? ? ? ? ? ? ? except OSError: ? ? ? ? ? ? ? ? ? ? tkinter.messagebox.showerror(title='', message='%s轉(zhuǎn)換出錯' % img) ? ? ? ? ? ? ? ?? ? ? ? ? ? ? tkinter.messagebox.showinfo(title='', message='轉(zhuǎn)換完成') ? ? ?? ? ? ? ? def gif_type(): ? ? ? ? ? ? image_type = types[3] ? ? ? ? ? ? for img in image_list: ? ? ? ? ? ? ? ? f = Image.open(img) ? ? ? ? ? ? ? ? img_name = img[:-4] ? ? ? ? ? ? ? ? try: ? ? ? ? ? ? ? ? ? ? f.save(img_name + image_type) ? ? ? ? ? ? ? ? except OSError: ? ? ? ? ? ? ? ? ? ? tkinter.messagebox.showerror(title='', message='%s轉(zhuǎn)換出錯' % img) ? ? ? ? ? ? tkinter.messagebox.showinfo(title='', message='轉(zhuǎn)換完成') ? ? ?? ? ? ? ? button2_1 = tk.Button(window2, text = 'JEPG', font = ('宋體', 12), width = 8, height = 1, command = jpg_type) ? ? ? ? button2_1.pack() ? ? ? ? button2_2 = tk.Button(window2, text = 'PNG', font = ('宋體', 12), width = 8, height = 1, command = png_type) ? ? ? ? button2_2.pack() ? ? ? ? button2_3 = tk.Button(window2, text = 'TIF', font = ('宋體', 12), width = 8, height = 1, command = tif_type) ? ? ? ? button2_3.pack() ? ? ? ? button2_4 = tk.Button(window2, text = 'GIF', font = ('宋體', 12), width = 8, height = 1, command = gif_type) ? ? ? ? button2_4.pack() ? ? ? ?? ? ? ? ? window2.mainloop() ? ? ? ? ? ? ? ? ? ? botton1 = tk.Button(window1, text = '選擇圖片', font = ('宋體', 12), width = 8, height = 1, command = select_image) ? ? botton1.place(x = 65, y = 40) ? ? window1.mainloop() if __name__ == '__main__': ? ? main()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python?Pandas庫read_excel()參數(shù)實(shí)例詳解
人們經(jīng)常用pandas處理表格型數(shù)據(jù),時常需要讀入excel表格數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于python?Pandas庫read_excel()參數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Python Tornado 實(shí)現(xiàn)SSE服務(wù)端主動推送方案
SSE是Server-Sent Events 的簡稱,是一種服務(wù)器端到客戶端(瀏覽器)的單項(xiàng)消息推送,本文主要探索兩個方面的實(shí)踐一個是客戶端發(fā)送請求,服務(wù)端的返回是分多次進(jìn)行傳輸?shù)?直到傳輸完成,這種情況下請求結(jié)束后,考慮關(guān)閉SSE,所以這種連接可以認(rèn)為是暫時的,感興趣的朋友一起看看吧2024-01-01Python 使用 attrs 和 cattrs 實(shí)現(xiàn)面向?qū)ο缶幊痰膶?shí)踐
這篇文章主要介紹了Python 使用 attrs 和 cattrs 實(shí)現(xiàn)面向?qū)ο缶幊痰膶?shí)踐,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06python統(tǒng)計(jì)字母、空格、數(shù)字等字符個數(shù)的實(shí)例
今天小編就為大家分享一篇python統(tǒng)計(jì)字母、空格、數(shù)字等字符個數(shù)的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Pytorch?nn.Unfold()?與?nn.Fold()圖碼詳解(最新推薦)
這篇文章主要介紹了Pytorch?nn.Unfold()?與?nn.Fold()圖碼詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05