Introduction:
Small friends all know, just when the entry is required to take their own credentials, this should be a few inches of it, small editor so long have forgotten!!
Yesterday, after work, I had dinner with my old friends. In fact, one of my brothers said to me, “Cousin, you are a programmer now, and there is no way to help me?” I heard, “What’s up? You said”
“My wife just entered a new company a few days ago, but after entering the office, I just know that the photo is to need blue background son, the main is that the photo of the certificate is too far from home, simply ask next you have any way? “
Of course. To save them time, IT’s my duty to do this for only a few minutes.
Many of you are not sure whether the company needs a blue background or a red background, today xiaobi directly take you to do a Python background for a small program, no matter what the background, let’s come!
The body of the
Environmental Installation Part (1) :
Python3.6; Numpy function library; Opencv library.
pip install numpy
pip install opencv-python
Copy the code
Reading and scaling ratio column (2) :
Img = cv2.imread(file)# step1.2: Img = cv2.resize(img, None, fx=1.5, fy=1.5) Rows, cols, channels = img.shapeCopy the code
The three-color picture has RGB three color channels, which cannot be corrosion and expansion operations. This requires us to convert the color picture to HSV grayscale image, and then complete the operation of corrosion and expansion (3) :
Cv2. imshow("original...") HSV = cv2.cvtcolor (img, cv2.color_bgr2HSv)Copy the code
Binarization of images (4) :
Red to blue, set the pixel values within the two thresholds to white (255)
And the pixels that are not in the threshold range are set to black (0)
Array ([255,255,255]) mask = cv2.inrange (HSV, lower_red, upper_red)Copy the code
Image corrosion and expansion, traversing each pixel for color replacement (5):
Step2.3: corrosion expansion if there is still white noise after corrosion expansion, You can add the value of iterations to the report as a result of the report. Go to the bottom of the report as a result of the report. You can go to the bottom of the report as a result of the report. [I,j]=(iterations= 5, iterations= 5, iterations= 5, iterations= 5, iterations= 5, iterations= 5) Img [I,j]=(0,0,255) "for I in range(rows): for j in range(cols): If dilate[I,j]= =255: # if dilate[I,j]= =255Copy the code
Image effect:
Attached to the source code:
#! /usr/bin/env python # -* -coding :utf-8 -* -import cv2 import numpy as np file=r" Img = cv2.resize(img, None, fx=1.5, fy=1.5) rows, cols Imshow ("original...") CvtColor (img, cv2.color_bgr2HSV) # : Binary processing of the image # Red to blue # Set the pixel value within the two thresholds to white (255), # lower_red= Np.array ([0,125,125]) upper_red = Np.array ([255,255,255]) mask = cv2.inrange (HSV, Step2.3: Corrosion expansion If there is still white noise after corrosion expansion, You can add the value of iterations to the report as a result of the report. Go to the bottom of the report as a result of the report. You can go to the bottom of the report as a result of the report. [I,j]=(iterations= 5, iterations= 5, iterations= 5, iterations= 5, iterations= 5, iterations= 5) Img [I,j]=(0,0,255) "for I in range(rows): for j in range(cols): if dilate[i,j] ==255: Img [I,j]=(255,0,0) PNG "cv2.imwrite(new_file, img) res = cv2.imread(new_file) cv2.imshow('result... ', res) # cv2.waitKey(0)Copy the code
conclusion
Anne! Change the small procedure of certificate photo is written. Try changing your own pictures!!
#959755565 welcome to learn and exchange!
Remember sanlian oh ~ love you