Paste png with alpha channel as transparent image with Python / OpenCV

TL; DR

This is the code for overlaying png_image.png (image with alpha channel) on the upper left of bg.jpg.

import cv2

frame = cv2.imread("bg.jpg ")
png_image = cv2.imread("alpha.png ", cv2.IMREAD_UNCHANGED)  #Read with alpha channel included

#Setting the paste destination coordinates. For the time being, in the upper left
x1, y1, x2, y2 = 0, 0, png_image.shape[1], png_image.shape[0]

#Synthetic!
frame[y1:y2, x1:x2] = frame[y1:y2, x1:x2] * (1 - png_image[:, :, 3:] / 255) + \
                      png_image[:, :, :3] * (png_image[:, :, 3:] / 255)

Brief commentary

The PNG file contains data that represents the transparency of each pixel called the "alpha channel". The range is 0-255, which is the same as RGB. A value of 255 is 100% valid, and a value of 0 is 0% (fully transparent).

png_image = cv2.imread("alpha.png ", cv2.IMREAD_UNCHANGED)  #Read with alpha channel included

In normal cv2.imread (), it will be in the form of numpy.ndarray of[h, w, 3], but specify cv2.IMREAD_UNCHANGED to specify cv2.imread (). When called, it takes the form [h, w, 4]. BGR is BGRA and ends with an alpha channel.

After loading the image, combine it. However, it can be synthesized by the usual NuPy matrix operation. What we are doing is allocating the original background image and the image to be drawn by the numerical value of the alpha channel and adding them together.

frame[y1:y2, x1:x2] = frame[y1:y2, x1:x2] * (1 - png_image[:, :, 3:] / 255) + \
                      png_image[:, :, :3] * (png_image[:, :, 3:] / 255)

png_image [:,:, 3:] is the alpha channel retrieval. The range of the alpha channel is 0-255, so divide by 255 to get a ratio of 0-1. You can get the final image by multiplying the image to be drawn by the calculated ratio, multiplying the background by the "remaining" of the ratio, and adding them together.

By the way, if you write png_image [:,:, 3], you will get angry if the size of the matrix does not match (I made a mistake).

Example

bg.jpg bg_moon_sky_mangetsu_building.jpg

alpha.png stand_naname3_man.png

The result of cv2.imwrite ("result.jpg ", frame) testout.jpg

Extra

Although there are various useful sites, I couldn't find the code that was just right, so I made an article. (Maybe it's too obvious to write it instead)

Site that I was allowed to refer to

-Display png with alpha channel in OpenCV -Qiita -Python, OpenCV, NumPy for alpha blending and masking images \ | note \ .nkmk \ .me

For the image, I used the material of Cute free material collection Irasutoya.

Recommended Posts

Paste png with alpha channel as transparent image with Python / OpenCV
Image editing with python OpenCV
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
Find image similarity with Python + OpenCV
Image processing with Python & OpenCV [Tone Curve]
Image acquisition from camera with Python + OpenCV
Light image processing with Python x OpenCV
I tried "smoothing" the image with Python + OpenCV
Convert PDF to image (JPEG / PNG) with Python
I tried "differentiating" the image with Python + OpenCV
How to crop an image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
[Small story] Test image generation with Python / OpenCV
[Python + OpenCV] Whiten the transparent part of the image
Binarization with OpenCV / Python
Image processing with Python
JPEG image generation by specifying quality with Python + OpenCV
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
Image processing with Python (Part 2)
I tried "gamma correction" of the image with Python + OpenCV
"Apple processing" with OpenCV3 + Python3
Camera capture with Python + OpenCV
Sorting image files with Python (2)
Sorting image files with Python (3)
Image processing with Python (Part 1)
Tweet with image in Python
Grayscale image is displayed as a color image in OpenCV / Python
Sorting image files with Python
Image processing with Python (Part 3)
Face detection with Python + OpenCV
Get image features with OpenCV
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
[Python] Easy reading of serial number image files with OpenCV
[Python] Image processing with scikit-image
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
I tried to make an image similarity function with Python + OpenCV
Shining life with Python and OpenCV
Cut out an image with python
Real-time image processing basics with opencv
Neural network with OpenCV 3 and Python 3
Image processing with Python 100 knocks # 3 Binarization
Easy Python + OpenCV programming with Canopy
Let's do image scraping with Python
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Load gif images with Python + OpenCV
Try blurring the image with opencv2
Use OpenCV with Python 3 in Window
Image processing with Python 100 knocks # 2 Grayscale
Draw an illustration with Python + OpenCV
Introduction to image analysis opencv python
Track baseball balls with Python + OpenCV
Graph Based Segmentation with Python + OpenCV
Send image with python, save with php
Draw arrows (vectors) with opencv / python
Basic study of OpenCV with Python
Gradation image generation with Python [1] | np.linspace
How to crop the lower right part of the image with Python OpenCV