3

I have two images. The first is called white_1.png which is just a white background 600px, the second image is called img2222.png which is just black letter "A".

I would like to paste the letter A into white_1.png. however, in the ouput I only get a black background nothing else. both files are 600px. This is the code I have so far.

from PIL import Image

im1 = Image.open('white_1.png') 
im2 = Image.open('img2222.png')   
im1.paste(im2)

im1.save('dasdsdsad.jpg')
halfer
  • 19,824
  • 17
  • 99
  • 186
ben olsen
  • 663
  • 1
  • 14
  • 27
  • I found the answer. finally https://stackoverflow.com/questions/7510313/transparent-png-in-pil-turns-out-not-to-be-transparent – ben olsen Jun 27 '17 at 22:14

2 Answers2

3

Your img2222.png is probably using transparency. Paste requires a mask argument if you want to use transparent pixels. If you use paste with no mask, it assumes transparent values as white/black pixels.

All you need to do is apply the mask to apply transparency.

im1.paste(im2, (0, 0), im2)

or

im1.paste(im2,mask=im2)

Also be aware that when you save your file as a JPG, JPG doesn't support transparency by default. Wouldn't be a problem here since you don't have any transparency at the end.

NinjaKitty
  • 638
  • 7
  • 17
1

If you shared the images you are using helping would be easier.

Just some thoughts:

  • You are changing the format from .png to .jpg
  • You are not expressing where the paste should be. For instance: im1.paste(im2,(0,0)).
Anton vBR
  • 18,287
  • 5
  • 40
  • 46