# Combine Images Using Numpy

5/5 - (1 vote)

Summary: You can combine images represented in the form of Numpy arrays using the `concatenate` function of the Numpy library as `np.concatenate((numpydata_1, numpydata_2), axis=1)`. This combines the images horizontally. Use syntax: `np.concatenate((numpydata_1, numpydata_2), axis=0)` to combine the images vertically.

## Problem Formulation

Consider you have two images represented as Numpy arrays of pixels. How will you combine the two images represented in the form of Numpy pixel arrays?

Combining two images that are in the form of Numpy arrays will create a new Numpy array having pixels that will represent a new combined image formed by either concatenating the two images horizontally or vertically. Let’s understand this with the help of an example:

Given: Let’s say we have two different images as given below (Both images have similar dimensions)

When you convert them to Numpy arrays this is how you can represent the two images:

Challenge: Combine the two images – (i) horizontally (ii) vertically

Expected Output:

So, are you up for the challenge? Well! If it looks daunting – don’t worry. This tutorial will guide you through the techniques to solve the programming challenge. So, without further delay let us dive into the solution.

Prerequisite: To understand how the solutions to follow work it is essential to understand – “How to concatenate two Numpy arrays in Python.”

NumPy’s `concatenate()` method joins a sequence of arrays along an existing axis. The first couple of comma-separated array arguments are joined. If you use the axis argument, you can specify along which axis the arrays should be joined. For example, `np.concatenate(a, b, axis=0)` joins arrays along the first axis and `np.concatenate(a, b, axis=None)` joins the flattened arrays.

To learn more about concatenating arrays in Python, here’s a wonderful tutorial that will guide you through numerous methods of doing so: How to Concatenate Two NumPy Arrays?

## Combine Images “Horizontally” with Numpy

Approach: The `concatenate()` method of the Numpy library allows you combine matrices of different images along different axes. To combine the two image arrays horizontally, you must specify the axis=1.

Code: Please go through the comments mentioned in the script in order to understand how each line of code works.

```from PIL import Image
import numpy as np
img_1 = Image.open('img_1.JPG')
img_2 = Image.open('img_2.JPG')
# Converting the two images into Numpy Arrays
numpydata_1 = np.asarray(img_1)
numpydata_2 = np.asarray(img_2)
# Combining the two images horizontally
horizontal = np.concatenate((numpydata_1, numpydata_2), axis=1)
# Display the horizontally combined image as a Numpy Array
print(horizontal)
# converting the combined image in the Numpy Array form to an image format
data = Image.fromarray(horizontal)
# Saving the combined image
data.save('combined_pic.png')```

Output:

```[[[184 186 201]
[184 186 201]
[184 187 202]
...
[242 245 252]
[240 243 250]
[241 244 251]]

[[184 186 201]
[184 186 201]
[184 187 202]
...
[242 245 252]
[241 244 251]
[241 244 251]]

[[183 186 203]
[183 186 203]
[184 187 204]
...
[243 246 253]
[243 246 253]
[241 244 251]]

...

[[ 43  64  55]
[ 45  66  57]
[ 48  69  60]
...
[ 72 108 106]
[ 73 109 107]
[ 77 113 111]]

[[ 49  70  61]
[ 50  71  62]
[ 53  73  64]
...
[ 69 104 100]
[ 69 104 100]
[ 71 106 102]]

[[ 53  74  65]
[ 52  73  64]
[ 56  73  65]
...
[ 65  97  94]
[ 60  92  89]
[ 64  96  93]]]```

Here’s how the horizontally combined image looks like when saved to a file:

Wonderful! Isn’t it?

## Combine Images “Vertically” with Numpy

In the previous solution, we combined the images horizontally. In this soution you will learn how to combine two images represented in the form of Numpy arrays vertically.

Approach: The idea is quite similar to the previous solution with the only difference in the axis parameter of the `concatenate()` method. To combine the two image arrays vertically, you must specify the axis=0.

Code:

```from PIL import Image
import numpy as np
img_1 = Image.open('img_1.JPG')
img_2 = Image.open('img_2.JPG')
# Converting the two images into Numpy Arrays
numpydata_1 = np.asarray(img_1)
numpydata_2 = np.asarray(img_2)
# Combining the two images horizontally
vertical = np.concatenate((numpydata_1, numpydata_2), axis=0)
# Display the vertically combined image as a Numpy Array
print(vertical)
# converting the combined image in the Numpy Array form to an image format
data = Image.fromarray(vertical)
# Saving the combined image
data.save('combined_pic.png')```

Output:

```[[[184 186 201]
[184 186 201]
[184 187 202]
...
[174 189 218]
[174 189 218]
[173 188 217]]

[[184 186 201]
[184 186 201]
[184 187 202]
...
[174 189 218]
[173 188 217]
[173 188 217]]

[[183 186 203]
[183 186 203]
[184 187 204]
...
[173 188 217]
[173 188 217]
[172 187 216]]

...

[[115 152 144]
[111 151 142]
[ 96 142 131]
...
[ 72 108 106]
[ 73 109 107]
[ 77 113 111]]

[[ 75 118 108]
[ 79 125 114]
[ 82 132 120]
...
[ 69 104 100]
[ 69 104 100]
[ 71 106 102]]

[[ 42  90  78]
[ 47  97  85]
[ 64 116 103]
...
[ 65  97  94]
[ 60  92  89]
[ 64  96  93]]]```

Here’s how the horizontally combined image looks like when saved to a file:

Hurrah! We have successfully combined the two images vertically.

## Exercises

Before we wrap this tutorial, here’s a set of challenges to further enhance your knowledge.

Challenge 1: Consider that you have been given an image. How will you convert this image to a Numpy array?

Given Image

Solution:

```from PIL import Image
from numpy import asarray
img = Image.open('img.png')
img_to_array = asarray(img)
print(img_to_array)```

Challenge 2: Consider that you have two images of different dimensions. How will you combine the two images horizontally using OpenCV?

Given Images:

Solution:

```import cv2
import numpy as np
h1, w1 = img_1.shape[:2]
h2, w2 = img_2.shape[:2]
img_3 = np.zeros((max(h1, h2), w1 + w2, 3), dtype=np.uint8)
img_3[:, :] = (255, 255, 255)
img_3[:h1, :w1, :3] = img_1
img_3[:h2, w1:w1 + w2, :3] = img_2
cv2.imwrite('Img_3.png', img_3)```

Output: