Removing Black Background and Make Transparent using Python OpenCV
Last Updated :
03 Jan, 2023
In this article, we will discuss how to remove the black background and make it transparent in Python OpenCV.
cv2.cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV.
Syntax: cv2.cvtColor(src, code[, dst[, dstCn]])
Parameters:
- src: Image
- code: It is the color space conversion code.
- dst: (optional), It is the output image of the same size and depth as src image.
- dstCn: (optional),It is the number of channels in the destination image
Return Value: It returns an image.
Image used for demonstration:
 Stepwise Implementation:
Step 1: First of all, import the library OpenCV.
import cv2
Step 2: Now, import the image from your computer.
file_name = "#Image-Location"
Step 3: Then, read the image in OpenCV.
src = cv2.imread(file_name, 1)
Step 4: Then, convert the image background to gray image background.
tmp = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
Step 5: Moreover, apply the thresholding technique.
_,alpha = cv2.threshold(tmp,0,255,cv2.THRESH_BINARY)
Step 6: Further, use cv2.split() to split channels of colored image.
b, g, r = cv2.split(src)
Step 7: Later on, make a list of Red, Green, and Blue Channels and alpha.
rgba = [b,g,r, alpha]
Step 8: Next, use cv2.merge() to merge rgba into a coloured/multi-channeled image.
dst = cv2.merge(rgba,4)
Step 9: Finally, write and save to a new image location.
cv2.imwrite("#New-Image-Location", dst)
Below is the complete implementation:
Python3
# Import the library OpenCV
import cv2
# Import the image
file_name = "gfg_black.png"
# Read the image
src = cv2.imread(file_name, 1)
# Convert image to image gray
tmp = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# Applying thresholding technique
_, alpha = cv2.threshold(tmp, 0, 255, cv2.THRESH_BINARY)
# Using cv2.split() to split channels
# of coloured image
b, g, r = cv2.split(src)
# Making list of Red, Green, Blue
# Channels and alpha
rgba = [b, g, r, alpha]
# Using cv2.merge() to merge rgba
# into a coloured/multi-channeled image
dst = cv2.merge(rgba, 4)
# Writing and saving to a new image
cv2.imwrite("gfg_white.png", dst)
Output:
Â
Similar Reads
How to make background image transparent using Python? In this article, the task is to create a background transparent of the image in Python Library Required : First Install pillow library on your Python Application before going ahead. Python Imaging Library is a free and open-source additional library for the Python programming language that adds supp
1 min read
How to remove the Background from an image using Python? In this article, we'll learn how to remove the background of an image using Python. Pillow module: The Pillow library, a derivative of the Python Imaging Library (PIL), aids in giving Python interpreter image processing capabilities. This library offers a wide range of file format support, a product
2 min read
Python | Background subtraction using OpenCV Background Subtraction has several use cases in everyday life, It is being used for object segmentation, security enhancement, pedestrian tracking, counting the number of visitors, number of vehicles in traffic etc. It is able to learn and identify the foreground mask.As the name suggests, it is abl
2 min read
How to make Background Transparent in Canva? Canva is a popular designing tool that is available in the form of application on mobile and web browser on desktops. It is used for designing posters and performing graphic design related stuff. In this article, we will learn how to make background transparent in Canva in order to improve our desig
4 min read
Negative transformation of an image using Python and OpenCV Image is also known as a set of pixels. When we store an image in computers or digitally, itâs corresponding pixel values are stored. So, when we read an image to a variable using OpenCV in Python, the variable stores the pixel values of the image. When we try to negatively transform an image, the b
4 min read