How to merge a transparent png image with another image using PIL
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Pillow (PIL Fork) merges a transparent PNG overlay onto a background image using Image.paste() with the alpha channel as a mask, or Image.alpha_composite() for full alpha blending. The key is using the overlay's alpha channel (transparency information) so that transparent pixels show the background through. Without the mask parameter, paste() replaces pixels entirely, ignoring transparency.
Basic Overlay with paste()
The third argument mask=overlay tells Pillow to use the overlay's alpha channel as the transparency mask. Without it, transparent pixels would be pasted as opaque.
Using alpha_composite() (Full Alpha Blending)
alpha_composite() handles semi-transparent pixels (alpha values between 0 and 255) correctly, while paste() with a mask treats pixels as either fully transparent or fully opaque.
Centering the Overlay
Resizing the Overlay
Adjusting Overlay Opacity
Or create a uniform semi-transparent overlay:
Tiling an Overlay (Watermark Pattern)
Batch Processing
Common Pitfalls
- Forgetting to convert to RGBA:
Image.open("photo.jpg")opens as RGB (no alpha channel). Both the background and overlay must be converted to RGBA with.convert("RGBA")before compositing, otherwisealpha_composite()raises a ValueError. - Not passing the mask parameter to paste():
background.paste(overlay, (x, y))withoutmask=overlayreplaces pixels entirely, making transparent areas appear as black or white rectangles instead of showing the background through. - Overlay larger than background: Pasting an overlay that extends beyond the background boundaries silently clips the overlay. Check sizes with
overlay.sizeand resize or reposition if needed. - Saving RGBA to JPEG format: JPEG does not support transparency. Saving an RGBA image as JPEG either raises an error or drops the alpha channel. Convert to RGB with
.convert("RGB")before saving as JPEG, or save as PNG to preserve transparency. - Using paste() instead of alpha_composite() for semi-transparent overlays:
paste()with a mask treats each pixel as either transparent or opaque (binary mask). For smooth semi-transparent blending (like glass effects or gradient overlays), usealpha_composite()which properly handles alpha values between 0 and 255.
Summary
- Use
Image.paste(overlay, position, mask=overlay)for simple overlay with transparency - Use
Image.alpha_composite(background, temp)for correct semi-transparent blending - Both images must be in RGBA mode — convert with
.convert("RGBA") - Adjust overlay opacity by modifying the alpha channel with
ImageEnhance.Brightnessorpoint() - Convert to RGB before saving as JPEG since JPEG does not support transparency
- Use
Image.LANCZOSfor high-quality resizing of overlays
Related reading
- How to obtain filenames during prediction while using tf.keras.preprocessing.image_dataset_from_directory?
- How to perform tf.image.per_image_standardization on a batch of images in tensorflow
- How to Plot and save a tensor as an image in Tensorflow
- How to properly Multithread in OpenCV in 2019?
- How to merge lists into a list of tuples?
- How to merge multiple dataframes
- How to merge master branch into main
- How to merge remote changes at GitHub?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.