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
The sidebar is recommended
- [Python introduction project] use Python to generate QR code
- Compile D + +, and use d to call C from python
- Quickly build Django blog based on function calculation
- Python collects and monitors system data -- psutil
- Finally, this Python import guide has been sorted out. Look!
- Quickly build Django blog based on function calculation
- Python interface test unittest usage details
- Implementation of top-level design pattern in Python
- You can easily get started with Excel. Python data analysis package pandas (VII): breakdown
- Python simulation random coin toss (non optimized version)
guess what you like
-
Python tiktok 5000+ V, and found that everyone love this video.
-
Using linear systems in python with scipy.linalg
-
Using linear systems in python with scipy.linalg
-
Together with Python to do a license plate automatic recognition system, fun and practical!
-
You can easily get started with Excel. Python data analysis package pandas (XI): segment matching
-
Advanced practical case: Javascript confusion of Python anti crawling
-
Using linear systems in python with scipy.linalg
-
Fast power modulus Python implementation of large numbers
-
Quickly build Django blog based on function calculation
-
This paper clarifies the chaotic switching operation and elegant derivation of Python
Random recommended
- You can easily get started with Excel pandas (I): filtering function
- You can easily get started with Excel. Python data analysis package pandas (II): advanced filtering (I)
- You can easily get started with Excel. Python data analysis package pandas (2): advanced filtering (2)
- You can easily get started with Excel. Python data analysis package pandas (3): making score bar
- Test Development: self study Dubbo + Python experience summary and sharing
- You can easily get started with Excel. Python data analysis package pandas (V): duplicate value processing
- How does Python correctly call jar package encryption to get the encrypted value?
- Python 3 interview question: give an array. If there is 0 in the array, add a 0 after 0, and the overall array length remains the same
- Python simple Snake game (single player mode)
- Using linear systems in python with scipy.linalg
- Python executes functions and even code through strings! Come and understand the operation of such a top!
- Decoding the verification code of Taobao slider with Python + selenium, the road of information security
- [Python introduction project] use Python to generate QR code
- Vanessa basks in her photos and gets caught up in the golden python. There are highlights in the accompanying text. She can't forget Kobe after all
- [windows] Python installation pyteseract
- [introduction to Python project] create bar chart animation in Python
- Fundamentals of Python I
- Python series tutorials 116
- Python code reading (chapter 35): fully (deeply) expand nested lists
- Practical series 1 ️⃣ Wechat applet automatic testing practice (with Python source code)
- Python Basics: do you know how to use lists?
- Solution of no Python 3.9 installation was detected when uninstalling Python
- [Python homework] coupling network information dissemination
- [common links of Python & Python]
- Python application software development tool - tkinterdesigner v1.0 5.1 release!
- [Python development tool tkinterdiesigner]: example: develop stock monitoring alarm using Tkinter desinger
- [Python development tool Tkinter designer]: Lecture 2: introduction to Tkinter designer's example project
- [Python development tool Tkinter designer]: Lecture 1: introduction to the basic functions of Tkinter Designer
- [introduction to Python tutorial] use Python 3 to teach you how to extract any HTML main content
- Python socket implements UDP server and client
- Python socket implements TCP server and client
- leetcode 1261. Find Elements in a Contaminated Binary Tree(python)
- [algorithm learning] 1486 Array XOR operation (Java / C / C + + / Python / go / trust)
- leetcode 1974. Minimum Time to Type Word Using Special Typewriter(python)
- The mobile phone uses Python to operate picture files
- [learning notes] Python exception handling try except...
- Two methods of using pandas to read poorly structured excel. You're welcome to take them away
- Python sum (): the summation method of Python
- Practical experience sharing: use pyo3 to build your Python module
- Using Python to realize multitasking process