由于工作需要,老是要拆分一些砖图,特意做了这个小工具自用,分享给大家试一下。作用就是把一张图片按设定的行列数平均分割图片。批量拆分的,把要分割的图片放同一个文件夹里就行了
下载链接放123盘了,蓝揍云那个客端登不上,有的朋友麻烦转一个。
PS:火绒查毒无问题,python打包的,360安全卫士可能会报毒,就算代码里只有一个Hello world!提示框,它也报毒,无语了,可么各位试一下用PY2EXE重新打包试一下吧,不想整了,自行选择。顺便吐槽一下,360越来越垃圾了,一不小心就给你来一份全家桶套餐,火绒比它强多了,仅代表个人意见,不喜勿喷。
V2.1更新
1.加入了图片无缝拼接功能,UI从英文改成中文,加入清空图片功能,增加可处理图片的像素上限为28亿。
2.去掉选择文件夹改为直接选择图片即可。
3.加入切割完图片后的切割完成提示。

【RJ0019】图片拼接与切割V2.1

【RJ0019】图片拼接与切割V2.1

【RJ0019】图片拼接与切割V2.1

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()

【RJ0019】图片拼接与切割V2.1