吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2454|回复: 14
收起左侧

[Python 原创] python-图片切割程序_1.0

[复制链接]
TZ425 发表于 2023-10-31 11:52
本帖最后由 TZ425 于 2023-11-2 03:26 编辑

功能:
选择一张图片进行 N*N 分割
[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import filedialog
from PIL import Image
class App:
    def __init__(self, master):
        self.master = master
        master.title("图片切割程序")
        self.label1 = tk.Label(master, text="切割的图片:")
        self.label1.grid(row=0, column=0)
        self.entry1 = tk.Entry(master)
        self.entry1.grid(row=0, column=1)
        self.button1 = tk.Button(master, text="选 择 文 件", command=self.select_file)
        self.button1.grid(row=0, column=2)
        self.label2 = tk.Label(master, text="保存的路径:")
        self.label2.grid(row=1, column=0)
        self.entry2 = tk.Entry(master)
        self.entry2.grid(row=1, column=1)
        self.button2 = tk.Button(master, text="选择文件夹", command=self.select_folder)
        self.button2.grid(row=1, column=2)
        self.label3 = tk.Label(master, text="切割后生成横排的数量:")
        self.label3.grid(row=2, column=0)
        self.entry3 = tk.Entry(master)
        self.entry3.grid(row=2, column=1)
        self.label4 = tk.Label(master, text="切割后生成的纵列数量:")
        self.label4.grid(row=3, column=0)
        self.entry4 = tk.Entry(master)
        self.entry4.grid(row=3, column=1)
        self.button3 = tk.Button(master, text="开始切割", command=self.start_cutting)
        self.button3.grid(row=4, column=1)
        self.button4 = tk.Button(master, text="退出", command=master.quit)
        self.button4.grid(row=5, column=1)
        self.rows = 0
        self.columns = 0
    def select_file(self):
        file_path = filedialog.askopenfilename()
        self.entry1.delete(0, tk.END)
        self.entry1.insert(0, file_path)
    def select_folder(self):
        folder_path = filedialog.askdirectory()
        self.entry2.delete(0, tk.END)
        self.entry2.insert(0, folder_path)
    def start_cutting(self):
        file_path = self.entry1.get()
        folder_path = self.entry2.get()
        rows = int(self.entry3.get())
        columns = int(self.entry4.get())
        try:
            image = Image.open(file_path)
            width, height = image.size
            cell_width = width // columns
            cell_height = height // rows
            for i in range(rows):
                for j in range(columns):
                    left = j * cell_width
                    upper = i * cell_height
                    right = left + cell_width
                    lower = upper + cell_height
                    cropped_image = image.crop((left, upper, right, lower))
                    save_path = folder_path + f"/{i}_{j}.png"  # 保存为PNG格式,可根据需要修改
                    cropped_image.save(save_path)
            print("图片切割完成!")
        except Exception as e:
            print("发生错误:", e)
root = tk.Tk()
app = App(root)
root.mainloop()
切割成品.png
切割器5.png

免费评分

参与人数 2吾爱币 +8 热心值 +2 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
long8586 + 1 + 1 谢谢@Thanks!

查看全部评分

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

HumorsMrLin 发表于 2023-10-31 16:53
打包好了
https://coolboyin.lanzouj.com/imC8d1dc1t5c
密码:52pj
头像被屏蔽
rogabet 发表于 2023-11-1 23:16
苏紫方璇 发表于 2023-10-31 13:12
代码插入建议使用下边这个帖子的方法
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/thread-713042-1-1.html
(出处: 吾爱破解论坛)
long8586 发表于 2023-10-31 13:20
成品呢?
 楼主| TZ425 发表于 2023-10-31 14:34

成品太大了,附件放不进
long8586 发表于 2023-11-1 20:33
HumorsMrLin 发表于 2023-10-31 16:53
打包好了
https://coolboyin.lanzouj.com/imC8d1dc1t5c
密码:52pj

感谢!!!!
JokerDa 发表于 2023-11-2 11:18
感谢楼主的分享,学习了!!
qwe5333515 发表于 2023-12-13 11:01
可以批量切割嘛
 楼主| TZ425 发表于 2023-12-14 12:52

不行,一次只能一张
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2024-11-24 11:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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