{"id":128171,"date":"2022-09-16T15:45:26","date_gmt":"2022-09-16T15:45:26","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=682461"},"modified":"2022-09-16T15:45:26","modified_gmt":"2022-09-16T15:45:26","slug":"combine-images-using-numpy","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/16\/combine-images-using-numpy\/","title":{"rendered":"Combine Images Using Numpy"},"content":{"rendered":"\n<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;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> <span class=\"kksr-muted\">Rate this post<\/span> <\/div>\n<\/div>\n<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>\n<h2><strong>Problem Formulation<\/strong><\/h2>\n<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>\n<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&#8217;s understand this with the help of an example:<\/p>\n<p><strong>Given: <\/strong>Let&#8217;s say we have two different images as given below (<em>Both images have similar dimensions)<\/em> &#8211;<\/p>\n<figure class=\"wp-container-2 wp-block-gallery-1 wp-block-gallery has-nested-images columns-default is-cropped\">\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 448px) 100vw, 448px\" \/><figcaption><strong>img_1.JPG<\/strong><\/figcaption><\/figure>\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" 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=\"auto, (max-width: 448px) 100vw, 448px\" \/><figcaption><strong>img_2.JPG<\/strong><\/figcaption><\/figure>\n<\/figure>\n<p>When you convert them to Numpy arrays this is how you can represent the two images: <\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>numpydata_1<\/strong><\/td>\n<td><strong>numpydata_2<\/strong><\/td>\n<\/tr>\n<tr>\n<td>[[[184 186 201]<br \/>[184 186 201]<br \/>[184 187 202]<br \/>&#8230;<br \/>[174 189 218]<br \/>[174 189 218]<br \/>[173 188 217]]<\/p>\n<p>[[184 186 201]<br \/>[184 186 201]<br \/>[184 187 202]<br \/>&#8230;<br \/>[174 189 218]<br \/>[173 188 217]<br \/>[173 188 217]]<\/p>\n<p>[[183 186 203]<br \/>[183 186 203]<br \/>[184 187 204]<br \/>&#8230;<br \/>[173 188 217]<br \/>[173 188 217]<br \/>[172 187 216]]<\/p>\n<p>&#8230;<\/p>\n<p>[[ 43 64 55]<br \/>[ 45 66 57]<br \/>[ 48 69 60]<br \/>&#8230;<br \/>[ 63 84 77]<br \/>[ 64 83 77]<br \/>[ 64 83 77]]<\/p>\n<p>[[ 49 70 61]<br \/>[ 50 71 62]<br \/>[ 53 73 64]<br \/>&#8230;<br \/>[ 58 84 73]<br \/>[ 60 83 73]<br \/>[ 61 84 74]]<\/p>\n<p>[[ 53 74 65]<br \/>[ 52 73 64]<br \/>[ 56 73 65]<br \/>&#8230;<br \/>[ 56 84 72]<br \/>[ 57 83 72]<br \/>[ 57 83 72]]]<\/td>\n<td>[[[255 255 255]<br \/>[255 255 255]<br \/>[255 255 255]<br \/>&#8230;<br \/>[242 245 252]<br \/>[240 243 250]<br \/>[241 244 251]]<\/p>\n<p>[[255 255 255]<br \/>[255 255 255]<br \/>[255 255 255]<br \/>&#8230;<br \/>[242 245 252]<br \/>[241 244 251]<br \/>[241 244 251]]<\/p>\n<p>[[255 255 255]<br \/>[255 255 255]<br \/>[255 255 255]<br \/>&#8230;<br \/>[243 246 253]<br \/>[243 246 253]<br \/>[241 244 251]]<\/p>\n<p>&#8230;<\/p>\n<p>[[115 152 144]<br \/>[111 151 142]<br \/>[ 96 142 131]<br \/>&#8230;<br \/>[ 72 108 106]<br \/>[ 73 109 107]<br \/>[ 77 113 111]]<\/p>\n<p>[[ 75 118 108]<br \/>[ 79 125 114]<br \/>[ 82 132 120]<br \/>&#8230;<br \/>[ 69 104 100]<br \/>[ 69 104 100]<br \/>[ 71 106 102]]<\/p>\n<p>[[ 42 90 78]<br \/>[ 47 97 85]<br \/>[ 64 116 103]<br \/>&#8230;<br \/>[ 65 97 94]<br \/>[ 60 92 89]<br \/>[ 64 96 93]]]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><strong>Challenge: <\/strong>Combine the two images &#8211; (i) horizontally (ii) vertically<\/p>\n<p><strong>Expected Output:<\/strong><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table class=\"has-fixed-layout\">\n<tbody>\n<tr>\n<td><strong>Horizontal Combination<\/strong><\/td>\n<td><strong>Vertical Combination<\/strong><\/td>\n<\/tr>\n<tr>\n<td>[[[184 186 201]<br \/>[184 186 201]<br \/>[184 187 202]<br \/>\u2026<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 \/>\u2026<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 \/>\u2026<br \/>[243 246 253]<br \/>[243 246 253]<br \/>[241 244 251]]<br \/>\u2026<br \/>[[ 43 64 55]<br \/>[ 45 66 57]<br \/>[ 48 69 60]<br \/>\u2026<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 \/>\u2026<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 \/>\u2026<br \/>[ 65 97 94]<br \/>[ 60 92 89]<br \/>[ 64 96 93]]]<\/td>\n<td>[[[184 186 201]<br \/>[184 186 201]<br \/>[184 187 202]<br \/>\u2026<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 \/>\u2026<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 \/>\u2026<br \/>[173 188 217]<br \/>[173 188 217]<br \/>[172 187 216]]<br \/>\u2026<br \/>[[115 152 144]<br \/>[111 151 142]<br \/>[ 96 142 131]<br \/>\u2026<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 \/>\u2026<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 \/>\u2026<br \/>[ 65 97 94]<br \/>[ 60 92 89]<br \/>[ 64 96 93]]]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>So, are you up for the challenge? Well! If it looks daunting &#8211; don&#8217;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>\n<p><strong>Prerequisite:<\/strong> To understand how the solutions to follow work it is essential to understand &#8211; &#8220;How to concatenate two Numpy arrays in Python.&#8221;<\/p>\n<p>NumPy\u2019s\u00a0<code><a rel=\"noreferrer noopener\" href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.concatenate.html\" target=\"_blank\">concatenate()<\/a><\/code>\u00a0method 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,\u00a0<code>np.concatenate(a, b, axis=0)<\/code>\u00a0joins arrays along the first axis and\u00a0<code>np.concatenate(a, b, axis=None)<\/code>\u00a0joins the flattened arrays. <\/p>\n<p class=\"has-base-2-background-color has-background\">To learn more about concatenating arrays in Python, here&#8217;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>\n<h2>Combine Images &#8220;Horizontally&#8221; with Numpy<\/h2>\n<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>\n<p><strong>Code:<\/strong> Please go through the comments mentioned in the script in order to understand how each line of code works.<\/p>\n<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\nimport numpy as np\n# Reading the given images img_1 = Image.open('img_1.JPG')\nimg_2 = Image.open('img_2.JPG')\n# Converting the two images into Numpy Arrays\nnumpydata_1 = np.asarray(img_1)\nnumpydata_2 = np.asarray(img_2)\n# Combining the two images horizontally\nhorizontal = np.concatenate((numpydata_1, numpydata_2), axis=1)\n# Display the horizontally combined image as a Numpy Array\nprint(horizontal)\n# converting the combined image in the Numpy Array form to an image format\ndata = Image.fromarray(horizontal)\n# Saving the combined image\ndata.save('combined_pic.png')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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>\n<p>Here&#8217;s how the horizontally combined image looks like when saved to a file:<\/p>\n<figure class=\"wp-block-image size-full is-style-default\"><img decoding=\"async\" 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=\"auto, (max-width: 896px) 100vw, 896px\" \/><\/figure>\n<p>Wonderful! Isn&#8217;t it?<\/p>\n<h2><strong>Combine Images &#8220;Vertically&#8221; with Numpy<\/strong><\/h2>\n<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>\n<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>\n<p><strong>Code:<\/strong><\/p>\n<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\nimport numpy as np\n# Reading the given images\nimg_1 = Image.open('img_1.JPG')\nimg_2 = Image.open('img_2.JPG')\n# Converting the two images into Numpy Arrays\nnumpydata_1 = np.asarray(img_1)\nnumpydata_2 = np.asarray(img_2)\n# Combining the two images horizontally\nvertical = np.concatenate((numpydata_1, numpydata_2), axis=0)\n# Display the vertically combined image as a Numpy Array\nprint(vertical)\n# converting the combined image in the Numpy Array form to an image format\ndata = Image.fromarray(vertical)\n# Saving the combined image\ndata.save('combined_pic.png')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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=\"\">\n[[[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>\n<p>Here&#8217;s how the horizontally combined image looks like when saved to a file:<\/p>\n<figure class=\"wp-block-image size-full is-style-default\"><img decoding=\"async\" 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=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/figure>\n<p>Hurrah! We have successfully combined the two images vertically.<\/p>\n<h2><strong>Exercises<\/strong><\/h2>\n<p>Before we wrap this tutorial, here&#8217;s a set of challenges to further enhance your knowledge.<\/p>\n<p><strong>Challenge 1: <\/strong>Consider that you have been given an image. How will you convert this image to a Numpy array?<\/p>\n<p class=\"has-small-font-size\"><strong><strong>Given Image<\/strong><\/strong><\/p>\n<figure class=\"wp-block-image size-full is-resized is-style-default\"><img decoding=\"async\" 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=\"auto, (max-width: 186px) 100vw, 186px\" \/><\/figure>\n<p><strong>Solution:<\/strong><\/p>\n<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\nfrom numpy import asarray\nimg = Image.open('img.png')\nimg_to_array = asarray(img)\nprint(img_to_array)<\/pre>\n<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>\n<p>Given Images:<\/p>\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><img decoding=\"async\" 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>\n<figure class=\"wp-block-image size-full is-resized is-style-default\"><img decoding=\"async\" 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=\"auto, (max-width: 104px) 100vw, 104px\" \/><figcaption>Image_2.png<\/figcaption><\/figure>\n<p><strong>Solution:<\/strong><\/p>\n<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\nimport numpy as np\nimg_1 = cv2.imread('Imgage_1.png')\nimg_2 = cv2.imread('Image_2.png')\nh1, w1 = img_1.shape[:2]\nh2, w2 = img_2.shape[:2]\nimg_3 = np.zeros((max(h1, h2), w1 + w2, 3), dtype=np.uint8)\nimg_3[:, :] = (255, 255, 255)\nimg_3[:h1, :w1, :3] = img_1\nimg_3[:h2, w1:w1 + w2, :3] = img_2\ncv2.imwrite('Img_3.png', img_3)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><img decoding=\"async\" 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=\"auto, (max-width: 452px) 100vw, 452px\" \/><figcaption>Img_3.png<\/figcaption><\/figure>\n<p class=\"has-background\" style=\"background-color:#e0f8f2\">Want to learn about OpenCV? Here&#8217;s an amazing tutorial to get you started with OpenCV &#8211; <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>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Phew! That was some coding challenge! I hope you can now successfully combine images given as Numpy arrays in both dimensions &#8211; 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>\n<p>Happy coding! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/academy.finxter.com\/university\/opencv\/\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" 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=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/a><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Rate this post 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. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-128171","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128171"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128171\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}