current position:Home>Opencv skills | saving pictures in common formats as transparent background pictures (with Python source code) - teach you to easily make logo

Opencv skills | saving pictures in common formats as transparent background pictures (with Python source code) - teach you to easily make logo

2021-08-23 04:48:06 Color Space

Reading guide

This paper mainly introduces the use of OpenCV Method and implementation code of saving common format pictures as transparent background pictures .

Achieve the goal

The objectives of this paper are as follows :

① The common format [jpg/png/bmp] The white background picture is converted and saved as a transparent background picture ;

② The common format [jpg/png/bmp] Complex background image is converted and saved as transparent background image .

Implementation steps and detailed demonstration

Implementation steps :

① Load pictures in color mode ;

② The image by BGR The color space is converted to BGRA Color space ;

③ Corresponding the pixel value of the white position in the original image A All channels are set to 0;

④ Save the processed image as PNG Format .

Code implementation and demonstration :

The image to be processed :

Processing result image :

Compare carefully to see the difference ( White background and transparent background ):

Alpha Channel processing results ( The white part is retained , The black part is finally a transparent background ):

Try another picture :

Python-OpenCV Implementation code :

import cv2
import numpy as np

img = cv2.imread("opencv.jpg")
cv2.imshow('src', img)
print(img.shape)

result = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)

for i in range(0,img.shape[0]): # Access all rows 
    for j in range(0,img.shape[1]): # Access all columns 
        if img[i,j,0] > 200 and img[i,j,1] > 200 and img[i,j,2] > 200:
            result[i,j,3] = 0

cv2.imwrite('result.png', result, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
print(result.shape)
cv2.imshow('result', result)
B,G,R,A = cv2.split(result)
cv2.imshow('B', B)
cv2.imshow('G', G)
cv2.imshow('R', R)
cv2.imshow('A', A)
 
cv2.waitKey()
cv2.destroyAllWindows()

What if the background of the picture is a little more complex ? All change is the same , Just put the part you want to keep Alpha The gray value of the corresponding part of the channel changes to 255, What you don't want to keep Alpha The gray value of the corresponding part of the channel changes to 0, And save it as PNG Just pictures .

Take the following picture as an example :

The goal is to extract the middle part of the flower , Then it is processed into a transparent background . Extracting flowers can transform the R After the channel threshold processing, it is directly used as Alpah Just the passage .

R Channel separation effect :

Binary effect :

Code implementation and final results :

import cv2
import numpy as np

img = cv2.imread("flower.jpg")
cv2.imshow('src', img)
print(img.shape)

result = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)

B,G,R = cv2.split(img)

_, Alpha= cv2.threshold(R, 200, 255, cv2.THRESH_BINARY)
cv2.imshow('thres', Alpha)

B2,G2,R2,A2 = cv2.split(result)
A2 = Alpha
result = cv2.merge([B2,G2,R2,A2]) # Channel merging 

cv2.imwrite('result.png', result)
print(result.shape)
cv2.imshow('result', result)
B,G,R,A = cv2.split(result)
cv2.imshow('B', B)
cv2.imshow('G', G)
cv2.imshow('R', R)
cv2.imshow('A', A)
 
cv2.waitKey()
cv2.destroyAllWindows()

This article is from WeChat official account. - OpenCV And AI Deep learning (OpenCV_AI_DL) , author :Color Space

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .

Original publication time : 2021-08-07

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

copyright notice
author[Color Space],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2021/08/20210823044803156v.html

Random recommended