Python基于tkinter canvas實(shí)現(xiàn)圖片裁剪功能
實(shí)現(xiàn):tkinter 畫(huà)布上顯示圖片,按下鼠標(biāo)左鍵并且移動(dòng),實(shí)現(xiàn)截圖
代碼如下
# -*- 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('鼠標(biāo)左鍵按下') 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('鼠標(biāo)左鍵釋放') 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('鼠標(biāo)左鍵按下并移動(dòng)') 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) # 刪除前一個(gè)矩形 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('鼠標(biāo)右鍵按下') pass def right_mouse_up(event): # print('鼠標(biāo)右鍵釋放') 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) # 鼠標(biāo)左鍵按下 canvas.bind('<ButtonRelease-1>', left_mouse_up) # 鼠標(biāo)左鍵釋放 canvas.bind('<Button-3>', right_mouse_down) # 鼠標(biāo)右鍵按下 canvas.bind('<ButtonRelease-3>', right_mouse_up) # 鼠標(biāo)右鍵釋放 canvas.bind('<B1-Motion>', moving_mouse) # 鼠標(biāo)左鍵按下并移動(dòng) win.mainloop()
原圖one.png
運(yùn)行
one_corp.png
源碼(https://github.com/rainbow-tan/rainbow/tree/master/%E8%A3%81%E5%89%AA%E5%9B%BE%E7%89%87)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于python的docx模塊處理word和WPS的docx格式文件方式
今天小編就為大家分享一篇基于python的docx模塊處理word和WPS的docx格式文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02詳解python的幾種標(biāo)準(zhǔn)輸出重定向方式
這篇文章是基于Python2.7版本,介紹常見(jiàn)的幾種標(biāo)準(zhǔn)輸出(stdout)重定向方式。顯然,這些方式也適用于標(biāo)準(zhǔn)錯(cuò)誤重定向。學(xué)習(xí)python的小伙伴們可以參考借鑒。2016-08-08python獲取本周、上周、本月、上月及本季的時(shí)間代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于python獲取本周、上周、本月、上月及本季的時(shí)間的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(zhuǎn)化
這篇文章主要介紹了tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(zhuǎn)化問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Python 使用SMTP發(fā)送郵件的代碼小結(jié)
python的smtplib提供了一種很方便的途徑發(fā)送電子郵件。它對(duì)smtp協(xié)議進(jìn)行了簡(jiǎn)單的封裝,需要的朋友可以參考下2016-09-09python密碼學(xué)簡(jiǎn)單替代密碼解密及測(cè)試教程
這篇文章主要介紹了python密碼學(xué)簡(jiǎn)單替代密碼解密及測(cè)試教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python?實(shí)現(xiàn)簡(jiǎn)單智能聊天機(jī)器人
這篇文章主要介紹了Python?實(shí)現(xiàn)簡(jiǎn)單智能聊天機(jī)器人,首先通過(guò)計(jì)算機(jī)接收用戶(hù)的語(yǔ)音輸入再將用戶(hù)輸入的語(yǔ)音輸入轉(zhuǎn)化為文本信息展開(kāi)實(shí)現(xiàn)過(guò)程,需要的小伙伴可以參考一下2022-05-05Python中函數(shù)參數(shù)調(diào)用方式分析
這篇文章主要介紹了Python中函數(shù)參數(shù)調(diào)用方式,結(jié)合實(shí)例形式分析了Python函數(shù)參數(shù)定義與使用的四種常見(jiàn)操作方法,需要的朋友可以參考下2018-08-08淺談Python數(shù)據(jù)類(lèi)型之間的轉(zhuǎn)換
下面小編就為大家?guī)?lái)一篇淺談Python數(shù)據(jù)類(lèi)型之間的轉(zhuǎn)換。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06