吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5542|回复: 80
收起左侧

[Windows] 图片拼接与切割V2.1

  [复制链接]
william0712 发表于 2024-6-29 17:51
本帖最后由 william0712 于 2024-7-31 11:30 编辑

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

分割:

分割

分割


无缝拼图:

合并

合并


拼图与分割前后对比:

合并与分割

合并与分割


以下是完整代码:
[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()


免费评分

参与人数 26吾爱币 +22 热心值 +23 收起 理由
yxrswt + 1 + 1 我很赞同!
抱薪风雪雾 + 1 + 1 谢谢@Thanks!
gxybb + 1 + 1 我很赞同!
b19341 + 1 + 1 用心讨论,共获提升!
taitianna + 1 + 1 谢谢@Thanks!
caocao2020 + 1 热心回复!
zhongyihaitun + 1 谢谢@Thanks!
yanglinman + 1 谢谢@Thanks!
liubiguan + 1 + 1 我很赞同!
壹目居士 + 1 + 1 我很赞同!
wuaiwxh + 1 我很赞同!
luozi1653 + 1 + 1 热心回复!
小猪佩奇007 + 1 + 1 谢谢@Thanks!
黄金体验 + 1 + 1 我很赞同!
jj131028 + 1 我很赞同!
ysy2001 + 1 + 1 谢谢@Thanks!
mhtsqj + 1 + 1 谢谢@Thanks!
dogox + 1 + 1 我很赞同!
mmSmm + 1 我很赞同!
dj42898 + 1 + 1 热心回复!
深爱我的女孩 + 1 + 1 谢谢@Thanks!
wanfon + 1 + 1 热心回复!
supernox + 1 + 1 我很赞同!
Coolboy520 + 1 + 1 热心回复!
tblc + 1 我很赞同!
zk20120707 + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

逍遥牛 发表于 2024-6-29 18:33
蓝奏链下载:https://wwt.lanzoul.com/iUk1H231m70b 密码:52pj

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
william0712 + 1 + 1 我很赞同!

查看全部评分

zhufuan 发表于 2024-6-29 18:45
老师,这个有打包好的exe执行程序码,我没有py
hbezkh 发表于 2024-6-29 18:15
可以提供PYTHON源码吗?我初学PYTHON中,之前根据需要做了个横向或竖向1分为2的,您这个的适用场景可丰富多了。
逍遥牛 发表于 2024-6-29 18:35
本帖最后由 逍遥牛 于 2024-6-29 18:38 编辑

谢谢分享
wuyubaixiaoshen 发表于 2024-6-29 18:44
感谢分享
jun000ze 发表于 2024-6-29 18:44
全家桶套餐
Nekrosies 发表于 2024-6-29 19:05
PS里也自带批量分割的功能,可以在剪裁里找到切图,右键在对话框里设定切的份数,最后图片保存用“保存为web所用格式”导出,就能完成切图

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
tsyhome + 1 + 1 谢谢@Thanks!

查看全部评分

supernox 发表于 2024-6-29 19:44
感谢分享~
shicoco 发表于 2024-6-29 19:53
分割拼图好用不
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-10-5 23:48

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表