New year’s benefits: Python easy to gather five blessings (Demo)

Time:2020-5-28

The new year is coming again, and every big company has sent a new year’s blessing to everyone at the end of this year. Alipay has continued the tradition of the past few years. At the end of the year, it has brought five activities to everyone. In order that everyone can get rid of the big “better fortune” faster and better, this day, we will lead you to use Python to make a little demo of “blessing” to “N”.

First look at the renderings

First of all, we need to get a big picture of “Fu”. Then, we can operate on this picture. What we want to achieve today is to transform a “Fu” character into five different styles, and present the renderings for you.

Gray scale

First of all, I will show you the gray-scale image. The gray-scale image is to transform the RGB three channel color image into a single channel gray image. The RGB three channel color is added by the proportion coefficient. The procedure is as follows.


def Gray_fu(self):
 gray_img = cv2.cvtColor(self.ori_img, cv2.COLOR_BGR2GRAY)
 return gray_img

Outline drawing

Next is the contour map. The contour map needs to input a gray-scale image. First, filter the image to remove the noise, and then detect the edge for the points in the gray image where the pixels suddenly change. What is the edge suddenly change? Refer to our gray-scale map. In the gray-scale map, there is a huge difference between the edge of the “Fu” word and the background color, which is the difference It is the basis for finding the edge outline, and the procedure is shown in the figure below.


def Canny_fn(self):
 img = cv2.cvtColor(self.ori_img, cv2.COLOR_BGR2GRAY)
 img = cv2.GaussianBlur(img, (3, 3), 0)
 canny_img = cv2.Canny(img, 50, 150)
 return canny_img

Antiphromatic graph

It is not difficult to understand the inverse color map, that is to subtract every pixel value in the original image by 255, and then we get our inverse color map. The procedure is as follows:


def invert_fu(self):
 img = 255 - self.ori_img
 return img

Corrosion map

For the corrosion map, because in the image we selected, red is the background, and black is the blessing character, so expanding red means corroding black. This is also a built-in feature of OpenCV. We just need to call the expansion function in OpenCV.


def dilate_fu(self):
 # img = cv2.cvtColor(self.ori_img, cv2.COLOR_BGR2GRAY)
 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
 img = cv2.dilate(self.ori_img, kernel, iterations=1)
 print(img.shape)
 return img

Fodoga

Finally, our “Fu Jia”, we just need to rotate the word Fu.


def rotate_fu(self):
 img = cv2.rotate(self.ori_img, 1)
 return img

Full code

import cv2
class Change_pic():
 def __init__(self, img):
  self.ori_img = cv2.imread(img)
 def Gray_fu(self):
  '' grayscale ''
  gray_img = cv2.cvtColor(self.ori_img, cv2.COLOR_BGR2GRAY)
  return gray_img
 def Canny_fn(self):
  '' outline ''
  img = cv2.cvtColor(self.ori_img, cv2.COLOR_BGR2GRAY)
  img = cv2.GaussianBlur(img, (3, 3), 0)
  canny_img = cv2.Canny(img, 50, 150)
  return canny_img
 def invert_fu(self):
  '' reversed color chart ''
  img = 255 - self.ori_img
  return img
 def dilate_fu(self):
  '' corrosion chart ''
  # img = cv2.cvtColor(self.ori_img, cv2.COLOR_BGR2GRAY)
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
  img = cv2.dilate(self.ori_img, kernel, iterations=1)
  print(img.shape)
  return img
 def rotate_fu(self):
  '' lucky home '
  img = cv2.rotate(self.ori_img, 1)
  return img

summary

The above is the new year’s benefits introduced to you by Xiaobian. Python is easy to gather all the five blessings. I hope it can help you. If you have any questions, please leave a message to me, and Xiaobian will reply to you in time. Thank you very much for your support of the developepaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!