Note that there are some explanatory texts on larger screens.

plurals
  1. PONumPy, PIL adding an image
    text
    copied!<p>I'm trying to add two images together using NumPy and PIL. The way I would do this in <a href="http://en.wikipedia.org/wiki/MATLAB" rel="noreferrer">MATLAB</a> would be something like:</p> <pre><code>&gt;&gt; M1 = imread('_1.jpg'); &gt;&gt; M2 = imread('_2.jpg'); &gt;&gt; resM = M1 + M2; &gt;&gt; imwrite(resM, 'res.jpg'); </code></pre> <p>I get something like this:</p> <p><a href="http://www.deadlink.cc/matlab.jpg" rel="noreferrer">alt text http://www.deadlink.cc/matlab.jpg</a></p> <p>Using a compositing program and adding the images the MATLAB result seems to be right.</p> <p>In Python I'm trying to do the same thing like this:</p> <pre><code>from PIL import Image from numpy import * im1 = Image.open('/Users/rem7/Desktop/_1.jpg') im2 = Image.open('/Users/rem7/Desktop/_2.jpg') im1arr = asarray(im1) im2arr = asarray(im2) addition = im1arr + im2arr resultImage = Image.fromarray(addition) resultImage.save('/Users/rem7/Desktop/a.jpg') </code></pre> <p>and I get something like this:</p> <p><a href="http://www.deadlink.cc/python.jpg" rel="noreferrer">alt text http://www.deadlink.cc/python.jpg</a></p> <p>Why am I getting all those funky colors? I also tried using <code>ImageMath.eval("a+b", a=im1, b=im2)</code>, but I get an error about RGB unsupported.</p> <p>I also saw that there is an <code>Image.blend()</code> but that requires an alpha.</p> <p>What's the best way to achieve what I'm looking for?</p> <p>Source Images (images have been removed):</p> <p><a href="http://www.deadlink.cc/_1.jpg" rel="noreferrer">alt text http://www.deadlink.cc/_1.jpg</a> <a href="http://www.deadlink.cc/_2.jpg" rel="noreferrer">alt text http://www.deadlink.cc/_2.jpg</a></p> <p>Humm, OK, well I added the source images using the add image icon and they show up when I'm editing the post, but for some reason the images don't show up in the post. </p> <p>(images have been removed) 2013 05 09</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload