python实现将多张图片结合为一张图片的方法

将多张图片结合为一张图片

案例一:

import cv2
import numpy as np
import os
import glob

# img1=cv2.imread(r"F:\myyolov5\202302141303-L2_27.jpg")
# print(img1.shape)
# img2=cv2.imread(r"F:\myyolov5\202303011344R2_112_1.jpg")
# print(img2.shape)
# img1=cv2.resize(img1,(512,500))
# img2=cv2.resize(img2,(512,500))
# inputs=np.hstack((img1,img2))
# # cv2.imshow("input img",inputs)
# cv2.imwrite(r"F:\Desktop\a.jpg",inputs)
# cv2.waitKey(0)


def open_image(path1):
    img_path = glob.glob(path1)
    print(img_path)
    return np.array([cv2.imread(true_path,0) for true_path in img_path])

def combine_images(datasets,newraw,newcol):
    raw,col = datasets[0].shape
    newimage = np.zeros((newraw*raw,newcol*col))
    for i in range(0,newraw):
        for j in range(0,newcol):
            for ii in range(0,raw):
                for jj in range(0,col):
                    newimage[i*raw+ii][j*col+jj] = output[i*newcol+j][ii][jj]

    return newimage


if __name__ == '__main__':
    inputpath = 'F:\Desktop\myfile\*'
    output = open_image(inputpath)

    ohhh = combine_images(output,1,14)  # 这个4,6的意思就是,我的图片通过那个打开的函数之后,变成了一个大的ndarray里面有24张图片,我想让他们以,行4列6,的形式组合起来。
    cv2.imwrite(r"F:\Desktop\b.jpg",ohhh)

案例二:

def image_resize(img_name):
    img=cv2.imread(img_name,0)
    # w,h=img.shape
    # img=cv2.resize(img,(int(w/2),int(h/2)))
    return img


def open_image(path1):
    img_path = glob.glob(path1)
    print(img_path)
    img_path.sort(key=lambda x: int(x.split("\\")[-1].split(".")[0]))
    print(img_path)
    # return np.array([cv2.imread(true_path,0) for true_path in img_path])
    return np.array([image_resize(true_path) for true_path in img_path])

def combine_images(datasets,newraw,newcol):
    raw,col = datasets[0].shape
    newimage = np.zeros((newraw*raw,newcol*col))
    for i in range(0,newraw):
        for j in range(0,newcol):
            for ii in range(0,raw):
                for jj in range(0,col):
                    newimage[i*raw+ii][j*col+jj] = output[i*newcol+j][ii][jj]

    return newimage


if __name__ == '__main__':
    num_name="right_up"
    select="right_up"
    # inputpath = r'F:\Desktop\image\right_up\352\*'
    inputpath=r"D:\image\%s\%s\*"%(select,num_name)
    outputpath=r"D:\360_ditie_concat_after_image1\%s"%(select)
    if not os.path.exists(outputpath):
        os.mkdir(outputpath)
    # print(inputpath)
    output = open_image(inputpath)
    print(output.shape)
    ohhh=np.concatenate(output,1)
    print(ohhh.shape)
    # ohhh = combine_images(output,1,output.shape[0])  # 这个4,6的意思就是,我的图片通过那个打开的函数之后,变成了一个大的ndarray里面有24张图片,我想让他们以,行4列6,的形式组合起来。
    # print(ohhh.shape)
    # cv2.imwrite(os.path.join(outputpath,"GZDT18-043044_left_up_camImg_%s.jpg"%(num_name)),ohhh,[cv2.IMWRITE_JPEG_QUALITY, 90])
    cv2.imwrite(os.path.join(outputpath, "GZDT18-043044_%s_camImg_%s.png" % (select,num_name)), ohhh)

案例三:

import cv2
import numpy as np
import os
import glob

def open_image(path1):
    img_path = glob.glob(path1)
    print(img_path)
    img_path.sort(key=lambda x: int(x.split("\\")[-1].split(".")[0]))
    print(img_path)
    x1,x2,x3,x4=img_path[:15],img_path[15:30],img_path[30:45],img_path[45:60]
    x1=np.array([cv2.imread(true_path, 0) for true_path in x1])
    x2=np.array([cv2.imread(true_path,0) for true_path in x2])
    x3=np.array([cv2.imread(true_path,0) for true_path in x3])
    x4=np.array([cv2.imread(true_path,0) for true_path in x4])
    return x1,x2,x3,x4

def combine_images(datasets,newraw,newcol):
    raw,col = datasets[0].shape
    newimage = np.zeros((newraw*raw,newcol*col))
    for i in range(0,newraw):
        for j in range(0,newcol):
            for ii in range(0,raw):
                for jj in range(0,col):
                    newimage[i*raw+ii][j*col+jj] = output[i*newcol+j][ii][jj]

    return newimage


if __name__ == '__main__':
    inputpath = r'F:\Desktop\my_gaotietu\*'
    print(inputpath)
    # inputpath=inputpath.sort(key=lambda x: int(x.split('/')[-1].split('.')[0].split('_')[-1]))
    # print(inputpath)
    output = open_image(inputpath)

    ohhh = combine_images(output[0],1,15)  # 这个4,6的意思就是,我的图片通过那个打开的函数之后,变成了一个大的ndarray里面有24张图片,我想让他们以,行4列6,的形式组合起来。
    cv2.imwrite(r"F:\Desktop\my_gantie_save\1.jpg",ohhh)
    ohhh_1 = combine_images(output[1], 1, 15)
    cv2.imwrite(r"F:\Desktop\my_gantie_save\2.jpg", ohhh_1)
    ohhh_2 = combine_images(output[2], 1, 15)
    cv2.imwrite(r"F:\Desktop\my_gantie_save\3.jpg", ohhh_2)
    ohhh_3 = combine_images(output[3], 1, 15)
    cv2.imwrite(r"F:\Desktop\my_gantie_save\4.jpg", ohhh_3)

你可能感兴趣的:(python,numpy,开发语言)