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!')

调用方式

opencv生成图图片阵列_第1张图片

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

效果

opencv生成图图片阵列_第2张图片
opencv生成图图片阵列_第3张图片

你可能感兴趣的:(opencv,计算机视觉,图像处理)