Monday, July 15, 2024
HomePythonMix Photos Utilizing Numpy – Finxter

Mix Photos Utilizing Numpy – Finxter


Abstract: You may mix photographs represented within the type of Numpy arrays utilizing the concatenate perform of the Numpy library as np.concatenate((numpydata_1, numpydata_2), axis=1). This combines the pictures horizontally. Use syntax: np.concatenate((numpydata_1, numpydata_2), axis=0) to mix the pictures vertically.

Downside Formulation

Take into account you’ve two photographs represented as Numpy arrays of pixels. How will you mix the 2 photographs represented within the type of Numpy pixel arrays?

Combining two photographs which are within the type of Numpy arrays will create a brand new Numpy array having pixels that may signify a brand new mixed picture shaped by both concatenating the 2 photographs horizontally or vertically. Let’s perceive this with the assistance of an instance:

Given: Let’s say now we have two completely different photographs as given under (Each photographs have comparable dimensions)

Whenever you convert them to Numpy arrays that is how one can signify the 2 photographs:

numpydata_1 numpydata_2
[[[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]]

[[ 43 64 55]
[ 45 66 57]
[ 48 69 60]

[ 63 84 77]
[ 64 83 77]
[ 64 83 77]]

[[ 49 70 61]
[ 50 71 62]
[ 53 73 64]

[ 58 84 73]
[ 60 83 73]
[ 61 84 74]]

[[ 53 74 65]
[ 52 73 64]
[ 56 73 65]

[ 56 84 72]
[ 57 83 72]
[ 57 83 72]]]

[[[255 255 255]
[255 255 255]
[255 255 255]

[242 245 252]
[240 243 250]
[241 244 251]]

[[255 255 255]
[255 255 255]
[255 255 255]

[242 245 252]
[241 244 251]
[241 244 251]]

[[255 255 255]
[255 255 255]
[255 255 255]

[243 246 253]
[243 246 253]
[241 244 251]]

[[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]]]

Problem: Mix the 2 photographs – (i) horizontally (ii) vertically

Anticipated Output:

Horizontal Mixture Vertical Mixture
[[[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]]]
[[[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]]]

So, are you up for the problem? Properly! If it seems daunting – don’t fear. This tutorial will information you thru the strategies to unravel the programming problem. So, with out additional delay allow us to dive into the answer.

Prerequisite: To grasp how the options to comply with work it’s important to know – “The way to concatenate two Numpy arrays in Python.”

NumPy’s concatenate() technique joins a sequence of arrays alongside an current axis. The primary couple of comma-separated array arguments are joined. Should you use the axis argument, you possibly can specify alongside which axis the arrays must be joined. For instance, np.concatenate(a, b, axis=0) joins arrays alongside the primary axis and np.concatenate(a, b, axis=None) joins the flattened arrays.

To study extra about concatenating arrays in Python, right here’s a beautiful tutorial that may information you thru quite a few strategies of doing so: The way to Concatenate Two NumPy Arrays?

Mix Photos “Horizontally” with Numpy

Strategy: The concatenate() technique of the Numpy library permits you mix matrices of various photographs alongside completely different axes. To mix the 2 picture arrays horizontally, you could specify the axis=1.

Code: Please undergo the feedback talked about within the script to be able to perceive how every line of code works.

from PIL import Picture
import numpy as np
# Studying the given photographs 
img_1 = Picture.open('img_1.JPG')
img_2 = Picture.open('img_2.JPG')
# Changing the 2 photographs into Numpy Arrays
numpydata_1 = np.asarray(img_1)
numpydata_2 = np.asarray(img_2)
# Combining the 2 photographs horizontally
horizontal = np.concatenate((numpydata_1, numpydata_2), axis=1)
# Show the horizontally mixed picture as a Numpy Array
print(horizontal)
# changing the mixed picture within the Numpy Array kind to a picture format
information = Picture.fromarray(horizontal)
# Saving the mixed picture
information.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]]]

Right here’s how the horizontally mixed picture seems like when saved to a file:

Great! Isn’t it?

Mix Photos “Vertically” with Numpy

Within the earlier answer, we mixed the pictures horizontally. On this soution you’ll discover ways to mix two photographs represented within the type of Numpy arrays vertically.

Strategy: The thought is kind of just like the earlier answer with the one distinction within the axis parameter of the concatenate() technique. To mix the 2 picture arrays vertically, you could specify the axis=0.

Code:

from PIL import Picture
import numpy as np
# Studying the given photographs
img_1 = Picture.open('img_1.JPG')
img_2 = Picture.open('img_2.JPG')
# Changing the 2 photographs into Numpy Arrays
numpydata_1 = np.asarray(img_1)
numpydata_2 = np.asarray(img_2)
# Combining the 2 photographs horizontally
vertical = np.concatenate((numpydata_1, numpydata_2), axis=0)
# Show the vertically mixed picture as a Numpy Array
print(vertical)
# changing the mixed picture within the Numpy Array kind to a picture format
information = Picture.fromarray(vertical)
# Saving the mixed picture
information.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]]]

Right here’s how the horizontally mixed picture seems like when saved to a file:

Hurrah! We have now efficiently mixed the 2 photographs vertically.

Workouts

Earlier than we wrap this tutorial, right here’s a set of challenges to additional improve your data.

Problem 1: Take into account that you’ve been given a picture. How will you change this picture to a Numpy array?

Given Picture

Answer:

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

Problem 2: Take into account that you’ve two photographs of various dimensions. How will you mix the 2 photographs horizontally utilizing OpenCV?

Given Photos:

Image_1.png
Image_2.png

Answer:

import cv2
import numpy as np
img_1 = cv2.imread('Imgage_1.png')
img_2 = cv2.imread('Image_2.png')
h1, w1 = img_1.form[:2]
h2, w2 = img_2.form[: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:

Img_3.png

Wish to find out about OpenCV? Right here’s an incredible tutorial to get you began with OpenCV – Python OpenCV Picture Processing.

Conclusion

Phew! That was some coding problem! I hope now you can efficiently mix photographs given as Numpy arrays in each dimensions – horizontally in addition to vertically. With that we come to the top of this tutorial. Please subscribe and keep tuned for extra attention-grabbing tutorials and options sooner or later.

Completely satisfied coding! 🙂


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments