有很多时候,我们都需要转化图片格式,所以我们来使用python制作一个转化工具
效果:
可以看到,支持转化为四种格式,png,jpg,jpeg,bmp
一.按照我们需要使用的模块
首先是使用pip安装一下(在cmd中运行)【仅对Win系统有效】
pip install pillow
pip install tkinter
如果安装错误可以试试一下的代码
pip2 install pillow
pip2 install tkinter
pip3 install pillow
pip3 install tkinter
二.写入一个自定义库
这也是一个库,由别人开发而成的,这里我们不讲解,直接搬运一下
import subprocess
import os
import platform
def desk():
if platform.system() == "Darwin":
return os.path.expanduser("~/Desktop/")
elif platform.system() == "Windows":
import winreg
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
return winreg.QueryValueEx(key, "Desktop")[0] + "/"
else:
return os.path.expanduser("~/Desktop/")
def xopen(file_name=None):
if file_name:
new_path = desk() + file_name
if not os.path.exists(new_path):
os.mkdir(new_path)
else:
new_path = os.getcwd()
if platform.system() == "Windows":
os.startfile(new_path)
elif platform.system() == "Darwin":
subprocess.Popen(["open", new_path])
else:
subprocess.Popen(["xdg-open", new_path])
return new_path + "/"
注意:使用这个代码需要拥有subprocess,os,platform库,一般高版本python会自带部分库
复制完后,我们在桌面上新建一个文档,原封不动的将代码复制进去,保存名称为xes_tool_plus.py【注意:这个文件和代码文件保持同一目录,如下图所示】
三.导入模块
导入我们之前安装的库,以及我们自定义的库
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from xes_tool_plus import *
from PIL import Image
四.设置变量
声明为全局变量,以防出现表里不一的bug
global image_path,image_name,image_path_text,v,root
我们在这里分别设置了image_path,image_name,image_path_text,v,root几个全局变量
五.tk展示UI部分
构建主体的TK UI展示部分
root = Tk()
root.attributes("-topmost",1)
root.title("图片格式转换器")
root.geometry("600x450+330+335")
l1=Label(root,text="图片:").pack()
image_path_text = StringVar()
image_path = Entry(root, textvariable=image_path_text, font=('FangSong', 10), width=30, state='readonly')
image_path.place(y=40,x=200)
button = Button(root,text="选择图片路径",command=select_file).place(y=75,x=260)
l2=Label(root,text="目标格式:").place(y=120,x=275)
format_list = [("png",0),("jpg",1),("jpeg",2),("bmp",3)]
v=IntVar()
pos_y = 145
for format_,num in format_list:
r = Radiobutton(root,text=format_,value=num,variable=v)
r.place(y=pos_y,x=275)
pos_y += 25
l3=Label(root,text="图片名称:").place(y=310,x=275)
image_name = Entry(root,width=50)
image_name.place(y=340,x=130)
button = Button(root,text="开始转换",command=image_to).place(y=370,x=270)
root.mainloop()
一个小小的TK按钮函数
def select_file():
path = filedialog.askopenfilename(title='请选择图片')
image_path_text.set(path)
六.PIL的转化图片函数
设置函数,选项,提示,以及PIL的转化图片库
def image_to():
format_dict = {0:".png",1:".jpg",2:".jpeg",3:".bmp"}
file = image_path.get()
file_name = image_name.get()
format_ = v.get()
img = Image.open(file)
img.thumbnail(img.size)
after_image_path = xopen("image") + file_name + format_dict[format_]
img.save(after_image_path)
messagebox.showinfo('成功','图片已保存至桌面')
七.完整代码
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from xes_tool_plus import *
from PIL import Image
global image_path,image_name,image_path_text,v,root
def image_to():
format_dict = {0:".png",1:".jpg",2:".jpeg",3:".bmp"}
file = image_path.get()
file_name = image_name.get()
format_ = v.get()
img = Image.open(file)
img.thumbnail(img.size)
after_image_path = xopen("image") + file_name + format_dict[format_]
img.save(after_image_path)
messagebox.showinfo('成功','图片已保存至桌面')
def select_file():
path = filedialog.askopenfilename(title='请选择图片')
image_path_text.set(path)
root = Tk()
root.attributes("-topmost",1)
root.title("图片格式转换器")
root.geometry("600x450+330+335")
l1=Label(root,text="图片:").pack()
image_path_text = StringVar()
image_path = Entry(root, textvariable=image_path_text, font=('FangSong', 10), width=30, state='readonly')
image_path.place(y=40,x=200)
button = Button(root,text="选择图片路径",command=select_file).place(y=75,x=260)
l2=Label(root,text="目标格式:").place(y=120,x=275)
format_list = [("png",0),("jpg",1),("jpeg",2),("bmp",3)]
v=IntVar()
pos_y = 145
for format_,num in format_list:
r = Radiobutton(root,text=format_,value=num,variable=v)
r.place(y=pos_y,x=275)
pos_y += 25
l3=Label(root,text="图片名称:").place(y=310,x=275)
image_name = Entry(root,width=50)
image_name.place(y=340,x=130)
button = Button(root,text="开始转换",command=image_to).place(y=370,x=270)
root.mainloop()
自定义库全部代码,具体安装方式看步骤二
import subprocess
import os
import platform
def desk():
if platform.system() == "Darwin":
return os.path.expanduser("~/Desktop/")
elif platform.system() == "Windows":
import winreg
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
return winreg.QueryValueEx(key, "Desktop")[0] + "/"
else:
return os.path.expanduser("~/Desktop/")
def xopen(file_name=None):
if file_name:
new_path = desk() + file_name
if not os.path.exists(new_path):
os.mkdir(new_path)
else:
new_path = os.getcwd()
if platform.system() == "Windows":
os.startfile(new_path)
elif platform.system() == "Darwin":
subprocess.Popen(["open", new_path])
else:
subprocess.Popen(["xdg-open", new_path])
return new_path + "/"
八.效果展示【需要完整代码请看步骤七】
(觉得不清晰可以搜索【文章【python实现图片转化技术】的内嵌插入视频】已高清播放示范)
ps:这里只展示动图