全球快报:Python实现图片格式转换小程序
基于Python实现图片格式转换的小程序,供大家参考,具体内容如下
特点:
1.批量处理图片
2.转换常见的4种图片格式
运行窗口
运行窗口-1
(相关资料图)
选择图片(可批量选择)-2
假设选中4张JEPG格式的图片
格式选择窗口-3
假设选择目标格式PNG
结束窗口-4
结果展示-5
可以发现4个JEPG目标图片成功转换为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 = "图片转换精灵(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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
X 关闭
X 关闭
- 1现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 2如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 3AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 4转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 5充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 6好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 7名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?
- 8亚马逊云科技成立量子网络中心致力解决量子计算领域的挑战
- 9京东绿色建材线上平台上线 新增用户70%来自下沉市场
- 10网红淘品牌“七格格”chuu在北京又开一家店 潮人新宠chuu能红多久