Use Python to make a large number of sketches of young ladies!

Sketch as a nearly perfect expression technique has its own unique charm. With the development of digital technology, sketch is no longer the patent of professional painters. Today, this article will talk about how to use python to obtain Miss Sister's sketch in batches. portrait. The article is divided into two parts:

  • The first part introduces two ideas of using python to generate sketches

  • The second part introduces how to obtain sketches in batches

 

1

 

Two ideas for obtaining sketch map

 

The two ideas introduced in this section are based on opencv and do not involve deep learning related content. The basic idea is to read in a photo map and transform it into a sketch map through various transformations. For the convenience of demonstration, let's first find a picture of a young lady as the experimental material.

 

1) Comic style

Let me talk about the first method first. The core idea of ​​this method is to use a technique called "thresholding", which is based on the grayscale difference between the object and the background in the image, and the pixel-level segmentation .

If you want to convert a picture into a sketch that only presents black and white, you need to perform a binarization operation on it. Opencv provides two binarization operations: threshold() and adaptiveThreshold(). Compared with threshold(), adaptiveThreshold() can automatically adjust locally according to the brightness distribution of different areas of the image, so it is called adaptive binarization. The following picture is the effect of binarizing the color image.

The concepts mentioned above may be rather obscure, and it doesn’t matter if you don’t understand it. Let’s focus on how to perform the actual operation.

The first step is to read in the picture and convert it to grayscale. This step is considered a routine operation. I believe students who have used opencv have written similar codes.

img_rgb = cv2.imread(src_image)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)

The second step is to use the adaptiveThreshold() method to binarize the picture. The parameters in the function are mostly used to set the adaptive binarization algorithm and threshold.

img_edge = cv2.adaptiveThreshold(img_gray, 255,
                                 cv2.ADAPTIVE_THRESH_MEAN_C,
                                 cv2.THRESH_BINARY, blockSize=3, C=2)

The third step is to save the converted picture

cv2.imwrite(dst_image, img_edge)

After the above steps, we got a new black and white picture, let's take a look at the converted picture effect.

Judging from the converted picture, although there is no problem with the outline, the effect is very unsatisfactory and cannot be called a sketch. This is mainly because adaptiveThreshold() will perform a binarization operation in each small local area of ​​the picture. Therefore, for some pictures with higher definition and finer color distinction, the above dense situation will appear.

This problem is actually very simple to solve, just add the following line of code to blur the original image before binarization.

img_gray = cv2.medianBlur(img_gray, 5)

Let's take a look at the sketch map (below) generated this time. Does it look more comfortable and has a hand-drawn comic feel.

 

2) Realistic style

Through the above method, although a pretty good sketch is finally obtained, it looks somewhat "distorted". In order to obtain a more realistic sketch, we try another method.

The core idea of ​​this method is to obtain some of the more important lines in the original image through "film fusion". The specific implementation steps are as follows:

The first step is the same as the above method, using opencv to read the picture and generate a grayscale image.

The second step is to blur the grayscale image. After experimentation, using the above-mentioned median filter function cv2.medianBlur() for blurring operation, the final sketch effect is not good. Here we try to use Gaussian filter to blur the image. The code is as follows:

img_blur = cv2.GaussianBlur(img_gray, ksize=(21, 21),
                            sigmaX=0, sigmaY=0)

Among them, the parameter ksize represents the size of the Gaussian kernel, and sigmaX and sigmaY represent the standard deviation of the Gaussian kernel in the X and Y directions, respectively.

The third step is to use the cv2.divide() method to fuse the original image and the blurred image. In essence, cv2.divide() performs a pixel-level division operation of the two images, and the result obtained can be simply understood as two images There are obvious differences between the parts. Look at the code:

cv2.divide(img_gray, img_blur, scale=255)

 

The fourth step is to save the generated picture. The code is the same as in the previous method. Let's directly look at the obtained sketch effect.

From the results, the lines of the sketch map obtained by this method are more delicate and the sketch effect is better.

 

 

2

 

Get Miss Sister Sketch Portraits in Batch

 

In this part, we want to realize the function of obtaining the sketch portraits of young lady in batches. Based on the comparison of the two sketch pictures above, the second method is used to realize the conversion from pictures to sketch pictures.

So, the next thing to solve is the problem of image source. Recently, many projects have successfully implemented the operation of obtaining beautiful ladies from Douyin or Zhihu. In fact, there are many websites that can obtain beautiful ladies' pictures in addition to these platforms.

I will not show the specific content of the website in the article. In order to specify the idea of ​​image crawling, I will roughly talk about the page structure: the homepage of the website lists N themes, and each theme page contains M pictures of Miss Sister. The schematic diagram is as follows:

The construction of each page URL is also very clear. For example, the page URL in the figure below is http://www.waxjj.cn/2794.html, where 2794 is the ID number of the theme page. Check the html code of the page (pictured below) and find that each picture is under a <li>label.

 

 

In this case, generally speaking, we can obtain the url of each picture through some kind of parser. However, after careful observation, it is found that only the part of the html code of the entire webpage that involves the image url has a complete http connection, so you can consider using regular expressions to extract the image url. The code to achieve this part of the function is as follows.

In the above code, we extract the ID of the theme page as part of the name of the picture to be saved. In the save_jpg() function, each picture will be converted into a sketch and saved locally.

Since we need to use opencv to perform various operations and conversions on the captured images, the images obtained using requests must be saved locally, and then re-read with opencv. Based on the above ideas, we built the save_jpg() function as shown below, where the rgb_to_sketch() function is an encapsulation of the second method of obtaining sketches mentioned in the first part above.

In the main function, we only need to specify the id number of the theme page we want to get, and build a list of URLs:

def main():
    idlist = ['id1', 'id2']
    urllist = ['http://www.waxjj.cn/'+x+'.html' for x in idlist]
    jpgurls = get_jpg_urls(urllist)

 

The above is the complete code, let's take a look at the effect after running~~

 

In fact, programmers want to prevent hair loss programmers are still worried about hair loss , I think that we should exercise more, less late at night, of course, to see more seductive little sister is not good!

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/109030981