Akik Look at that coder

Public account: Look at that code farmer

1. Project Introduction

Everyone has a love of beauty, I think we all use mobile phone software to photocopy their photos,

This case is about how to use Python to make cartoon filters for photos

2. Configure the project

  • Python3.x
  • Opencv library
  • Numpy library

3. Project process

1. Add cartoon animation filter function

The most critical step in the project was to write a function to add a cartoon filter,

Here we can call this function filter(), which takes two arguments,

  • The first parameter is the path of the input image
  • Name of the second picture

2. Use OpencV library for photo processing

In this function, we need to use the imread() method of OpencV library to read the source image, and convert it to gray blur effect, then enhance the edge, adjust the brightness and contrast, and finally convert the gray image to color, and save the image, so as to realize the addition of filters

3. Project code

import cv2
import os
import numpy asnp def filter(filein,picture_name): ImgI_filename =os.path.join(filein,picture_name) # imgO_filename =os.path.join(filein,picture_name)"E:/photo2/". Img_gray = cv2.cvtcolor (img_rgb, cv2.color_rGB2gray) picture_name # Add blur effect. Img_blur = cv2. MedianBlur (img_gray,5Img_edge = cv2. AdaptiveThreshold (img_blur,128,
        cv2.ADAPTIVE_THRESH_MEAN_C,
        cv2.THRESH_BINARY,
        blockSize=9,
        C=8Img_edge = cv2.cvtcolor (IMg_edge, cv2.color_gray2bGR) # Img_Cartoon = Cv2.bitwise_and (img_rgb,img_edge) # Adjust brightness and contrast2.0*img_cartoon+16),0.255Cv2. imwrite(imgO_filename,res)if __name__=='__main__':
    path="E:/photo/"
    imagelist=[]
    for filename in os.listdir(path):
        imagelist.append(filename)
        print(filename)
        filter(path,filename)
Copy the code

4. Original drawing of experiment

5. Experimental results

4. Summary of project thinking

The project is based on Python language, using Opencv library and NUMpy library for photo processing, and the final output. From this project can learn the general principle of Meitu software, at the same time through the case of this project, can be extended to achieve other filters, such as sketch filter and so on.

Although this project is simple, but the realization of Opencv library image processing operations, to a certain extent, let Python users deepen the understanding of Opencv library, for further learning Python other libraries laid a good foundation.

If you find this helpful:

1. Click “like” to support it, so that more people can see this article

2, pay attention to the public number: look at that code farmers, we study together and progress together.

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign