Python實現(xiàn)圖片格式轉(zhuǎn)換小程序
基于Python實現(xiàn)圖片格式轉(zhuǎn)換的小程序,供大家參考,具體內(nèi)容如下
特點:
1.批量處理圖片
2.轉(zhuǎn)換常見的4種圖片格式
運行窗口
運行窗口-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)格式【點擊即開始】')
? ? ? ? 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實現(xiàn)BT種子和磁力鏈接的相互轉(zhuǎn)換
這篇文章主要介紹了使用Python實現(xiàn)BT種子和磁力鏈接的相互轉(zhuǎn)換的方法,有時比如迅雷無法加載磁力鏈接或者無法上傳附件分享時可以用到,需要的朋友可以參考下2015-11-11
Python3 執(zhí)行Linux Bash命令的方法
今天小編就為大家分享一篇Python3 執(zhí)行Linux Bash命令的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
詳解用python實現(xiàn)基本的學(xué)生管理系統(tǒng)(文件存儲版)(python3)
這篇文章主要介紹了python實現(xiàn)基本的學(xué)生管理系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
pandas.DataFrame寫入數(shù)據(jù)庫的實現(xiàn)方式
這篇文章主要介紹了pandas.DataFrame寫入數(shù)據(jù)庫的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08

