How to paste a PNG image with transparency to another image in PIL without white pixels?
Question: I have two images, a background and a PNG image with transparent pixels. I am
trying to paste the PNG onto the background using Python-PIL but when I paste
the two images I get white pixels around the PNG image where there were
transparent pixels.

My code:

    
    
    import os
    from PIL import Image, ImageDraw, ImageFont
    
    filename='pikachu.png'
    ironman = Image.open(filename, 'r')
    filename1='bg.png'
    bg = Image.open(filename1, 'r')
    text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
    text_img.paste(bg, (0,0))
    text_img.paste(ironman, (0,0))
    text_img.save("ball.png", format="png")
    

My images:  
[![enter image description
here](http://i.stack.imgur.com/BcUf3m.png)](http://i.stack.imgur.com/BcUf3m.png)
[![enter image description
here](http://i.stack.imgur.com/tC7som.png)](http://i.stack.imgur.com/tC7som.png)

**my output image:**  
[![enter image description
here](http://i.stack.imgur.com/IZtSP.png)](http://i.stack.imgur.com/IZtSP.png)

How can I have transparent pixels instead of white?


Answer: You just need to specify the image as the mask as follows in the paste
function:

    
    
    import os
    from PIL import Image, ImageDraw, ImageFont
    
    filename='pikachu.png'
    ironman = Image.open(filename, 'r')
    filename1='bg.png'
    bg = Image.open(filename1, 'r')
    text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
    text_img.paste(bg, (0,0))
    text_img.paste(ironman, (0,0), mask=ironman)
    text_img.save("ball.png", format="png")
    

Giving you:

[![paste with
transparency](http://i.stack.imgur.com/bwoXS.png)](http://i.stack.imgur.com/bwoXS.png)

