极乐门资源网 Design By www.ioogu.com
实现:tkinter 画布上显示图片,按下鼠标左键并且移动,实现截图
代码如下
# -*- encoding=utf-8 -*- import os import tkinter as tk from PIL import Image from PIL import ImageTk left_mouse_down_x = 0 left_mouse_down_y = 0 left_mouse_up_x = 0 left_mouse_up_y = 0 sole_rectangle = None def 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.y def 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('鼠标右键按下') pass def right_mouse_up(event): # print('鼠标右键释放') pass def 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
运行
one_corp.png
源码(https://github.com/rainbow-tan/rainbow/tree/master/%E8%A3%81%E5%89%AA%E5%9B%BE%E7%89%87)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
极乐门资源网 Design By www.ioogu.com
极乐门资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com
暂无Python基于tkinter canvas实现图片裁剪功能的评论...
更新日志
2024年11月15日
2024年11月15日
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]