import cv2 as cv
import numpy as np

def stackImage(imgArray,scale,lables=[]):
    '''
    imgArray:表示图像列表,格式为([src1,src2...],[src3,src4...])
    scale:表示图像的缩放倍数
    lables:表示每一个图像的标签，比如[["1","2"...],["3","4"...]]标签与图像一一对应
    '''
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0],list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range(0,rows):
            for y in range(0,cols):
                imgArray[x][y] = cv.resize(imgArray[x][y],(0,0),None,scale,scale)
                if len(imgArray[x][y].shape)==2:
                    imgArray[x][y]=cv.cvtColor(imgArray[x][y],cv.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height,width,3),np.uint8)
        hor = [imageBlank]*rows
        hor_con = [imageBlank]*rows
        for x in range(0,rows):
            hor[x] = np.hstack(imgArray[x])
            hor_con[x]=np.concatenate(imgArray[x])
        ver = np.hstack(hor)
        ver_con = np.concatenate(hor)
    else:
        for x in range(0,rows):
            imgArray[x] = cv.resize(imgArray[x],(0,0),None,scale,scale)
            if len(imgArray[x].shape)==2:
                imgArray[x]=cv.cvtColor(imgArray[x],cv.COLOR_GRAY2BGR)
        hor = np.hstack(imgArray)
        hor_con=np.concatenate(imgArray)
        ver = hor
    if len(lables) != 0:
        eachImgWidth=int(ver.shape[1]/cols)
        eachImgHeight=int(ver.shape[0]/rows)
        for d in range(0,rows):
            for c in range(0,cols):
                cv.rectangle(ver,(c*eachImgWidth,eachImgHeight*d),
                (c*eachImgWidth+len(lables[d][c])*13+27,30+eachImgHeight*d),(0,0,255),2)
                cv.putText(ver,lables[d][c],(c*eachImgWidth+10,eachImgHeight*d+20),
                cv.FONT_HERSHEY_COMPLEX,0.7,(255,0,255),2)
    return ver