[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from PIL import Image, ImageTk, ImageFile
# PIL库设置
ImageFile.LOAD_TRUNCATED_IMAGES = True
Image.MAX_IMAGE_PIXELS = 2800000000
class ImageStitcherApp:
def __init__(self, parent):
self.parent = parent
self.image_paths = []
self.rows = tk.IntVar()
self.cols = tk.IntVar()
self.create_widgets()
def create_widgets(self):
# 图片显示的框架
self.image_frame = tk.Frame(self.parent)
self.image_frame.pack(pady=10, fill='both', expand=True)
# 添加图片的按钮
self.add_button = tk.Button(self.parent, text="输入图片", command=self.add_images)
self.add_button.pack(pady=5)
# 清空图片的按钮
self.clear_button = tk.Button(self.parent, text="清空图片", command=self.clear_images)
self.clear_button.pack(pady=5)
# 行数输入框
self.row_label = tk.Label(self.parent, text="输入拼接行数:")
self.row_label.pack()
self.row_entry = tk.Entry(self.parent, textvariable=self.rows)
self.row_entry.pack()
# 列数输入框
self.col_label = tk.Label(self.parent, text="输入拼接列数:")
self.col_label.pack()
self.col_entry = tk.Entry(self.parent, textvariable=self.cols)
self.col_entry.pack()
# 拼接图片的按钮
self.stitch_button = tk.Button(self.parent, text="保存图片", command=self.stitch_images)
self.stitch_button.pack(pady=10)
def add_images(self):
paths = filedialog.askopenfilenames(filetypes=[("Image Files", "*.jpg;*.jpeg;*.png")])
if paths:
self.image_paths.extend(paths)
self.show_images()
def clear_images(self):
self.image_paths.clear()
for widget in self.image_frame.winfo_children():
widget.destroy()
def show_images(self):
for widget in self.image_frame.winfo_children():
widget.destroy()
# 显示图片
for path in self.image_paths:
img = Image.open(path)
img.thumbnail((150, 150)) # 调整图片大小
img_tk = ImageTk.PhotoImage(img)
label = tk.Label(self.image_frame, image=img_tk)
label.image = img_tk # 保持图片引用
label.pack(side=tk.LEFT, padx=5)
def stitch_images(self):
try:
rows = self.rows.get()
cols = self.cols.get()
if rows <= 0 or cols <= 0:
messagebox.showerror("Error", "行数和列数必须大于零。")
return
# 打开所有图片
images = [Image.open(path) for path in self.image_paths]
# 假设所有图片具有相同的尺寸
width, height = images[0].size
# 创建一个新的图片用于拼接
result_width = width * cols
result_height = height * rows
result_image = Image.new('RGB', (result_width, result_height))
# 拼接图片
for row in range(rows):
for col in range(cols):
index = row * cols + col
if index < len(images):
result_image.paste(images[index], (col * width, row * height))
# 保存拼接后的图片
output_filename = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg")])
if output_filename:
result_image.save(output_filename)
messagebox.showinfo("Success", "图片拼接完成!")
except Exception as e:
messagebox.showerror("Error", f"发生错误: {str(e)}")
finally:
# 关闭所有图片
for image in images:
image.close()
def split_image(file_path, rows, cols):
img = Image.open(file_path)
width, height = img.size
cell_width = width // cols
cell_height = height // rows
count = 1
for i in range(rows):
for j in range(cols):
box = (j * cell_width, i * cell_height, (j + 1) * cell_width, (i + 1) * cell_height)
cell = img.crop(box)
cell.save(file_path.replace('.jpg', f'_{count}.jpg'))
count += 1
messagebox.showinfo("Success", "图片切割完成!")
def select_images():
file_path = filedialog.askopenfilenames(title='选择图片文件', filetypes=[('JPG files', '*.jpg')])
global entry_rows, entry_cols
for path in file_path:
split_image(path, int(entry_rows.get()), int(entry_cols.get()))
# 主窗口
root = tk.Tk()
root.title('图片处理 by.KOG丛林')
# 使用标签页分开不同的功能
notebook = ttk.Notebook(root)
notebook.pack(expand=1, fill='both')
# 图片拼接标签页
stitch_tab = ttk.Frame(notebook, padding="3 3 12 12")
notebook.add(stitch_tab, text='图片拼接')
# 图片分割标签页
split_tab = ttk.Frame(notebook, padding="3 3 12 12")
notebook.add(split_tab, text='图片分割')
# 在拼接标签页中添加拼接应用实例
app_stitcher = ImageStitcherApp(stitch_tab)
# 在分割标签页中添加分割应用UI
label_rows = tk.Label(split_tab, text='输入分割行数:')
label_rows.grid(row=0, column=0, sticky='e')
entry_rows = tk.Entry(split_tab)
entry_rows.grid(row=0, column=1)
label_cols = tk.Label(split_tab, text='输入分割列数:')
label_cols.grid(row=1, column=0, sticky='e')
entry_cols = tk.Entry(split_tab)
entry_cols.grid(row=1, column=1)
button_select = tk.Button(split_tab, text='选择图片并自动分割', command=select_images)
button_select.grid(row=2, column=0, columnspan=2)
# 主循环
root.mainloop()