gpt4 book ai didi

python - 如何组合不同 channel 的图像?

转载 作者:太空宇宙 更新时间:2023-11-03 21:35:39 25 4
gpt4 key购买 nike

我想合并不同 channel 的两个图像:

import numpy as np
from PIL import Image

list_im = ['1.png', '2.png']
imgs = [ Image.open(i) for i in list_im ]
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]

imgs_comb = np.vstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
imgs_comb = Image.fromarray( imgs_comb)
imgs_comb.save( '3.png' )


##imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
##imgs_comb = Image.fromarray( imgs_comb)
##imgs_comb.save( '4.png' )

输出:

    return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
ValueError: all the input arrays must have same number of dimensions

是否可以先将它们组合起来,然后将它们保存在一张图像中并保留其基本“颜色”(灰度和 3 channel )?

最佳答案

我发现opencv支持跨 channel 操作:

import cv2
import numpy as np

img1 = cv2.imread('1.png') #smaller img (size set to this img)
img2 = cv2.imread('2.png')

img2 = cv2.resize(img2, (len(img1[0]),len(img1))) # resize img

hstack = np.concatenate((img1, img2), axis = 0) # combine horizontally
cv2.imwrite('3.png', hstack)

vstack = np.concatenate((img1, img2), axis = 1) # combine vertically
cv2.imwrite('4.png', vstack)

关于python - 如何组合不同 channel 的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53265729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com