[Python + OpenCV] Whiten the transparent part of the image

Introduction

This is a sample program that paints the transparent part of the image white. When I looked it up on the net, I found some ways to scan pixels with a for statement and to combine alpha channels after getting RGB. I was able to implement it in a few lines without doing such complicated processing, so I will leave it in the article as a memo for myself.

environment

Execution example

Paint the following transparent image in black (because you cannot see the change if it is white). output.png

Source code

 -*- coding:utf-8 -*-
import cv2
import numpy as np

# Read the input image (also read the alpha channel by specifying -1)
img = cv2.imread("kangaru.png ", -1)
# Get the index where the alpha channel is 0
 ex) ([0, 1, 3, 3, ...],[2, 4, 55, 66, ...])
# It is a tuple (length 2) in which column and row are stored respectively.
index = np.where(img[:, :, 3] == 0)
# Paint white
img[index] = [255, 255, 255, 255]
# output
cv2.imwrite("output.png ", img)

in conclusion

You could easily acquire the alpha channel and paint the transparent part white.

Recommended Posts

[Python + OpenCV] Whiten the transparent part of the image
How to crop the lower right part of the image with Python OpenCV
I tried "gamma correction" of the image with Python + OpenCV
I tried "smoothing" the image with Python + OpenCV
[Python] Read the source code of Bottle Part 2
I tried "differentiating" the image with Python + OpenCV
the zen of Python
[Python] Read the source code of Bottle Part 1
Image processing? The story of starting Python for
I tried "binarizing" the image with Python + OpenCV
I tried using the image filter of OpenCV
Use Pillow to make the image transparent and overlay only part of it
Extract the table of image files with OneDrive & Python
Explanation of the concept of regression analysis using python Part 2
[OpenCV / Python] I tried image analysis of cells with OpenCV
Explanation of the concept of regression analysis using Python Part 1
Rotate and scale the image before cropping [python] [OpenCV]
Python OpenCV tried to display the image in text.
Image processing with Python (Part 2)
Towards the retirement of Python2
Image editing with python OpenCV
About the ease of Python
Image processing with Python (Part 1)
Image processing with Python (Part 3)
About the features of Python
The Power of Pandas: Python
I tried to find the entropy of the image with python
I made a dot picture of the image of Irasutoya. (part1)
Paste png with alpha channel as transparent image with Python / OpenCV
I made a dot picture of the image of Irasutoya. (part2)
Estimate the attitude of AR markers with Python + OpenCV + drone
[Python] Easy reading of serial number image files with OpenCV
I made a function to crop the image of python openCV, so please use it.
Environment construction of python and opencv
Basics of Python x GIS (Part 3)
The story of Python and the story of NaN
Image capture of firefox using python
[Python] Using OpenCV with Python (Image Filtering)
[Python] The stumbling block of import
Judgment of backlit image using OpenCV
[Python] Using OpenCV with Python (Image transformation)
Existence from the viewpoint of Python
Change the Python version of Homebrew
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
Find image similarity with Python + OpenCV
Try blurring the image with opencv2
Introduction to image analysis opencv python
About the basics list of Python basics
Basic study of OpenCV with Python
[OpenCV; Python] Summary of findcontours function
Learn the basics of Python ① Beginners
Wrap (part of) the AtCoder Library in Cython for use in Python
Color extraction with Python + OpenCV solved the mystery of the green background
[Python] Try to graph from the image of Ring Fit [OCR]
I just erased the object using image repair (inpaint) (OpenCV: Python)
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
Change the length of Python csv strings
Check the behavior of destructor in Python