需求:同事发来一个图片,需要印刷出来给学生做练习。图片是按照PDF格式文件导出来的,每个页面都是A4大小,按图片高度平均分割即可。如果手动分割特别麻烦。求助豆包。
提问:使用python语言生成一个图片分割工具。要求按图片高度平均分割,可以手动输入要分割的图片个数。
回答:以下是一个使用 Python 语言实现的图片分割工具,它可以按图片高度平均分割图片,并且允许用户手动输入要分割的图片个数。这里我们使用Pillow库来处理图片。
from PIL import Image
def split_image(image_path, num_parts):
# 打开图片
image = Image.open(image_path)
width, height = image.size
# 计算每个分割部分的高度
part_height = height // num_parts
# 分割图片
for i in range(num_parts):
# 计算当前分割部分的起始和结束高度
top = i * part_height
bottom = (i + 1) * part_height if i < num_parts - 1 else height
# 裁剪图片
cropped_image = image.crop((0, top, width, bottom))
# 保存分割后的图片
output_path = f"part_{i + 1}.png"
cropped_image.save(output_path)
print(f"分割后的图片 {output_path} 已保存。")
if __name__ == "__main__":
# 获取用户输入的图片路径
image_path = input("请输入要分割的图片的路径: ")
try:
# 获取用户输入的分割图片个数
num_parts = int(input("请输入要分割的图片个数: "))
if num_parts <= 0:
print("分割个数必须是正整数。")
else:
split_image(image_path, num_parts)
except ValueError:
print("输入无效,请输入一个有效的整数。")
运行如下图:

程序默认为命令行界面,升级为图形界面。
提问:升级程序:升级为图形界面,有打开图片按钮、选择保存路径按钮,能显示分割后的图片文件名列表。
回答:以下是一个使用 Python 的tkinter库创建图形用户界面(GUI)的图片分割工具,它包含打开图片按钮、选择保存路径按钮,并且能显示分割后的图片文件名列表。
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image
class ImageSplitterGUI:
def __init__(self, root):
self.root = root
self.root.title("图片分割工具")
# 初始化变量
self.image_path = ""
self.save_path = ""
# 创建控件
self.open_button = tk.Button(root, text="打开图片", command=self.open_image)
self.open_button.pack(pady=10)
self.save_path_button = tk.Button(root, text="选择保存路径", command=self.select_save_path)
self.save_path_button.pack(pady=10)
self.num_parts_label = tk.Label(root, text="输入要分割的图片个数:")
self.num_parts_label.pack(pady=5)
self.num_parts_entry = tk.Entry(root)
self.num_parts_entry.pack(pady=5)
self.split_button = tk.Button(root, text="分割图片", command=self.split_image)
self.split_button.pack(pady=20)
self.file_listbox = tk.Listbox(root, width=50)
self.file_listbox.pack(pady=10)
def open_image(self):
self.image_path = filedialog.askopenfilename(filetypes=[("图片文件", "*.png;*.jpg;*.jpeg")])
if self.image_path:
messagebox.showinfo("提示", f"已选择图片: {self.image_path}")
def select_save_path(self):
self.save_path = filedialog.askdirectory()
if self.save_path:
messagebox.showinfo("提示", f"已选择保存路径: {self.save_path}")
def split_image(self):
if not self.image_path:
messagebox.showerror("错误", "请先选择要分割的图片。")
return
if not self.save_path:
messagebox.showerror("错误", "请先选择保存路径。")
return
try:
num_parts = int(self.num_parts_entry.get())
if num_parts <= 0:
messagebox.showerror("错误", "分割个数必须是正整数。")
return
except ValueError:
messagebox.showerror("错误", "输入无效,请输入一个有效的整数。")
return
try:
# 打开图片
image = Image.open(self.image_path)
width, height = image.size
# 计算每个分割部分的高度
part_height = height // num_parts
# 清空列表框
self.file_listbox.delete(0, tk.END)
# 分割图片
for i in range(num_parts):
# 计算当前分割部分的起始和结束高度
top = i * part_height
bottom = (i + 1) * part_height if i < num_parts - 1 else height
# 裁剪图片
cropped_image = image.crop((0, top, width, bottom))
# 保存分割后的图片
output_name = f"part_{i + 1}.png"
output_path = f"{self.save_path}/{output_name}"
cropped_image.save(output_path)
# 将文件名添加到列表框中
self.file_listbox.insert(tk.END, output_name)
messagebox.showinfo("提示", "图片分割完成。")
except Exception as e:
messagebox.showerror("错误", f"分割图片时出现错误: {str(e)}")
if __name__ == "__main__":
root = tk.Tk()
app = ImageSplitterGUI(root)
root.mainloop()
程序运行如下图:
