title: PIL Convert PNG or GIF with Transparency to JPG without
body: <p>I'm prototyping an image processor in Python 2.7 using PIL1.1.7 and I would like all images to end up in JPG.  Input file types will include tiff,gif,png both with transparency and without.  I've been trying to combine two scripts that I found that 1. convert other file types to JPG and 2. removing transparency by creating a blank white image and pasting the original image over the white background.  My searches are being spammed with people seeking to generate or preserve transparency rather than the opposite.</p>
  
  <p>I'm currently working with this:</p>
  
  <pre><code>#!/usr/bin/python
  import os, glob
  import Image
  
  images = glob.glob("*.png")+glob.glob("*.gif")
  
  for infile in images:
      f, e = os.path.splitext(infile)
      outfile = f + ".jpg"
      if infile != outfile:
          #try:
          im = Image.open(infile)
          # Create a new image with a solid color
          background = Image.new('RGBA', im.size, (255, 255, 255))
          # Paste the image on top of the background
          background.paste(im, im)
          #I suspect that the problem is the line below
          im = background.convert('RGB').convert('P', palette=Image.ADAPTIVE)
          im.save(outfile)
          #except IOError:
             # print "cannot convert", infile
  </code></pre>
  
  <p>Both scripts work in isolation, but as I have combined them I get a ValueError: Bad Transparency Mask.</p>
  
  <pre><code>Traceback (most recent call last):
  File "pilhello.py", line 17, in &lt;module&gt;
  background.paste(im, im)
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1101, in paste
  self.im.paste(im, box, mask.im)
  ValueError: bad transparency mask
  </code></pre>
  
  <p>I suspect that if I were to save a PNG without transparency I could then open that new file, and re-save it as a JPG, and delete the PNG that was written to disk, but I'm hoping that there is an elegant solution that I haven't found yet.</p>