实现效果![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/1ca5a0a1df024e7ab499ad0f3114c9eb.png#pic_center)
介绍
本项目是一个基于Python实现的高级图片切割工具,使用Tkinter库构建用户界面,Pillow库处理图片。用户可以通过简单的操作选择图片、设置切割的行列数,程序会将图片切割成指定数量的碎片,并以PNG格式保存。
环境配置
在开始之前,请确保你的系统中已经安装了Python和以下库:
- Tkinter:Python的标准GUI库,通常随Python一起安装。
- Pillow:一个图像处理库,用于打开、处理和保存图片。
安装Pillow库可以使用pip:
pip install Pillow
项目分布
项目包含以下几个主要部分:
fill_image
函数:将图片填充为正方形。cut_image
函数:根据用户设置的行列数切割图片。save_images
函数:保存切割后的图片。select_image
函数:用户选择图片并显示预览。cut_and_save
函数:执行图片切割和保存的操作。- 主程序:创建Tkinter窗口和布局,绑定按钮事件。
详细代码
完整的代码如下:
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk, ImageDraw
import tkinter.filedialog
# 图片处理函数
def fill_image(image):
width, height = image.size
new_size = max(width, height)
new_image = Image.new(image.mode, (new_size, new_size), color='white')
if width > height:
new_image.paste(image, (0, int((new_size - height) / 2)))
else:
new_image.paste(image, (int((new_size - width) / 2), 0))
return new_image
def cut_image(image, rows, cols):
width, height = image.size
col_width = width // cols
row_height = height // rows
images = []
for i in range(rows):
for j in range(cols):
left = j * col_width
upper = i * row_height
right = (j + 1) * col_width
lower = (i + 1) * row_height
images.append(image.crop((left, upper, right, lower)))
return images
def save_images(image_list):
for index, image in enumerate(image_list):
image.save(f'piece_{index + 1}.png', 'PNG')
# UI更新函数
def select_image():
file_path = tk.filedialog.askopenfilename()
if file_path:
txt.set(file_path)
try:
image = Image.open(file_path)
image.thumbnail((310, 280))
photo = ImageTk.PhotoImage(image)
image_label.config(image=photo)
image_label.image = photo
except Exception as e:
tk.messagebox.showerror("Error", str(e))
def cut_and_save():
try:
file_path = txt.get()
if not file_path:
raise ValueError("No image selected")
image = Image.open(file_path)
image = fill_image(image)
images = cut_image(image, int(rows.get()), int(cols.get()))
save_images(images)
label1.config(text='切割成功!请在程序所在目录查看!')
except Exception as e:
tk.messagebox.showerror("Error", str(e))
# 设置窗口
main = tk.Tk()
main.geometry('500x500')
main.title('高级切图器')
main.iconbitmap('mr.ico')
# 图片选择和预览
txt = tk.StringVar()
txt_entry = ttk.Entry(main, width=55, textvariable=txt)
txt_entry.pack()
image_label = ttk.Label(main)
image_label.pack()
# 行列数设置
rows = tk.StringVar(value="3")
cols = tk.StringVar(value="3")
row_label = ttk.Label(main, text="行数:")
row_label.pack()
row_entry = ttk.Entry(main, width=5, textvariable=rows)
row_entry.pack()
col_label = ttk.Label(main, text="列数:")
col_label.pack()
col_entry = ttk.Entry(main, width=5, textvariable=cols)
col_entry.pack()
# 按钮和状态提示
select_button = ttk.Button(main, text='选择图片', command=select_image)
select_button.pack()
cut_button = ttk.Button(main, text='切割图片', command=cut_and_save)
cut_button.pack()
label1 = ttk.Label(main, text='')
label1.pack()
main.mainloop()
总结
本项目提供了一个简单但实用的图片切割工具,用户可以通过图形界面方便地操作。通过安装必要的Python库和运行代码,用户可以在自己的计算机上快速实现图片的切割和保存。这个项目可以作为图像处理入门的一个良好实践,也可以根据需要扩展更多的功能,如图片格式转换、滤镜应用等。