title: Python PIL - function to divide blend two images?
body: <p><strong>EDIT:</strong> Code is working now, thanks to Mark and zephyr. zephyr also has two alternate working solutions below.</p>
  
  <p>I want to divide blend two images with PIL. I found <code>ImageChops.multiply(image1, image2)</code> but I couldn't find a similar <code>divide(image, image2)</code> function.</p>
  
  <p><a href="http://emptyeasel.com/2008/10/31/explaining-blending-modes-in-photoshop-and-gimp-multiply-divide-overlay-screen/" rel="noreferrer">Divide Blend Mode Explained</a> (I used the first two images here as my test sources.)</p>
  
  <p>Is there a built-in divide blend function that I missed (PIL or otherwise)?</p>
  
  <p>My test code below runs and is getting close to what I'm looking for. The resulting image output is similar to the divide blend example image here: <a href="http://emptyeasel.com/2008/10/31/explaining-blending-modes-in-photoshop-and-gimp-multiply-divide-overlay-screen/" rel="noreferrer">Divide Blend Mode Explained</a>.</p>
  
  <p>Is there a more efficient way to do this divide blend operation (less steps and faster)? At first, I tried using lambda functions in <code>Image.eval</code> and <code>ImageMath.eval</code> to check for black pixels and flip them to white during the division process, but I couldn't get either to produce the correct result.</p>
  
  <p><strong>EDIT: Fixed code</strong> and shortened thanks to Mark and zephyr. The resulting image output matches the output from zephyr's numpy and scipy solutions below.</p>
  
  <pre><code># PIL Divide Blend test
  
  import Image, os, ImageMath
  
  imgA = Image.open('01background.jpg')
  imgA.load()
  imgB = Image.open('02testgray.jpg')
  imgB.load()
  
  # split RGB images into 3 channels
  rA, gA, bA = imgA.split()
  rB, gB, bB = imgB.split()
  
  # divide each channel (image1/image2)
  rTmp = ImageMath.eval("int(a/((float(b)+1)/256))", a=rA, b=rB).convert('L')
  gTmp = ImageMath.eval("int(a/((float(b)+1)/256))", a=gA, b=gB).convert('L')
  bTmp = ImageMath.eval("int(a/((float(b)+1)/256))", a=bA, b=bB).convert('L')
  
  # merge channels into RGB image
  imgOut = Image.merge("RGB", (rTmp, gTmp, bTmp))
  
  imgOut.save('PILdiv0.png', 'PNG')
  
  os.system('start PILdiv0.png')
  </code></pre>