Nick
Nick

Reputation: 67

How to concatenate three or more images vertically?

I try to concatenate three images vertically and I need some assistance with the code/function. So far I imported one image and cropped 3 smaller images with the same size. Now I want to concatenate them in one image that will be long, but narrow. However, I can't find an appropriate function or even if I find one I get an error message when I apply it to my code.

I already tried to make a collection from my three pictures and then use the function skimage.io.concatenate_images(sf_collection), but this results in a 4-dimensional picture that cannot be visualized.

sf_collection = (img1,img2,img3)
concat_page = skimage.io.concatenate_images(sf_collection)

My expected output is the three images to be concatenated vertically in one image (very long and narrow).

Upvotes: 1

Views: 5358

Answers (2)

dubbbdan
dubbbdan

Reputation: 2740

Ive never used skimage.io.concatenate, but I think you are looking for np.concatenate. It defaults to axis=0, but you can specify axis=1 for a horizontal stack. This also assumes you have already loaded the images into their array from.

from scipy.misc import face
import numpy as np
import matplotlib.pyplot as plt

face1 = face()
face2 = face()
face3 = face()

merge = np.concatenate((face1,face2,face3))

plt.gray()
plt.imshow(merge)

which returns: enter image description here

If you look at the skimage.io.concatenate_images docs, it's using np.concatenate too. It seems like that function provides a data structure to hold collections of images, but not merge into a single image.

Upvotes: 3

Mark Setchell
Mark Setchell

Reputation: 208107

Like this:

import numpy as np

h, w = 100, 400

yellow = np.zeros((h,w,3),dtype=np.uint8) + np.array([255,255,0],dtype=np.uint8)
red    = np.zeros((h,w,3),dtype=np.uint8) + np.array([255,0,0],dtype=np.uint8)
blue   = np.zeros((h,w,3),dtype=np.uint8) + np.array([0,0,255],dtype=np.uint8)

# Stack vertically
result = np.vstack((yellow,red,blue))

enter image description here

Use the following to stack side-by-side (horizontally):

result = np.hstack((yellow,red,blue))

Upvotes: 1

Related Questions