[PYTHON] Use Pillow to make the image transparent and overlay only part of it

Introduction

I need to overlay only a part of the image in my work, so I investigated the method, so I will leave it as a memo.

At first I was planning to use opencv and stack it, but while I was investigating, it seemed easier to use Pillow, so I switched to that method.

The images used are as follows test1.png  test2.png

Transparent image

First, the image is transparent. If you search for this, it will come up quite a bit, so I think you can do it right away.

The implementation is as follows.

from PIL import Image

im1 = Image.open("test1.png ")
im2 = Image.open("test2.png ")

im1.putalpha(128)
im2.putalpha(128)

What you are doing

  1. Load the image with ʻImage.open ("filename") `
  2. Transparent with putalpha (alpha)

Overlay images

Next, the transparent image is overlaid. Create an original image when overlaying, and overlay the transparent image as if you were pasting it.

The implementation is as follows.


bg = Image.new("RGBA", (1000, 1000), (255, 255, 255, 0))

bg.paste(im1, (200, 200), im1)
bg.paste(im2, (400, 400), im2)

bg.save("join.png ")

What you are doing

  1. Use ʻImage.new ()` to create a base image for overlaying images.
  2. Use paste () to overlay the transparent image on the base image.
  3. Save the image with save ().

The saved image is below.

You can see that the transparent images are superimposed in a nice way!

Recommended Posts

Use Pillow to make the image transparent and overlay only part of it
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
[Python + OpenCV] Whiten the transparent part of the image
How to use Decorator in Django and how to make it
How to save the feature point information of an image in a file and use it for matching
The background of the characters in the text image is overexposed to make it easier to read.
I want to use only the normalization process of SudachiPy
Extract only the sound of a specific instrument from a MIDI file and make it a separate file
Return the image data with Flask of Python and draw it to the canvas element of HTML
I made a function to crop the image of python openCV, so please use it.
Convert the result of python optparse to dict and utilize it
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
Use Cloud Dataflow to dynamically change the destination according to the value of the data and save it in GCS
Start the webcam to take a still image and save it locally
[python] Send the image captured from the webcam to the server and save it
[Minimum configuration] Use the pillow module of python to synthesize the characters "100 yen" with the image of tapioca milk tea.
I wanted to use the find module of Ansible2, but it took some time, so make a note
The tree.plot_tree of scikit-learn was very easy and convenient, so I tried to summarize how to use it easily.
Dispersion with Python's standard library and Pillow Find the RGB standard deviation of the image and determine if it is monochromatic
GAE --With Python, rotate the image based on the rotation information of EXIF and upload it to Cloud Storage.
How to make VS Code aware of the venv environment and its benefits
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Make sure to align the pre-processing at the time of forecast model creation and forecast
Make it easy to specify the time of AWS CloudWatch Events with CDK.
Try installing only the core part of Ubuntu
I tried to correct the keystone of the image
Get only the address part of NIC (eth0)
Data cleansing 3 Use of OpenCV and preprocessing of image data
How to install Cascade detector and how to use it
Use Pandas to write only the specified lines of the data frame to an excel file
[Ruby] How to replace only a part of the string matched by the regular expression?
I tried to make it easy to change the setting of authenticated Proxy on Jupyter
It is difficult to install a green screen, so I cut out only the face and superimposed it on the background image
How to use the grep command and frequent samples
I tried to erase the negative part of Meros
How to use argparse and the difference between optparse
POST the image with json and receive it with flask
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~
Resize the image to the specified size and blacken the margins
The vertical and horizontal axes of the matplotlib histogram are unpleasant, so make it feel good
POST the image selected on the website with multipart / form-data and save it to Amazon S3! !!
(Memo) Until you extract only the part you want from a certain Web page, convert it to a Sphinx page, and print it as a PDF
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
I tried to find the entropy of the image with python
[Part 4] Use Deep Learning to forecast the weather from weather images
Try to make it using GUI and PyQt in Python
[Part 1] Use Deep Learning to forecast the weather from weather images
[Part 3] Use Deep Learning to forecast the weather from weather images
Make the display of Python module exceptions easier to understand
[Introduction to cx_Oracle] (Part 4) Fetch and scroll of result set
You who color the log to make it easier to see
[pyqtgraph] Add region to the graph and link it with the graph region
Extract only the cat part from the cat image (matting / semantic segmentation)
Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
[Python] Summary of how to use split and join functions
I made a dot picture of the image of Irasutoya. (part1)
Comparison of how to use higher-order functions in Python 2 and 3
Hook to the first import of the module and print the module path
Attempt to automatically adjust the speed of time-lapse movies (Part 2)
[Part 2] Use Deep Learning to forecast the weather from weather images
The process of making Python code object-oriented and improving it