脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现图片格式转换小程序

Python实现图片格式转换小程序

2022-08-10 09:13aguang5241 Python

这篇文章主要为大家详细介绍了Python实现图片格式转换小程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

基于Python实现图片格式转换的小程序,供大家参考,具体内容如下

特点:

1.批量处理图片
2.转换常见的4种图片格式

运行窗口

运行窗口-1

Python实现图片格式转换小程序

选择图片(可批量选择)-2

假设选中4张JEPG格式的图片

Python实现图片格式转换小程序

格式选择窗口-3

假设选择目标格式PNG

Python实现图片格式转换小程序

结束窗口-4

Python实现图片格式转换小程序

结果展示-5

可以发现4个JEPG目标图片成功转换为PNG格式的图片

Python实现图片格式转换小程序

代码

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 = "图片转换精灵(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 = "图片转换精灵(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 = "目标格式【点击即开始】")
        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转换出错" % img)

            tkinter.messagebox.showinfo(title="", message="转换完成")       

        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转换出错" % img)

            tkinter.messagebox.showinfo(title="", message="转换完成")       

        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转换出错" % img)
                
            tkinter.messagebox.showinfo(title="", message="转换完成")       

        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转换出错" % img)

            tkinter.messagebox.showinfo(title="", message="转换完成")       

        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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文地址:https://blog.csdn.net/aguang5241/article/details/98899992

延伸 · 阅读

精彩推荐