Python實(shí)現(xiàn)圖片格式轉(zhuǎn)換小程序
基于Python實(shí)現(xiàn)圖片格式轉(zhuǎn)換的小程序,供大家參考,具體內(nèi)容如下
特點(diǎn):
1.批量處理圖片
2.轉(zhuǎn)換常見(jiàn)的4種圖片格式
運(yùn)行窗口
運(yùn)行窗口-1
選擇圖片(可批量選擇)-2
假設(shè)選中4張JEPG格式的圖片
格式選擇窗口-3
假設(shè)選擇目標(biāo)格式PNG
結(jié)束窗口-4
結(jié)果展示-5
可以發(fā)現(xiàn)4個(gè)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)擊即開(kāi)始】') ? ? ? ? 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)換出錯(cuò)' % 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)換出錯(cuò)' % 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)換出錯(cuò)' % 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)換出錯(cuò)' % 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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python實(shí)現(xiàn)BT種子和磁力鏈接的相互轉(zhuǎn)換
這篇文章主要介紹了使用Python實(shí)現(xiàn)BT種子和磁力鏈接的相互轉(zhuǎn)換的方法,有時(shí)比如迅雷無(wú)法加載磁力鏈接或者無(wú)法上傳附件分享時(shí)可以用到,需要的朋友可以參考下2015-11-11python中subprocess實(shí)例用法及知識(shí)點(diǎn)詳解
在本篇文章里小編給大家分享的是關(guān)于python中subprocess實(shí)例用法及知識(shí)點(diǎn)詳解內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)下。2021-10-10Python3 執(zhí)行Linux Bash命令的方法
今天小編就為大家分享一篇Python3 執(zhí)行Linux Bash命令的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07詳解用python實(shí)現(xiàn)基本的學(xué)生管理系統(tǒng)(文件存儲(chǔ)版)(python3)
這篇文章主要介紹了python實(shí)現(xiàn)基本的學(xué)生管理系統(tǒng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04python實(shí)現(xiàn)拉普拉斯特征圖降維示例
今天小編就為大家分享一篇python實(shí)現(xiàn)拉普拉斯特征圖降維示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11pandas.DataFrame寫入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方式
這篇文章主要介紹了pandas.DataFrame寫入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08