Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As everyone suggested already, the weird colors you're observing are overflow. And as you point out in the <a href="https://stackoverflow.com/questions/524930/numpy-pil-adding-an-image/524943#524943">comment of schnaader's answer</a> you <strong>still get overflow</strong> if you add your images like this:</p> <pre><code>addition=(im1arr+im2arr)/2 </code></pre> <p>The reason for this overflow is that your NumPy arrays (<em>im1arr</em> <em>im2arr</em>) are of the <strong>uint8</strong> type (i.e. 8-bit). This means each element of the array can only hold values up to 255, so when your sum exceeds 255, it loops back around 0:</p> <pre><code>&gt;&gt;&gt;array([255,10,100],dtype='uint8') + array([1,10,160],dtype='uint8') array([ 0, 20, 4], dtype=uint8) </code></pre> <p>To avoid overflow, your arrays should be able to contain values beyond 255. You need to <strong>convert them to floats</strong> for instance, perform the blending operation and <strong>convert the result back to uint8</strong>:</p> <pre><code>im1arrF = im1arr.astype('float') im2arrF = im2arr.astype('float') additionF = (im1arrF+im2arrF)/2 addition = additionF.astype('uint8') </code></pre> <p>You <strong>should not</strong> do this:</p> <pre><code>addition = im1arr/2 + im2arr/2 </code></pre> <p>as you lose information, by squashing the dynamic of the image (you effectively make the images 7-bit) before you perform the blending information.</p> <p><strong>MATLAB note</strong>: the reason you don't see this problem in MATLAB, is probably because MATLAB takes care of the overflow implicitly in one of its functions.</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