Python实现图片格式转换小程序
短信预约 -IT技能 免费直播动态提醒
基于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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341