栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

opencv生成图图片阵列

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

opencv生成图图片阵列

from typing import Tuple
import cv2
import numpy as np
import argparse
import os

parser = argparse.ArgumentParser('将目录中的图片生成固定大小的图片阵列')
parser.add_argument('inputDir', type=str,
                    help='the directory stores the pictures!')
parser.add_argument('format', type=str, help='formet of picture to process!')
parser.add_argument('row', type=int, help='the row num of picture array!')
parser.add_argument('col', type=int, help='the column num of picture array!')
parser.add_argument('outPath', type=str,
                    help='the path to store the picture array!')
parser.add_argument('--label', type=bool,
                    help='the flag indicate whether note the picture with its name!', default=True)

args = parser.parse_args()
inputDir = args.inputDir
format = args.format
row = args.row
col = args.col
outPath = args.outPath

imgfiles = os.listdir(inputDir)
imgfiles = list(filter(lambda imgfile: imgfile.endswith(format), imgfiles))
if row*col != len(imgfiles):
    print('The row num and row num don't match the num of images!')
    exit()
imgArray = list()
index = 0
for i in range(row):
    rowArray = list()
    for j in range(col):
        filename = imgfiles[index]
        imgfile = os.path.join(inputDir, filename)
        img = cv2.imread(imgfile)
        filename = filename[0:filename.rindex('.')]
        img = cv2.putText(img=img, text=filename, org=(10, 30), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                          fontScale=1, color=(0, 0, 0), thickness=2, lineType=cv2.LINE_AA, bottomLeftOrigin=False)
        rowArray.append(img)
        index += 1
    rowArray = np.concatenate(rowArray, axis=1)
    imgArray.append(rowArray)
imgArray = np.concatenate(imgArray, axis=0)
print(imgArray.shape)
cv2.imwrite(outPath, imgArray)
print('finished!')

调用方式

 python imgArray.py ./ 'tif' 5 4 demo.tif
效果


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498984.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号