0

I have two pictures: one contains black letters and has a white background and the other one is a common image processing picture. I want to add them so the final picture has the second image as background and the letters in front. I'm using Python's PIL library.

My first picture containing only letters

My first picture containing only letters

The second picture I want to have as background

The second picture I want to have as background

How can I do that?

Thanks

costisst
  • 381
  • 2
  • 6
  • 22

2 Answers2

0

Assuming that you can make the background of your "words only" layer transparent, you can then use the following solution: How to merge a transparent png image with another image using PIL

DatHydroGuy
  • 1,056
  • 3
  • 11
  • 18
0

You can do it by pasting the text image onto background image with a mask. In this case you can construct the mask from the image with text by first converting it to grayscale, and then inverting it.

Where the mask image is 0, the image will be unchanged, and where it's 1 the pixels are copied from the image being pasted. Since the letters of the text are black in this case (which is 0), inverting the text to create the mask was necessary.

Here's what I mean:

from PIL import Image, ImageOps

text = Image.open('riders.jpg')
mask = ImageOps.invert(text.convert('L'))
face = Image.open('face.jpg')
face.paste(text, mask=mask)
face.show()

Resulting image displayed:

results

martineau
  • 119,623
  • 25
  • 170
  • 301