欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 實(shí)現(xiàn)圖片裁剪小工具

 更新時(shí)間:2021年02月02日 17:33:44   作者:南風(fēng)丶輕語(yǔ)  
這篇文章主要介紹了python 實(shí)現(xiàn)圖片裁剪小工具的示例,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下

完整項(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)左鍵并且移動(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

以上就是python 實(shí)現(xiàn)圖片裁剪小工具的詳細(xì)內(nèi)容,更多關(guān)于python 圖片裁剪的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • TensorFlow實(shí)現(xiàn)簡(jiǎn)單線性回歸

    TensorFlow實(shí)現(xiàn)簡(jiǎn)單線性回歸

    這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)簡(jiǎn)單線性回歸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 詳解Python如何利用pymysql封裝項(xiàng)目通用的連接和查詢

    詳解Python如何利用pymysql封裝項(xiàng)目通用的連接和查詢

    一個(gè)項(xiàng)目通常都需要有數(shù)據(jù)庫(kù),本文就來(lái)為大家詳細(xì)講講Python如何利用pymysql簡(jiǎn)單分裝一個(gè)通用的連接,關(guān)閉和查詢,需要的可以參考一下
    2022-07-07
  • Python中subplots_adjust函數(shù)的用法

    Python中subplots_adjust函數(shù)的用法

    這篇文章主要介紹了Python中subplots_adjust函數(shù)的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python進(jìn)程間通信 multiProcessing Queue隊(duì)列實(shí)現(xiàn)詳解

    Python進(jìn)程間通信 multiProcessing Queue隊(duì)列實(shí)現(xiàn)詳解

    這篇文章主要介紹了python進(jìn)程間通信 mulitiProcessing Queue隊(duì)列實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 使用Python將Exception異常錯(cuò)誤堆棧信息寫入日志文件

    使用Python將Exception異常錯(cuò)誤堆棧信息寫入日志文件

    這篇文章主要介紹了使用Python將Exception異常錯(cuò)誤堆棧信息寫入日志文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python中的字典合并與列表合并技巧

    Python中的字典合并與列表合并技巧

    這篇文章主要介紹了Python中的字典合并與列表合并技巧,下文圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一的的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • Python實(shí)現(xiàn)批量修改Word文檔中圖片大小并居中對(duì)齊

    Python實(shí)現(xiàn)批量修改Word文檔中圖片大小并居中對(duì)齊

    這篇文章主要介紹了如何利用Python實(shí)現(xiàn)批量修改Word文檔中圖片大小并居中對(duì)齊,文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • Python爬蟲實(shí)戰(zhàn)之批量下載快手平臺(tái)視頻數(shù)據(jù)

    Python爬蟲實(shí)戰(zhàn)之批量下載快手平臺(tái)視頻數(shù)據(jù)

    讀萬(wàn)卷書不如行萬(wàn)里路,學(xué)的扎不扎實(shí)要通過(guò)實(shí)戰(zhàn)才能看出來(lái),本篇文章手把手帶你批量下載快手平臺(tái)視頻數(shù)據(jù),大家可以在過(guò)程中查缺補(bǔ)漏,看看自己掌握程度怎么樣
    2021-10-10
  • matplotlib繪制甘特圖的萬(wàn)能模板案例

    matplotlib繪制甘特圖的萬(wàn)能模板案例

    matplotlib是常見的繪圖庫(kù),本文主要介紹了matplotlib繪制甘特圖的萬(wàn)能模板案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Python中字典創(chuàng)建、遍歷、添加等實(shí)用操作技巧合集

    Python中字典創(chuàng)建、遍歷、添加等實(shí)用操作技巧合集

    這篇文章主要介紹了Python中字典創(chuàng)建、遍歷、添加等實(shí)用操作技巧合集,本文講解了字典中常見方法列表、創(chuàng)建字典的五種方法、字典中鍵值遍歷方法等內(nèi)容,需要的朋友可以參考下
    2015-06-06

最新評(píng)論