当前位置:   article > 正文

python实现将多张图片结合为一张图片的方法_python多张图片拼接成一张图片

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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

案例二:

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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

案例三:

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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/81489
推荐阅读
相关标签
  

闽ICP备14008679号