基于Python怎样实现图片裁剪的功能
Admin 2022-07-29 群英技术资讯 322 次浏览
首先复制代码会吧?
1.有张照片
这是网上随便找的一张照片,自行保存测试
2.看看照片
运行代码,其中show_img函数是展示照片
3.选择角点
按照左上,右上,右下,左下的顺序选择四个角点
如果担心自己选不好,可以直接去除我代码里的points的注释,那是我自己用的原版
4.最终结果
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)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了Pytorch中的Tensorboard与Transforms搭配使用,主要是结合了前两篇文章的的一个小练习,感兴趣的小伙伴可以来练习一下,希望对你的学习有所帮助
Python函数式编程入门要学习哪些,一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
当我们用python进行数据处理时会遇到很多缺失值,缺失值一般是由于我们所处理的数据本身的特性、当初录入的失误或者其它原因导致的,下面这篇文章主要给大家介绍了关于pandas返回缺失值位置的方法,需要的朋友可以参考下
Python基础-函数(三)<1> 对返回的数据直接拆包。<2> 交换2个变量的值
内容介绍基础代码改进再次改进:又次改进:改进:----加准确率基础代码pred_y=test_output.data.numpy()pred_y=pred_y.flatten()print(p
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008