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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1765|回复: 33
收起左侧

[Python 原创] 图片裁剪器

  [复制链接]
wangzherongyao2 发表于 2024-3-10 20:35
本帖最后由 wangzherongyao2 于 2024-3-11 15:12 编辑

[Asm] 纯文本查看 复制代码
from PIL import Image, ImageTk
import tkinter as tk
import imageio

class ImageCropper:
    def __init__(self, image_path):
        self.image = Image.open(image_path)
        self.crop_coordinates = None
        image = Image.open(image_path)
        width, height = image.size
        self.root = tk.Tk()
        self.canvas_width = width  # 设置画布宽度
        self.canvas_height = height  # 设置画布高度
        self.canvas = tk.Canvas(self.root, width=self.canvas_width, height=self.canvas_height)
        self.canvas.pack()
        self.canvas.bind("<ButtonPress-1>", self.on_mouse_press)
        self.canvas.bind("<B1-Motion>", self.on_mouse_drag)
        self.canvas.bind("<ButtonRelease-1>", self.on_mouse_release)

        self.photo = ImageTk.PhotoImage(self.image.resize((self.canvas_width, self.canvas_height)))  # 调整图片大小以适应画布
        self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo)

        self.root.mainloop()

    def on_mouse_press(self, event):
        self.start_x = event.x
        self.start_y = event.y

    def on_mouse_drag(self, event):
        self.canvas.delete("crop_rectangle")
        self.current_x = event.x
        self.current_y = event.y
        self.canvas.create_rectangle(self.start_x, self.start_y, self.current_x, self.current_y, outline="red", tags="crop_rectangle")

    def on_mouse_release(self, event):
        self.canvas.delete("crop_rectangle")
        self.end_x = event.x
        self.end_y = event.y
        self.crop_coordinates = (min(self.start_x, self.end_x), min(self.start_y, self.end_y), max(self.start_x, self.end_x), max(self.start_y, self.end_y))
        self.root.quit()

    def open_image(self, root,path):
        # file_path = filedialog.askopenfilename ()
        open_image = imageio.imread(path)
        image = Image. fromarray (open_image)
        imgtk = ImageTk. PhotoImage (image=image)
        panel = tk.Label(root)
        panel.pack(padx=10,pady=10)
        panel.imgtk = imgtk
        panel.config (image=imgtk)
        root.update()
        # tkinter.messagebox.showinfo('图片地址: ',file_path)
# 图片路径
def 裁剪器(image_path = r"python\pic\2.png"):        
      # 替换为实际的图片路径
    # 创建ImageCropper实例并获取裁剪区域坐标
    cropper = ImageCropper(image_path)
    crop_coordinates = cropper.crop_coordinates
    # print("裁剪区域坐标:", crop_coordinates)
    image = Image.open(image_path)
    cropped_image = image.crop(crop_coordinates)
    cropped_image.save(r"python\pic\1.no_bg.png")

if __name__ == '__main__':
    裁剪器(image_path = r'python\pic\Demo.jpg')






重构后的代码和可执行文件:链接: https://pan.baidu.com/s/1NhA2e4qsMtEw0s2vykKg8A?pwd=8gkf 提取码: 8gkf

免费评分

参与人数 4吾爱币 +8 热心值 +4 收起 理由
dlyuan + 1 + 1 谢谢@Thanks!
ssbutianlie + 1 + 1 我很赞同!
blindcat + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

本帖被以下淘专辑推荐:

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

 楼主| wangzherongyao2 发表于 2024-3-11 17:53

已经打包成exe文件

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
wazct + 1 + 1 热心回复!

查看全部评分

wazct 发表于 2024-3-10 22:01
shubiao 发表于 2024-3-10 23:29
chen360781 发表于 2024-3-10 23:43
好东西啊
pbf0613 发表于 2024-3-11 00:13
求什么语言编的!
chenbingwen 发表于 2024-3-11 00:24
谢谢分享
zhangxiaoxiao 发表于 2024-3-11 06:16
这个有意思,感谢楼主分享
pengbaojun_2020 发表于 2024-3-11 07:09
看不懂  有没有程序呢
棉周 发表于 2024-3-11 07:14
好多这类型的软件了,但还是感谢&#128522;
blindcat 发表于 2024-3-11 07:25
跟着学习一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

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

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

GMT+8, 2024-4-29 02:26

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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