Pillow handles PNG files incorrectly
<p>I can successfully convert a rectangular image into a png with transparent rounded corners like this: </p>

<p><a href="http://i.stack.imgur.com/phKNv.png" rel="nofollow"><img src="http://i.stack.imgur.com/phKNv.png" alt=".png image with transparent corners"></a></p>

<p>However, when I take this transparent cornered image and I want to use it in another image generated with Pillow, I end up with this: <a href="http://i.stack.imgur.com/7iJb0.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/7iJb0.jpg" alt="transparent corners turned black by Pillow"></a></p>

<p>The transparent corners become black. I've been playing around with this for a while but I can't find any way in which the transparent parts of an image don't turn black once I place them on another image with Pillow.
Here is the code I use:</p>

<pre><code>mask = Image.open('Test mask.png').convert('L')
im = Image.open('boat.jpg')
im.resize(mask.size)
output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
output.putalpha(mask)
output.save('output.png')

im = Image.open('output.png')

image_bg = Image.new('RGBA', (1292,440), (255,255,255,100))
image_fg = im.resize((710, 400), Image.ANTIALIAS)
image_bg.paste(image_fg, (20, 20))

image_bg.save('output2.jpg')
</code></pre>

<p>Is there a solution for this? Thanks.</p>

<p>Per some suggestions I exported the 2nd image as a PNG, but then I ended up with an image with holes in it: 
<a href="http://i.stack.imgur.com/qGd0n.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/qGd0n.jpg" alt="enter image description here"></a>
Obviously I want the second image to have a consistent white background without holes.</p>

<p>Here is what I actually want to end up with. The orange is only placed there to highlight the image itself. It's a rectangular image with white background, with a picture placed into it with rounded corners.
<a href="http://i.stack.imgur.com/XMKxS.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/XMKxS.jpg" alt="enter image description here"></a></p>

<p>If you paste an image with transparent pixels onto another image, the transparent pixels are just copied as well. It looks like you only want to paste the non-transparent pixels. In that case, you need a mask for the <code>paste</code> function.</p>

<pre><code>image_bg.paste(image_fg, (20, 20), mask=image_fg)
</code></pre>

<p>Note the third argument here. From the documentation:</p>

<blockquote>
  <p>If a mask is given, this method updates only the regions indicated by
  the mask.  You can use either "1", "L" or "RGBA" images (in the latter
  case, the alpha band is used as mask). Where the mask is 255, the
  given image is copied as is.  Where the mask is 0, the current value
  is preserved.  Intermediate values will mix the two images together,
  including their alpha channels if they have them.</p>
</blockquote>

<p>What we did here is provide an RGBA image as mask, and use the alpha channel as mask.</p>

python pillow