gpt4 book ai didi

python 实现图片裁剪小工具

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章python 实现图片裁剪小工具由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

完整项目地址下载:https://github.com/rainbow-tan/rainbow/tree/master/%E8%A3%81%E5%89%AA%E5%9B%BE%E7%89%87 。

实现:tkinter 画布上显示图片,按下鼠标左键并且移动,实现截图 。

# -*- encoding=utf-8 -*-import osimport tkinter as tkfrom PIL import Imagefrom PIL import ImageTkleft_mouse_down_x = 0left_mouse_down_y = 0left_mouse_up_x = 0left_mouse_up_y = 0sole_rectangle = Nonedef left_mouse_down(event):  # print("鼠标左键按下")  global left_mouse_down_x, left_mouse_down_y  left_mouse_down_x = event.x  left_mouse_down_y = event.ydef left_mouse_up(event):  # print("鼠标左键释放")  global left_mouse_up_x, left_mouse_up_y  left_mouse_up_x = event.x  left_mouse_up_y = event.y  corp_img(img_path, "img/one_corp.png", left_mouse_down_x, left_mouse_down_y,       left_mouse_up_x, left_mouse_up_y)def moving_mouse(event):  # print("鼠标左键按下并移动")  global sole_rectangle  global left_mouse_down_x, left_mouse_down_y  moving_mouse_x = event.x  moving_mouse_y = event.y  if sole_rectangle is not None:    canvas.delete(sole_rectangle) # 删除前一个矩形  sole_rectangle = canvas.create_rectangle(left_mouse_down_x, left_mouse_down_y, moving_mouse_x,                       moving_mouse_y, outline="red")def right_mouse_down(event):  # print("鼠标右键按下")  passdef right_mouse_up(event):  # print("鼠标右键释放")  passdef corp_img(source_path, save_path, x_begin, y_begin, x_end, y_end):  if x_begin < x_end:    min_x = x_begin    max_x = x_end  else:    min_x = x_end    max_x = x_begin  if y_begin < y_end:    min_y = y_begin    max_y = y_end  else:    min_y = y_end    max_y = y_begin  save_path = os.path.abspath(save_path)  if os.path.isfile(source_path):    corp_image = Image.open(source_path)    region = corp_image.crop((min_x, min_y, max_x, max_y))    region.save(save_path)    print("裁剪完成,保存于:{}".format(save_path))  else:    print("未找到文件:{}".format(source_path))if __name__ == "__main__":  pass  win = tk.Tk()  frame = tk.Frame()  frame.pack()  screenwidth = win.winfo_screenwidth()  screenheight = win.winfo_screenheight()  img_path = "img/one.png"  # img_path = "img/bg.jpg"  # img_path = "img/test.jpg"  # img_path = "img/pic.gif"  image = Image.open(img_path)  image_x, image_y = image.size  if image_x > screenwidth or image_y > screenheight:    print("The picture size is too big,max should in:{}x{}, your:{}x{}".format(screenwidth,                                          screenheight,                                          image_x,                                          image_y))  img = ImageTk.PhotoImage(image)  canvas = tk.Canvas(frame, width=image_x, height=image_y, bg="pink")  i = canvas.create_image(0, 0, anchor="nw", image=img)  canvas.pack()  canvas.bind("<Button-1>", left_mouse_down) # 鼠标左键按下  canvas.bind("<ButtonRelease-1>", left_mouse_up) # 鼠标左键释放  canvas.bind("<Button-3>", right_mouse_down) # 鼠标右键按下  canvas.bind("<ButtonRelease-3>", right_mouse_up) # 鼠标右键释放  canvas.bind("<B1-Motion>", moving_mouse) # 鼠标左键按下并移动  win.mainloop()

原图one.png 。

python 实现图片裁剪小工具

 运行 。

python 实现图片裁剪小工具

python 实现图片裁剪小工具

one_corp.png 。

python 实现图片裁剪小工具

以上就是python 实现图片裁剪小工具的详细内容,更多关于python 图片裁剪的资料请关注我其它相关文章! 。

原文链接:https://www.cnblogs.com/rainbow-tan/p/13898131.html 。

最后此篇关于python 实现图片裁剪小工具的文章就讲到这里了,如果你想了解更多关于python 实现图片裁剪小工具的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com