1

I want to combine two images of different 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' ) 

Output:

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

Is it possible to first combine them and then save them in one image and keep their base "color" (grayscale and 3 channel)?

Artur Müller Romanov
  • 4,417
  • 10
  • 73
  • 132
  • 1
    The error says `all the input arrays must have same number of dimensions`. Is it possible that one of your images has a single channel (2 dimensions) and the other has multiple color channels (3 dimensions)? – jdehesa Nov 12 '18 at 16:00
  • Oh, now that you say it, yes. Is there any hope first to combine a grayscale image with a 3channel image and then save them as such? – Artur Müller Romanov Nov 12 '18 at 16:01
  • Well you would have to either convert the grayscale to RGB or the other way around. You can do this simply by replicating/averaging channels or using one of the existing formulas for that (see [Converting RGB to grayscale/intensity](https://stackoverflow.com/q/687261)). – jdehesa Nov 12 '18 at 16:19
  • You can convert grayscale to BGR with OpenCV using `img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)` or to RGB with `img = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)`. – Scratch'N'Purr Nov 12 '18 at 16:22
  • Or using PIL: [Python Pil Change GreyScale Tif To RGB](https://stackoverflow.com/q/18522295) – jdehesa Nov 12 '18 at 16:29
  • Maybe if you shared the images and the resultant image you expect, it would be possible to help you further. Or if you explained which aspects of each you wanted to retain. In general though, images either have 1 channel which means greyscale, or 3 channels which means RGB. – Mark Setchell Nov 13 '18 at 09:22

1 Answers1

0

I found out that opencv supports cross channel operations:

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)
Artur Müller Romanov
  • 4,417
  • 10
  • 73
  • 132
  • 2
    If that is the answer, your question is very misleading! :-) You aren't really combining channels to form a new image, you are placing two images side-by-side on a larger canvas really. You can do that without **OpenCV** too, by the way. You can also do that in **Terminal** at the command-line with **ImageMagick** `convert a.png b.png +append result.png` – Mark Setchell Nov 13 '18 at 14:38
  • Thanks for the insight. I am pretty new into image manipulation. Will try to specify my questions better. – Artur Müller Romanov Nov 14 '18 at 11:27