校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > python 實現(xiàn)圖片裁剪小工具

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

熱門標簽:杭州房產(chǎn)地圖標注 甘肅高頻外呼系統(tǒng) 江門智能電話機器人 滴滴地圖標注公司 地圖標注可以遠程操作嗎 如何申請400電話代理 智能電話機器人調(diào)研 天津塘沽區(qū)地圖標注 400電話在線如何申請

完整項目地址下載:https://github.com/rainbow-tan/rainbow/tree/master/%E8%A3%81%E5%89%AA%E5%9B%BE%E7%89%87

實現(xiàn):tkinter 畫布上顯示圖片,按下鼠標左鍵并且移動,實現(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('鼠標左鍵按下')
  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

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

您可能感興趣的文章:
  • 詳解Python+opencv裁剪/截取圖片的幾種方式
  • Python基于tkinter canvas實現(xiàn)圖片裁剪功能
  • Python OpenCV實現(xiàn)裁剪并保存圖片
  • Python 讀取xml數(shù)據(jù),cv2裁剪圖片實例
  • python通過opencv實現(xiàn)圖片裁剪原理解析
  • Python實現(xiàn)圖片裁剪的兩種方式(Pillow和OpenCV)
  • python openvc 裁剪、剪切圖片 提取圖片的行和列
  • python PIL和CV對 圖片的讀取,顯示,裁剪,保存實現(xiàn)方法
  • python實現(xiàn)對圖片進行旋轉(zhuǎn),放縮,裁剪的功能
  • Python圖片處理之圖片裁剪教程

標簽:重慶 德宏 東莞 廊坊 漢中 長春 河池 臨汾

巨人網(wǎng)絡通訊聲明:本文標題《python 實現(xiàn)圖片裁剪小工具》,本文關(guān)鍵詞  python,實現(xiàn),圖片,裁剪,小,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python 實現(xiàn)圖片裁剪小工具》相關(guān)的同類信息!
  • 本頁收集關(guān)于python 實現(xiàn)圖片裁剪小工具的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 石泉县| 印江| 尚志市| 文山县| 敖汉旗| 枣强县| 许昌县| 托克逊县| 天峨县| 柳林县| 淮南市| 天等县| 桂阳县| 东方市| 濮阳市| 西乌珠穆沁旗| 西安市| 犍为县| 金阳县| 漳州市| 汤原县| 扎囊县| 长治市| 马龙县| 抚松县| 颍上县| 年辖:市辖区| 晋江市| 余庆县| 元江| 宽城| 道真| 大宁县| 徐州市| 扶余县| 景宁| 巩义市| 金寨县| 鲁甸县| 莱芜市| 柯坪县|