python 實(shí)現(xiàn)圖片裁剪小工具
完整項(xiàng)目地址下載:https://github.com/rainbow-tan/rainbow/tree/master/%E8%A3%81%E5%89%AA%E5%9B%BE%E7%89%87
實(shí)現(xiàn):tkinter 畫布上顯示圖片,按下鼠標(biāo)左鍵并且移動,實(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)左鍵按下并移動')
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)左鍵按下并移動
win.mainloop()
原圖one.png

運(yùn)行


one_corp.png

以上就是python 實(shí)現(xiàn)圖片裁剪小工具的詳細(xì)內(nèi)容,更多關(guān)于python 圖片裁剪的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python GUI庫圖形界面開發(fā)之PyQt5工具欄控件QToolBar的詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5工具欄控件QToolBar的詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02
python數(shù)據(jù)結(jié)構(gòu)樹和二叉樹簡介
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)樹和二叉樹簡介,需要的朋友可以參考下2014-04-04
五個(gè)Jupyter?Notebook實(shí)用魔法命令分享
Jupyter?Notebook是一個(gè)開源的交互式編程環(huán)境,用于創(chuàng)建和共享包含實(shí)時(shí)代碼、文本等,本文主要來和大家分享一些有趣的Jupyter?Notebook魔法命令,需要的可以參考一下2023-07-07
Python+Selenium實(shí)現(xiàn)表單自動填充和提交
你是不是也厭倦了每天重復(fù)表單填寫的工作,是時(shí)候讓技術(shù)來幫助我們解放雙手了,下面小編就為大家介紹一下如何使用Selenium和Python來自動填充和提交表單2023-09-09
如何爬取通過ajax加載數(shù)據(jù)的網(wǎng)站
這篇文章主要介紹了如何爬取通過ajax加載數(shù)據(jù)的網(wǎng)站,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
python re模塊findall()函數(shù)實(shí)例解析
這篇文章主要介紹了python re模塊findall()函數(shù)實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

