Sick Gaming
[Tut] Combine Images Using Numpy - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Combine Images Using Numpy (/thread-99960.html)



[Tut] Combine Images Using Numpy - xSicKxBot - 09-18-2022

Combine Images Using Numpy

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;682461&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</div>
<p class="has-global-color-8-background-color has-background"><strong>Summary: </strong>You can combine images represented in the form of Numpy arrays using the <code data-enlighter-language="generic" class="EnlighterJSRAW">concatenate</code> function of the Numpy library as <code>np.concatenate((numpydata_1, numpydata_2), axis=1)</code>. This combines the images horizontally. Use syntax: <code>np.concatenate((numpydata_1, numpydata_2), axis=0)</code> to combine the images vertically.</p>
<h2><strong>Problem Formulation</strong></h2>
<p>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?</p>
<p>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:</p>
<p><strong>Given: </strong>Let’s say we have two different images as given below (<em>Both images have similar dimensions)</em> –</p>
<figure class="wp-container-2 wp-block-gallery-1 wp-block-gallery has-nested-images columns-default is-cropped">
<figure class="wp-block-image size-large is-style-default"><img loading="lazy" width="448" height="336" data-id="683617" src="https://blog.finxter.com/wp-content/uploads/2022/09/img_2-1.jpg" alt="" class="wp-image-683617" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/img_2-1.jpg 448w, https://blog.finxter.com/wp-content/uploads/2022/09/img_2-1-300x225.jpg 300w" sizes="(max-width: 448px) 100vw, 448px" /><figcaption><strong>img_1.JPG</strong></figcaption></figure>
<figure class="wp-block-image size-large is-style-default"><img loading="lazy" width="448" height="336" data-id="683616" src="https://blog.finxter.com/wp-content/uploads/2022/09/img_1-1.jpg" alt="" class="wp-image-683616" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/img_1-1.jpg 448w, https://blog.finxter.com/wp-content/uploads/2022/09/img_1-1-300x225.jpg 300w" sizes="(max-width: 448px) 100vw, 448px" /><figcaption><strong>img_2.JPG</strong></figcaption></figure>
</figure>
<p>When you convert them to Numpy arrays this is how you can represent the two images: </p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>numpydata_1</strong></td>
<td><strong>numpydata_2</strong></td>
</tr>
<tr>
<td>[[[184 186 201]<br />[184 186 201]<br />[184 187 202]<br />…<br />[174 189 218]<br />[174 189 218]<br />[173 188 217]]</p>
<p>[[184 186 201]<br />[184 186 201]<br />[184 187 202]<br />…<br />[174 189 218]<br />[173 188 217]<br />[173 188 217]]</p>
<p>[[183 186 203]<br />[183 186 203]<br />[184 187 204]<br />…<br />[173 188 217]<br />[173 188 217]<br />[172 187 216]]</p>
<p>…</p>
<p>[[ 43 64 55]<br />[ 45 66 57]<br />[ 48 69 60]<br />…<br />[ 63 84 77]<br />[ 64 83 77]<br />[ 64 83 77]]</p>
<p>[[ 49 70 61]<br />[ 50 71 62]<br />[ 53 73 64]<br />…<br />[ 58 84 73]<br />[ 60 83 73]<br />[ 61 84 74]]</p>
<p>[[ 53 74 65]<br />[ 52 73 64]<br />[ 56 73 65]<br />…<br />[ 56 84 72]<br />[ 57 83 72]<br />[ 57 83 72]]]</td>
<td>[[[255 255 255]<br />[255 255 255]<br />[255 255 255]<br />…<br />[242 245 252]<br />[240 243 250]<br />[241 244 251]]</p>
<p>[[255 255 255]<br />[255 255 255]<br />[255 255 255]<br />…<br />[242 245 252]<br />[241 244 251]<br />[241 244 251]]</p>
<p>[[255 255 255]<br />[255 255 255]<br />[255 255 255]<br />…<br />[243 246 253]<br />[243 246 253]<br />[241 244 251]]</p>
<p>…</p>
<p>[[115 152 144]<br />[111 151 142]<br />[ 96 142 131]<br />…<br />[ 72 108 106]<br />[ 73 109 107]<br />[ 77 113 111]]</p>
<p>[[ 75 118 108]<br />[ 79 125 114]<br />[ 82 132 120]<br />…<br />[ 69 104 100]<br />[ 69 104 100]<br />[ 71 106 102]]</p>
<p>[[ 42 90 78]<br />[ 47 97 85]<br />[ 64 116 103]<br />…<br />[ 65 97 94]<br />[ 60 92 89]<br />[ 64 96 93]]]</td>
</tr>
</tbody>
</table>
</figure>
<p><strong>Challenge: </strong>Combine the two images – (i) horizontally (ii) vertically</p>
<p><strong>Expected Output:</strong></p>
<figure class="wp-block-table is-style-stripes">
<table class="has-fixed-layout">
<tbody>
<tr>
<td><strong>Horizontal Combination</strong></td>
<td><strong>Vertical Combination</strong></td>
</tr>
<tr>
<td>[[[184 186 201]<br />[184 186 201]<br />[184 187 202]<br />…<br />[242 245 252]<br />[240 243 250]<br />[241 244 251]]<br />[[184 186 201]<br />[184 186 201]<br />[184 187 202]<br />…<br />[242 245 252]<br />[241 244 251]<br />[241 244 251]]<br />[[183 186 203]<br />[183 186 203]<br />[184 187 204]<br />…<br />[243 246 253]<br />[243 246 253]<br />[241 244 251]]<br />…<br />[[ 43 64 55]<br />[ 45 66 57]<br />[ 48 69 60]<br />…<br />[ 72 108 106]<br />[ 73 109 107]<br />[ 77 113 111]]<br />[[ 49 70 61]<br />[ 50 71 62]<br />[ 53 73 64]<br />…<br />[ 69 104 100]<br />[ 69 104 100]<br />[ 71 106 102]]<br />[[ 53 74 65]<br />[ 52 73 64]<br />[ 56 73 65]<br />…<br />[ 65 97 94]<br />[ 60 92 89]<br />[ 64 96 93]]]</td>
<td>[[[184 186 201]<br />[184 186 201]<br />[184 187 202]<br />…<br />[174 189 218]<br />[174 189 218]<br />[173 188 217]]<br />[[184 186 201]<br />[184 186 201]<br />[184 187 202]<br />…<br />[174 189 218]<br />[173 188 217]<br />[173 188 217]]<br />[[183 186 203]<br />[183 186 203]<br />[184 187 204]<br />…<br />[173 188 217]<br />[173 188 217]<br />[172 187 216]]<br />…<br />[[115 152 144]<br />[111 151 142]<br />[ 96 142 131]<br />…<br />[ 72 108 106]<br />[ 73 109 107]<br />[ 77 113 111]]<br />[[ 75 118 108]<br />[ 79 125 114]<br />[ 82 132 120]<br />…<br />[ 69 104 100]<br />[ 69 104 100]<br />[ 71 106 102]]<br />[[ 42 90 78]<br />[ 47 97 85]<br />[ 64 116 103]<br />…<br />[ 65 97 94]<br />[ 60 92 89]<br />[ 64 96 93]]]</td>
</tr>
</tbody>
</table>
</figure>
<p>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.</p>
<p><strong>Prerequisite:</strong> To understand how the solutions to follow work it is essential to understand – “How to concatenate two Numpy arrays in Python.”</p>
<p>NumPy’s <code><a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html" target="_blank">concatenate()</a></code> 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, <code>np.concatenate(a, b, axis=0)</code> joins arrays along the first axis and <code>np.concatenate(a, b, axis=None)</code> joins the flattened arrays. </p>
<p class="has-base-2-background-color has-background">To learn more about concatenating arrays in Python, here’s a wonderful tutorial that will guide you through numerous methods of doing so: <strong><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-concatenate-two-numpy-arrays/" target="_blank">How to Concatenate Two NumPy Arrays?</a></strong></p>
<h2>Combine Images “Horizontally” with Numpy</h2>
<p><strong>Approach: </strong>The <code>concatenate()</code> 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 <strong>axis=1</strong>. </p>
<p><strong>Code:</strong> Please go through the comments mentioned in the script in order to understand how each line of code works.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from PIL import Image
import numpy as np
# Reading the given images 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')</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[[[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]]]</pre>
<p>Here’s how the horizontally combined image looks like when saved to a file:</p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="896" height="336" src="https://blog.finxter.com/wp-content/uploads/2022/09/image-50.png" alt="" class="wp-image-683483" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/image-50.png 896w, https://blog.finxter.com/wp-content/uploads/2022/09/image-50-300x113.png 300w, https://blog.finxter.com/wp-content/uploads/2022/09/image-50-768x288.png 768w" sizes="(max-width: 896px) 100vw, 896px" /></figure>
<p>Wonderful! Isn’t it?</p>
<h2><strong>Combine Images “Vertically” with Numpy</strong></h2>
<p>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. </p>
<p><strong>Approach: </strong>The idea is quite similar to the previous solution with the only difference in the <em>axis</em> parameter of the <code>concatenate()</code> method. To combine the two image arrays vertically, you must specify the <strong>axis=0</strong>.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from PIL import Image
import numpy as np
# Reading the given images
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')</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">
[[[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]]]</pre>
<p>Here’s how the horizontally combined image looks like when saved to a file:</p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="448" height="672" src="https://blog.finxter.com/wp-content/uploads/2022/09/image-51.png" alt="" class="wp-image-683580" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/image-51.png 448w, https://blog.finxter.com/wp-content/uploads/2022/09/image-51-200x300.png 200w" sizes="(max-width: 448px) 100vw, 448px" /></figure>
<p>Hurrah! We have successfully combined the two images vertically.</p>
<h2><strong>Exercises</strong></h2>
<p>Before we wrap this tutorial, here’s a set of challenges to further enhance your knowledge.</p>
<p><strong>Challenge 1: </strong>Consider that you have been given an image. How will you convert this image to a Numpy array?</p>
<p class="has-small-font-size"><strong><strong>Given Image</strong></strong></p>
<figure class="wp-block-image size-full is-resized is-style-default"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2022/09/image-52.png" alt="" class="wp-image-683629" width="186" height="48" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/image-52.png 642w, https://blog.finxter.com/wp-content/uploads/2022/09/image-52-300x78.png 300w" sizes="(max-width: 186px) 100vw, 186px" /></figure>
<p><strong>Solution:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from PIL import Image
from numpy import asarray
img = Image.open('img.png')
img_to_array = asarray(img)
print(img_to_array)</pre>
<p><strong>Challenge 2: </strong>Consider that you have two images of different dimensions. How will you combine the two images horizontally using OpenCV?</p>
<p>Given Images:</p>
<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2022/09/tom_jerry-1-1024x768.jpg" alt="" class="wp-image-683691" width="289" height="217" /><figcaption>Image_1.png</figcaption></figure>
<figure class="wp-block-image size-full is-resized is-style-default"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2022/09/tomandjerry-1.jpg" alt="" class="wp-image-683694" width="104" height="104" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/tomandjerry-1.jpg 646w, https://blog.finxter.com/wp-content/uploads/2022/09/tomandjerry-1-300x300.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/09/tomandjerry-1-150x150.jpg 150w" sizes="(max-width: 104px) 100vw, 104px" /><figcaption>Image_2.png</figcaption></figure>
<p><strong>Solution:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import cv2
import numpy as np
img_1 = cv2.imread('Imgage_1.png')
img_2 = cv2.imread('Image_2.png')
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)</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2022/09/image-53-1024x499.png" alt="" class="wp-image-683705" width="452" height="220" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/image-53-1024x499.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/09/image-53-300x146.png 300w, https://blog.finxter.com/wp-content/uploads/2022/09/image-53-768x374.png 768w, https://blog.finxter.com/wp-content/uploads/2022/09/image-53-1536x749.png 1536w, https://blog.finxter.com/wp-content/uploads/2022/09/image-53.png 1846w" sizes="(max-width: 452px) 100vw, 452px" /><figcaption>Img_3.png</figcaption></figure>
<p class="has-background" style="background-color:#e0f8f2">Want to learn about OpenCV? Here’s an amazing tutorial to get you started with OpenCV – <strong><a href="https://blog.finxter.com/five-useful-image-processing-techniques-in-python-using-opencv/" target="_blank" rel="noreferrer noopener">Python OpenCV Image Processing</a></strong>.</p>
<h2><strong>Conclusion</strong></h2>
<p>Phew! That was some coding challenge! I hope you can now successfully combine images given as Numpy arrays in both dimensions – horizontally as well as vertically. With that we come to the end of this tutorial. Please subscribe and stay tuned for more interesting tutorials and solutions in the future. </p>
<p>Happy coding! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://academy.finxter.com/university/opencv/" target="_blank" rel="noopener"><img loading="lazy" width="363" height="650" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-305.png" alt="" class="wp-image-387309" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-305.png 363w, https://blog.finxter.com/wp-content/uploads/2022/05/image-305-168x300.png 168w" sizes="(max-width: 363px) 100vw, 363px" /></a></figure>
</div>
</div>


https://www.sickgaming.net/blog/2022/09/16/combine-images-using-numpy/