Python图片处理之图片裁剪教程

作者:怪皮蛇皮怪 时间:2022-02-02 06:32:40 

一、操作流程

首先复制代码会吧?

1.有张照片

这是网上随便找的一张照片,自行保存测试

Python图片处理之图片裁剪教程

2.看看照片

运行代码,其中show_img函数是展示照片

3.选择角点

按照左上,右上,右下,左下的顺序选择四个角点

Python图片处理之图片裁剪教程

如果担心自己选不好,可以直接去除我代码里的points的注释,那是我自己用的原版

4.最终结果

Python图片处理之图片裁剪教程

二、代码分析

import 没什么好说的


#如果python没有安装cv2,那么就安装python-opencv就好
import cv2 as cv
import numpy as np

获取图片的长宽


#输入cv.imread后的图片,通过点击四个点选择要裁剪的部分
def get_window_size(src, bound=600):
   h,w = src.shape[0], src.shape[1]
   if h > w:
       h, w = bound, int(w*bound/h)
   else:
       h, w = int(h*bound/w), bound
   return (h, w)

通过鼠标获取图片的坐标点,顺序是左上,右上,右下,左下


class Indexer:
   def __init__(self, bound=4):
       self.id = 0
       self.bound = bound

def get_id(self):
       self.id = (self.id + 1)
       return (self.id)

def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
   if event == cv.EVENT_LBUTTONDOWN:
       img = param['src']
       win_name = param['window']
       indexer = param['indexer']
       points = param['points']

curr_id = indexer.get_id()
       points.append((x, y))
       print('第{}个顶点: ({},{})'.format(curr_id, x, y))

cv.circle(img, (x, y), 10, (0, 0, 255), thickness=2)
       cv.putText(
           img,
           str(curr_id),  # 文字
           (x, y),  # 坐标
           cv.FONT_HERSHEY_PLAIN,
           5,  # 字号
           (0, 0, 255),  # 字体颜色
           thickness=2  # 粗细
       )

cv.imshow(win_name, img)

#输入cv.imread后的图片,通过点击四个点选择要裁剪的部分
def get_points(src):
   points = []
   indexer = Indexer()
   h, w=get_window_size(src)
   win_name = 'get_points'
   cv.namedWindow(win_name, cv.WINDOW_NORMAL)
   cv.resizeWindow(win_name, width=w, height=h)
   cv.imshow(win_name, src)
   cv.setMouseCallback(win_name, on_EVENT_LBUTTONDOWN,
                       param={'src': src, 'window': win_name, 'indexer': indexer, 'points': points})
   cv.waitKey(0)
   cv.destroyAllWindows()
   if len(points)>4:
       return points[0:4]
   # print(points)
   # points=[(2, 14), (90, 50), (87, 194), (1, 204)]
   return points

#输入cv.imread后的图片,展示图片长什么样
def show_img(src):
   win_name = 'show_img'
   h, w=get_window_size(src)
   cv.namedWindow(win_name, cv.WINDOW_NORMAL)
   cv.resizeWindow(win_name, width=w, height=h)
   cv.imshow(win_name, src)
   cv.waitKey(0)
   cv.destroyAllWindows()

将图片截取,并按照指定的长宽比恢复成矩形


def photo_cut_restore(src,points,H,W):

target_points = [(0, 0), (W, 0), (W, H), (0, H)]
   points, target_points = np.array(points, dtype=np.float32), np.array(target_points, dtype=np.float32)
   M = cv.getPerspectiveTransform(points, target_points)
   # print('透视变换矩阵:', M)

result = cv.warpPerspective(src_copy, M, (0, 0))
   result = result[:H, :W]
   win_name = 'Result'
   cv.namedWindow(win_name, cv.WINDOW_NORMAL)
   cv.resizeWindow(win_name, width=W, height=H)
   cv.imshow(win_name,result)
   cv.waitKey(0)
   cv.destroyAllWindows()
   return  result

主程序


if __name__ == '__main__':

path = './1.jpg'
   src = cv.imread(path)
   src_copy = src.copy()

show_img(src)

W = 20
   H = 20
   # points=[(112, 308), (175, 310), (176, 369), (113, 369)]

points=get_points(src)
   n = 20
   W = int(W * n)
   H = int(H * n)

result=photo_cut_restore(src_copy,points,H,W)

output_file = 'result.jpg'
   cv.imwrite(output_file, result)

三、懒人一键复制代码

诶,气不气,好不容易一段段复制完,结果最后居然有一键复制的地方


import cv2 as cv
import numpy as np

#输入cv.imread后的图片,通过点击四个点选择要裁剪的部分
def get_window_size(src, bound=600):
   h,w = src.shape[0], src.shape[1]
   if h > w:
       h, w = bound, int(w*bound/h)
   else:
       h, w = int(h*bound/w), bound
   return (h, w)

class Indexer:
   def __init__(self):
       self.id = 0

def get_id(self):
       self.id = (self.id + 1)
       return (self.id)

def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
   if event == cv.EVENT_LBUTTONDOWN:
       img = param['src']
       win_name = param['window']
       indexer = param['indexer']
       points = param['points']

curr_id = indexer.get_id()
       points.append((x, y))
       print('第{}个顶点: ({},{})'.format(curr_id, x, y))

cv.circle(img, (x, y), 10, (0, 0, 255), thickness=2)
       cv.putText(
           img,
           str(curr_id),  # 文字
           (x, y),  # 坐标
           cv.FONT_HERSHEY_PLAIN,
           5,  # 字号
           (0, 0, 255),  # 字体颜色
           thickness=2  # 粗细
       )

cv.imshow(win_name, img)

#输入cv.imread后的图片,通过点击四个点选择要裁剪的部分
def get_points(src):
   points = []
   indexer = Indexer()
   h, w=get_window_size(src)
   win_name = 'get_points'
   cv.namedWindow(win_name, cv.WINDOW_NORMAL)
   cv.resizeWindow(win_name, width=w, height=h)
   cv.imshow(win_name, src)
   cv.setMouseCallback(win_name, on_EVENT_LBUTTONDOWN,
                       param={'src': src, 'window': win_name, 'indexer': indexer, 'points': points})
   cv.waitKey(0)
   cv.destroyAllWindows()
   if len(points)>4:
       return points[0:4]
   # print(points)
   # points=[(2, 14), (90, 50), (87, 194), (1, 204)]
   return points

#输入cv.imread后的图片,展示图片长什么样
def show_img(src):
   win_name = 'show_img'
   h, w=get_window_size(src)
   cv.namedWindow(win_name, cv.WINDOW_NORMAL)
   cv.resizeWindow(win_name, width=w, height=h)
   cv.imshow(win_name, src)
   cv.waitKey(0)
   cv.destroyAllWindows()

def photo_cut_restore(src,points,H,W):

target_points = [(0, 0), (W, 0), (W, H), (0, H)]
   points, target_points = np.array(points, dtype=np.float32), np.array(target_points, dtype=np.float32)
   M = cv.getPerspectiveTransform(points, target_points)
   # print('透视变换矩阵:', M)

result = cv.warpPerspective(src_copy, M, (0, 0))
   result = result[:H, :W]
   win_name = 'Result'
   cv.namedWindow(win_name, cv.WINDOW_NORMAL)
   cv.resizeWindow(win_name, width=W, height=H)
   cv.imshow(win_name,result)
   cv.waitKey(0)
   cv.destroyAllWindows()
   return  result

if __name__ == '__main__':

path = './3.jpg'
   src = cv.imread(path)
   src_copy = src.copy()

# show_img(src)

W = 20
   H = 20
   # points=[(124, 182), (181, 177), (180, 243), (125, 266)]
   points=get_points(src)
   print(points)
   n = 20
   W = int(W * n)
   H = int(H * n)

result=photo_cut_restore(src_copy,points,H,W)

output_file = 'result.jpg'
   cv.imwrite(output_file, result)

来源:https://blog.csdn.net/weixin_43958086/article/details/117250288

标签:Python,图片,裁剪
0
投稿

猜你喜欢

  • python队列原理及实现方法示例

    2022-10-08 20:53:09
  • Docker-Compose创建mysql容器详解

    2024-01-17 06:10:19
  • Django配置Mysql数据库连接的实现

    2024-01-28 10:48:33
  • Yii2结合Workerman的websocket示例详解

    2023-11-17 04:48:40
  • CSS3中的box-sizing属性

    2010-04-05 21:52:00
  • python 字段拆分详解

    2021-03-16 22:59:58
  • Python实现图片批量加入水印代码实例

    2022-07-13 10:47:04
  • 简单谈谈php延迟静态绑定

    2024-06-05 15:42:33
  • Java 使用正则表达式对象实现正则的获取功能

    2023-05-10 06:29:16
  • JavaScript—window对象使用示例

    2024-05-08 09:39:34
  • Python的这些库,你知道多少?

    2021-12-12 04:02:39
  • OAuth 2.0 概念及授权流程梳理

    2023-06-28 02:41:28
  • Python爬虫基础讲解之scrapy框架

    2023-11-16 00:07:31
  • Python实现的寻找前5个默尼森数算法示例

    2023-05-15 17:34:29
  • 通俗易懂详解Python基础五种下划线作用

    2024-01-01 06:36:22
  • 举例讲解Python设计模式编程中的访问者与观察者模式

    2021-06-10 06:38:16
  • pandas.DataFrame.to_json按行转json的方法

    2022-11-09 11:24:23
  • 20分钟成功编写bootstrap响应式页面 就这么简单

    2023-08-12 06:12:13
  • Python logging设置和logger解析

    2021-07-15 22:12:52
  • Python使用 TCP协议实现智能聊天机器人功能

    2022-08-19 03:49:48
  • asp之家 网络编程 m.aspxhome.com