Есть библиотека с именем pillow
(которая является вилкой PIL
), которая может сделать это для вас. Вы можете немного поиграть с местами размещения, но я думаю, что это выглядит хорошо.
# Open your two images
cleantxt = Image.open('cleantext.jpg')
dirtybackground = Image.open('dirtybackground.jpg')
# Convert the image to RGBA
cleantxt = cleantxt.convert('RGBA')
# Return a sequence object of every pixel in the text
data = cleantxt.getdata()
new_data = []
# Turn every pixel that looks lighter than gray into a transparent pixel
# This turns everything except the text transparent
for item in data:
if item[0] >= 123 and item[1] >= 123 and item[2] >= 123:
new_data.append((255, 255, 255, 0))
else:
new_data.append(item)
# Replace the old pixel data of the clean text with the transparent pixel data
cleantxt.putdata(new_data)
# Resize the clean text to fit on the dirty background (which is 850 x 555 pixels)
cleantxt.thumbnail((555,555), Image.ANTIALIAS)
# Save the clean text if we want to use it for later
cleantxt.save("cleartext.png", "PNG")
# Overlay the clean text on top of the dirty background
## (0, 0) is the pixel where you place the top left pixel of the clean text
## The second cleantxt is used as a mask
## If you pass in a transparency, the alpha channel is used as a mask
dirtybackground.paste(cleantxt, (0,0), cleantxt)
# Show it!
dirtybackground.show()
# Save it!
dirtybackground.save("dirtytext.png", "PNG")
Вот выходное изображение: