Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use alpha_composite in pillow?

I try to put a transparent logo on the photo with Pillow:

# First, I convert my photo and watermark to RGBA:
image = Image.open(photo_image_path).convert('RGBA')
watermark = Image.open(watermark_image_path).convert('RGBA')

# then I сreate an empty layer with the same size as image
# and put watermark in x/y position
layer = Image.new('RGBA', image.size, (0, 0, 0, 0))
layer.paste(watermark, (x, y))

# then add transparency 
layer.putalpha(128)

# and merge image with logo
result = Image.alpha_composite(image, layer)

Initially my watermark has a full-transparent background. But I have a result with black half-transparent background on all image size. What am I doing wrong?

like image 246
Konstantin Komissarov Avatar asked Sep 15 '25 06:09

Konstantin Komissarov


1 Answers

I've resolved a problem with this:

image = Image.open(photo_image_path).convert('RGBA')
watermark = Image.open(watermark_image_path).convert('RGBA')
layer = Image.new('RGBA', image.size, (0, 0, 0, 0))
layer.paste(watermark, (x, y))

# Create a copy of the layer
layer2 = layer.copy()

# Put alpha on the copy
layer2.putalpha(128)

# merge layers with mask
layer.paste(layer2, layer)


result = Image.alpha_composite(image, layer)
like image 111
Konstantin Komissarov Avatar answered Sep 17 '25 20:09

Konstantin Komissarov