亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python基于tkinter canvas實現圖片裁剪功能

瀏覽:21日期:2022-07-06 13:19:36

實現: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基于tkinter canvas實現圖片裁剪功能

運行

Python基于tkinter canvas實現圖片裁剪功能

Python基于tkinter canvas實現圖片裁剪功能

one_corp.png

Python基于tkinter canvas實現圖片裁剪功能

源碼(https://github.com/rainbow-tan/rainbow/tree/master/%E8%A3%81%E5%89%AA%E5%9B%BE%E7%89%87)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 男人午夜影院 | 不卡福利视频 | 成人在线观看国产 | 黄色录像视频网站 | 国产一级爱c片免费播放 | 日韩成人影院 | 屁屁网站在线观看www | 久久精品无码一区二区三区 | 国产成人av性色在线影院 | 在线免费黄色网址 | 小泽玛利亚在线精品一区二区 | 男人看片资源 | 色婷婷久久综合中文久久一本 | 色综合久久丁香婷婷 | 亚洲精品永久一区 | 一级做a爱片特黄在线观看 一级做a爱片特黄在线观看免费看 | 日本特级aⅴ一级毛片 | 国产日韩一区二区三区在线播放 | 日本黄色片一级片 | aaa成人永久在线观看视频 | 国产91福利在线精品剧情尤物 | 成年视频xxxxxx在线 | 国产 麻豆 欧美亚洲综合久久 | 全黄性色大片 | 欧美亚洲一区二区三区 | 国产精品臀控福利在线观看 | 久久国产这里只有精品 | 一级特一级特色生活片 | 国产三级自拍视频 | 日韩中文字幕免费观看 | 中文字幕成人乱码在线电影 | 国产成人综合亚洲欧美在 | 成人毛片免费观看视频 | 国产 日韩 在线 亚洲 字幕 中文 | 国内成人精品亚洲日本语音 | 91视频免费网址 | 国产成人精品综合久久久软件 | 色婷婷六月丁香在线观看 | 免费在线看黄色片 | 亚洲一级毛片在线观 | 国产在线永久视频 |