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

效果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值