numpy数组做图片拼接的实现(concatenate、vstack、hstack)
作者:hi 你好。 时间:2022-10-23 15:21:21
两种方法拼接
#img = np.vstack((img, img2)) # vstack按垂直方向,hstack按水平方向
img = np.concatenate((img, img2), axis=0) # axis=0 按垂直方向,axis=1 按水平方向
统一图片大小,保证数组维度一致避免拼接失败。 把图片全部调整成第一张图的宽高
def img_size(image_names,width, height):
for i in image_names:
img = cv2.imread(os.path.join(img_path, i))
img_resize = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
cv2.imwrite(os.path.join(img_path, i), img_resize)
print(os.path.join(img_path, i))
完整案例,拼接文件夹中的所有图片
import cv2
import os
import numpy as np
def img_size(image_names,width, height):
for i in image_names:
img = cv2.imread(os.path.join(img_path, i))
img_resize = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
cv2.imwrite(os.path.join(img_path, i), img_resize)
print(os.path.join(img_path, i))
if __name__ == '__main__':
img_path = r'F:\studytest'
image_names = [name for name in os.listdir(img_path) if os.path.splitext(name)[1] == ".jpg"]
img1 = cv2.imread(os.path.join(img_path, image_names[0]))
width, height = img1.shape[:2][::-1]
img_size(image_names,width, height)
img = img1
for i in range(1,len(image_names)):
img_page = image_names[i]
img2 = cv2.imread(os.path.join(img_path, img_page))
#img = np.vstack((img, img2)) # vstack按垂直方向,hstack按水平方向
img = np.concatenate((img, img2), axis=0) # axis=0 按垂直方向,axis=1 按水平方向
cv2.imwrite(os.path.join(img_path,"res.jpg"), img)
# cv2.imshow("img",img)
# cv2.waitKey()
``
来源:https://blog.csdn.net/songrps/article/details/102938548
标签:numpy,数组,图片拼接
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用Vue.js制作一个拼图华容道小游戏
2024-05-22 10:43:11
![](https://img.aspxhome.com/file/2023/4/123214_0s.jpg)
解决vue请求接口第一次成功,第二次失败问题
2023-07-02 16:59:59
Python实现随机从图像中获取多个patch
2021-09-19 13:39:56
![](https://img.aspxhome.com/file/2023/0/97120_0s.jpg)
golang中之strconv包的具体使用方法
2024-02-01 00:03:20
python四则运算表达式求值示例详解
2023-01-31 06:03:04
详解Python中的GIL(全局解释器锁)详解及解决GIL的几种方案
2021-02-01 03:36:05
![](https://img.aspxhome.com/file/2023/1/114791_0s.jpg)
使用babel-plugin-import 实现自动按需引入方式
2024-04-27 16:00:42
![](https://img.aspxhome.com/file/2023/2/133052_0s.jpg)
Django框架获取form表单数据方式总结
2022-08-21 14:24:29
python实现文件助手中查看微信撤回消息
2023-09-03 07:23:50
python编写adb截图工具的实现源码
2021-03-24 08:50:15
![](https://img.aspxhome.com/file/2023/5/77725_0s.png)
Oracle开发之分析函数简介Over用法
2024-01-17 08:40:45
mysql主从复制读写分离的配置方法详解
2024-01-13 22:26:25
![](https://img.aspxhome.com/file/2023/7/71717_0s.png)
windows系统下Python环境搭建教程
2021-06-25 11:07:53
![](https://img.aspxhome.com/file/2023/4/113114_0s.png)
tensorflow常用函数API介绍
2022-10-18 17:49:54
![](https://img.aspxhome.com/file/2023/4/72714_0s.png)
Pytorch十九种损失函数的使用详解
2021-07-19 22:23:07
![](https://img.aspxhome.com/file/2023/0/95130_0s.png)
Python中struct模块对字节流/二进制流的操作教程
2021-05-10 19:26:56
![](https://img.aspxhome.com/file/2023/2/90342_0s.png)
PHP/ThinkPHP实现批量打包下载文件的方法示例
2024-05-11 09:49:00
python字符串的多行输出的实例详解
2021-06-25 20:59:45
Python使用cn2an实现中文数字与阿拉伯数字的相互转换
2021-07-28 23:33:51
实例解析js中try、catch、finally的执行规则
2024-04-22 13:25:49