Python implements changing the background color of photos (with code)

不言
Release: 2019-03-21 15:58:51
forward
6081 people have browsed it

The content of this article is about the Python implementation of changing the background color of photos (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. helped.

Nowadays, there are many web-based tools for changing the background color online. How do they do this? In fact, it can be achieved using Python.

Environmental requirements

Python3
numpy function library
opencv library

Installation

Download the appropriate version of numpy function library, my computer is WIN10 64-bit, the installed function library is
numpy-1.13.1 mkl-cp36-cp36m-win_amd64.whl

Download the appropriate version of the numpy function library, my computer is WIN10 64-bit, the installed function The library is
opencv_python-3.3.1-cp36-cp36m-win_amd64.whl

Install the above two libraries, WIN R->CMD->CD to the folder where the current two libraries are located, Enter the following commands to install step by step

pip install numpy-1.13.1+mkl-cp36-cp36m-win_amd64.whl
pip install opencv_python-3.3.1-cp36-cp36m-win_amd64.whl
Copy after login

Download address of the above two files: https://download.csdn.net/dow...

Python implements changing the background color of photos (with code)

Code

Create a new opencv.py in the current folder and copy the following code

import cv2
import  numpy as np

img=cv2.imread('timg.jpg')
#缩放
rows,cols,channels = img.shape
img=cv2.resize(img,None,fx=0.5,fy=0.5)
rows,cols,channels = img.shape
cv2.imshow('img',img)

#转换hsv
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower_blue=np.array([90,70,70])
upper_blue=np.array([110,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
cv2.imshow('Mask', mask)

#腐蚀膨胀
erode=cv2.erode(mask,None,iterations=1)
cv2.imshow('erode',erode)
dilate=cv2.dilate(erode,None,iterations=1)
cv2.imshow('dilate',dilate)

#遍历替换
for i in range(rows):
    for j in range(cols):
        if dilate[i,j]==255:
            img[i,j]=(0,0,255)#此处替换颜色,为BGR通道
cv2.imshow('res',img)

cv2.waitKey(0)
cv2.destroyAllWindows()
Copy after login

Explanation

The fourth line of timg.jpg is The three values ​​​​in the original image path
lower_blue=np.array([90,70,70]) can control the processed effect and noise

WIN R->CMD->CD to opencv.py folder, run

This article is all over here, for more other exciting content, you can pay attention to the python tutorial video column of the PHP Chinese website!

The above is the detailed content of Python implements changing the background color of photos (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template