How to overlay two images?

Dalal: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/3/43985606cfa3569a64e27c809bd99fd3e05ae4b9.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/43985606cfa3569a64e27c809bd99fd3e05ae4b9" title="hair"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/3/43985606cfa3569a64e27c809bd99fd3e05ae4b9_2_500x500.jpeg" alt="hair" data-base62-sha1="9DYmSn7KyX2ik55rGbUIO5W32WZ" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/3/43985606cfa3569a64e27c809bd99fd3e05ae4b9_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/4/3/43985606cfa3569a64e27c809bd99fd3e05ae4b9_2_750x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/3/43985606cfa3569a64e27c809bd99fd3e05ae4b9.jpeg 2x" data-dominant-color="100A0B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">hair</span><span class="informations">768×768 20.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/e/9e1d1b3a849d48c5cf118ed71aa23dbd151ed4a3.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/9e1d1b3a849d48c5cf118ed71aa23dbd151ed4a3" title="skin"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e1d1b3a849d48c5cf118ed71aa23dbd151ed4a3_2_500x500.jpeg" alt="skin" data-base62-sha1="myJNCPgm3JfhVlYkLY1RUoDaAH9" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e1d1b3a849d48c5cf118ed71aa23dbd151ed4a3_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e1d1b3a849d48c5cf118ed71aa23dbd151ed4a3_2_750x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/e/9e1d1b3a849d48c5cf118ed71aa23dbd151ed4a3.jpeg 2x" data-dominant-color="E49498"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">skin</span><span class="informations">768×768 18.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/f/9f3c6876001a310a4c73caf1d5b7d22fcd90fab1.png" data-download-href="https://discuss.pytorch.org/uploads/default/9f3c6876001a310a4c73caf1d5b7d22fcd90fab1" title="blended"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9f3c6876001a310a4c73caf1d5b7d22fcd90fab1_2_500x500.png" alt="blended" data-base62-sha1="mIFlaxLc19IjW8rHK13J1CQ0wAV" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9f3c6876001a310a4c73caf1d5b7d22fcd90fab1_2_500x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9f3c6876001a310a4c73caf1d5b7d22fcd90fab1_2_750x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/f/9f3c6876001a310a4c73caf1d5b7d22fcd90fab1.png 2x" data-dominant-color="B9787C"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">blended</span><span class="informations">768×768 265 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/2/d2dd5547671c5ae02c038d4d05fbd4f250dc27b6.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d2dd5547671c5ae02c038d4d05fbd4f250dc27b6" title="ISIC_0024339"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/2/d2dd5547671c5ae02c038d4d05fbd4f250dc27b6_2_500x500.jpeg" alt="ISIC_0024339" data-base62-sha1="u5otjvTGLS0VftCwQsJ0HKA1mpo" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/2/d2dd5547671c5ae02c038d4d05fbd4f250dc27b6_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/d/2/d2dd5547671c5ae02c038d4d05fbd4f250dc27b6_2_750x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/2/d2dd5547671c5ae02c038d4d05fbd4f250dc27b6.jpeg 2x" data-dominant-color="E39397"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">ISIC_0024339</span><span class="informations">768×768 22.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Hello,<br>
I want to overlay two images, the one of hair and the one of skin. When I try this code:<br>
blended = Image.blend(im1, im2, alpha=0.2) I get the result in Image 3 which is different from the result I am looking for (in the last image). How can I do that correctly?</p>
<p>thanks in advance.</p>

ptrblck: <p>This question doesn’t seem to be PyTorch-specific, so you might get a better answer in an image processing discussion board. In any case, I would check if morphological dilation and erosion could help narrowing down the mask a bit.</p>

InnovArul: <p>What if you do a direct assignment of pixels instead of blending?<br>
Something like below?</p>
<pre><code class="lang-auto">image1 = torch.randn(10,10) # 1st image
image2 = torch.randn(10,10) # 2nd image
image1[image2 &gt; 0] = image2[image2&gt;0]
</code></pre>