I'am trying to watermark an image however iam not able to do so without pasting the watermark with its background color. (see below example)
I tried to implement the tutorial from the following website .
As can be seen from below images, the Current outcome shows watermark with its black background which is not desirable. (See expected outcome image for required result)
I'am using Linux/Ubuntu 20.04 with Python 3.8
Here is the content:
Background Image :
Watermark Image :
Current Outcome :
Expected Outcome :
Current Outcome after making watermark transparent :
Below is code i have used which is same as website tutorial example.
#adding transparency to watermark image(as suggested)
from PIL import Image, ImageFilter
img = Image.open('path_to_watermark').convert('RGBA')
img.putalpha(130)
img.save('path_to_watermark')
def watermark_with_transparency(input_image_path, output_image_path, watermark_image_path, position):
base_image = Image.open(input_image_path).convert('RGBA')
watermark = Image.open(watermark_image_path).convert('RGBA')
width, height = base_image.size
transparent = Image.new('RGBA', (width, height), (0,0,0,0))
transparent.paste(base_image, (0,0))
transparent.paste(watermark, position, mask=watermark)
transparent.show()
transparent.save(output_image_path)
if __name__ == '__main__':
img = 'path_to_input_image /home/...'
watermark_with_transparency(img, 'path_to_output', 'path_to_watermark', position=(0,0))
What am I doing incorrectly? Thanks for your help.
You are pasting an Image with no transparency in it. Being a PNG, it doesn't mean it contains transparency, but an alpha layer, in which case, every pixel is equal to 255
There is no need to make it transparent with putalpha, you just need a PNG file with the black area transparent
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.