What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow

Preface

When I used Pillow to overlay PNG images of different sizes with ʻimg.paste`, the phenomenon that the transparency of the upper layer penetrated to the lower layer and the background was partially lost occurred, so a memo of how to deal with it.

environment

Python 3.8.2 Pillow 7.2 Ubuntu 20.04 on WSL2

Image you want to overlay with the background

Background (bg_cloudy.png) bg_cloudy.png

Image you want to overlay (cloudy.png) cloudy.png

This has a problem in the background

However, if you try to overlap ʻimg with bg.paste`, it will expire.

from PIL import Image

bg = Image.open('bg_cloudy.png').convert('RGBA')
img = Image.open('cloudy.png').convert('RGBA')

bg.paste(img, (232, 412))

bg.save('a.png')

result

The background is also transparent in the transparent part of the cloud icon a.png

approach

This can be avoided by using ʻImage.alpha_composite (bg, img)`. However, since the size of the image of the first argument and the image of the second argument must match, paste it at the specified position on the transparent image with nothing that has the same size as the background, and then overlay it. To do.

from PIL import Image

bg = Image.open('bg_cloudy.png').convert('RGBA')
img = Image.open('cloudy.png').convert('RGBA')

#Generate a transparent image the same size as the background
img_clear = Image.new("RGBA", bg.size, (255, 255, 255, 0))

#Paste on top of transparent image
img_clear.paste(img, (232, 412))

#Overlay
bg = Image.alpha_composite(bg, img_clear)

bg.save('a.png')

result

I was able to synthesize as intended without any problems in the transparent part. a.png

reference

https://www.it-swarm.dev/

Image source

Image of clouds in the background https://www.priga.jp/ Icon cloud image https://github.com/twitter/twemoji

Recommended Posts

What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
Use Pillow to make the image transparent and overlay only part of it
Make a note of what you want to do in the future with Raspberry Pi
[AWS] What to do when the ping command causes a "timeout"
What to do when the value type is ambiguous in Python?
What to do when the result downloaded via scrapy is in English
How to crop the lower right part of the image with Python OpenCV
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
[Introduction to Python] What is the method of repeating with the continue statement?
What to do when a warning message is displayed in pip list
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
[Python] What to do when PEP8 is violated in the process of importing from the directory added to sys.path
What to do when "Something is already running at port 8000" is displayed when the develop command of Gatsby is executed.
What to do when PermissionError of tempfile.mkstemp occurs
[Python + OpenCV] Whiten the transparent part of the image
Example of what to do when the sample script does not work (OpenCV-Python)
How to display in the entire window when setting the background image with tkinter
What to do if (base) is displayed at the beginning of the Mac terminal
The background of the characters in the text image is overexposed to make it easier to read.
curl: (60) What to do when Issuer certificate is invalid.
What to do when is not in the sudoers file.This incident will be reported.
What to do when gdal_merge creates a huge file
What to do when only the window is displayed and nothing is displayed in pygame Note
What to do if the progress bar is not displayed in tqdm of python
It was a little difficult to do flask with the docker version of nginx-unit
[Shell art] Only when it is a multiple of 3 and a number with 3 becomes stupid
What to do if a communication error occurs due to proxy when installing with package management of pip, npm, gem
[Introduction to Python] What is the important "if __name__ =='__main__':" when dealing with modules?
A memo of misunderstanding when trying to load the entire self-made module with Python3
What is a recommend engine? Summary of the types
Two solutions to the problem that it is hard to see the vector field when writing a vector field with quiver () of matplotlib pyplot
The sound of tic disorder at work is ... I managed to do it with the code
When reading an image with SimpleITK, there is a problem if there is Japanese in the path
What to do if the image is not displayed using matplotlib etc. in the Docker container
What to do when a Missing artifact occurs in a jar that is not defined in pom.xml
What seems to be a template of the standard input part of the competition pro in python3
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
[AWS] What to do when you want to pip with Lambda
What to do when Japanese is not displayed on matplotlib
What to do when PyCharm font is strange or garbled
What to do when a Remove Error occurs when updating conda
I made a dot picture of the image of Irasutoya. (part1)
What to do when an error occurs with import _ssl
I made a dot picture of the image of Irasutoya. (part2)
Is there a secret to the frequency of pi numbers?
The image is a slug
What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python
When generating a large number of graphs with matplotlib, I do not want to display the graph on the screen (jupyter environment)
What do you do with configuration management of a server that has implemented Ansible but is already running? I'm hitting the problem
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
[For IT beginners] What to do when the rev command cannot be used with Git Bash
[Django] What to do if the model you want to create has a large number of fields
Gold needle for when it becomes a stone by looking at the formula of image processing
Find the optimal value of a function with a genetic algorithm (Part 2)
[Statistics] Grasp the image of the central limit theorem with a graph
When it is troublesome to copy what you built with vue
What to do when a video cannot be read by cv2.VideoCapture
[Beanstalk] What to do when an error occurs with import uuid
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
What to do when the jupyterlab extension settings are not reflected
[Go] Create a CLI command to change the extension of the image