Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to make a massive sketch of a little sister

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use Python to make a massive sketch of a little sister. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's follow the editor to have a look.

As an almost perfect means of expression, sketch has its 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 sketch portraits of little sisters in batches.

1. Two ideas of obtaining Sketch

The two ideas introduced in this part are based on opencv and do not involve in-depth learning. The basic idea is to read a picture and then convert it into a sketch through various transformations. For the convenience of the demonstration, we first found a picture of the little sister as the experimental material.

1) Cartoon style

Let's start with the first method, the core idea of this method is to use a technique called "thresholding", which is based on the gray difference between the object and the background in the image.

If you want to convert an image into a sketch that shows only black and white, you need to binarize it. Two binarization methods are provided in opencv: threshold () and adaptiveThreshold (). Compared with threshold (), adaptiveThreshold () can adjust locally according to the luminance distribution of different regions of the image, so it is called adaptive binarization. The following picture is the effect of binarization of a color picture.

The concepts mentioned above may be obscure, and it doesn't matter if you don't understand them. Let's focus on how to do it in practice.

The first step is to read the picture and convert it into a grayscale image. This step can be regarded as a routine operation. I believe that students who have used opencv have written similar code.

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, and most of the parameters in the function are 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, Category 2)

The third step is to save the converted picture

Cv2.imwrite (dst_image, img_edge)

After the above steps, we get a new black-and-white image. Let's take a look at the effect of the converted image.

From the converted picture, although there is no problem with the outline, but the effect is not ideal, and can not be called a sketch. This is mainly because adaptiveThreshold () will binarization operation in each small local area of the picture, so for some pictures with high definition and fine color distinction, there will be such a dense situation as above.

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

Img_gray = cv2.medianBlur (img_gray, 5)

Let's take a look at the generated sketch (below) to see if it looks much more comfortable and has a feeling of hand-drawn cartoons.

2) realistic style

Through the above method, although we finally get a fairly good sketch, but it looks somewhat "distorted". In order to get a more realistic-looking sketch, let's try another method.

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

The first step is to use opencv to read the image and generate a grayscale image, as in the above method.

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

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

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. Cv2.divide () is essentially a division operation at the pixel level of the two images, and the result can be simply understood as the part where there are obvious differences between the two images. Take a look at the code:

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

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

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

two。 Obtain sketch portraits of little sister in batch

In this part, we want to achieve the function of obtaining the sketch portraits of the little sister in batches. based on the comparison of the effects of the two sketches mentioned above, we use the second method to realize the conversion from the picture to the sketch.

So, the next thing to solve is the problem of picture source. Recently, many projects have successfully achieved the operation of obtaining beautiful little sisters from Douyin or Zhihu. In fact, in addition to these platforms, there are also many websites that can get pictures of beautiful little sisters.

I will not show the specific content of the website in the article. In order to specify the idea of picture crawling, I will probably talk about the page structure: the home page of the website lists N themes, and each theme page contains M pictures of the little sister. The structure diagram is as follows:

The construction of the url of each page is also clear. For example, the page url in the following figure is http://www.waxjj.cn/2794.html, where 2794 is the ID number of the topic page. Look at the html code of the page (below) and find that each picture is under a tag.

In this case, generally speaking, we can get the url of each image through some kind of parser. However, after careful observation, it is found that only the part of the html code that involves the picture url has a complete http connection, so you can consider using regular expressions to extract the picture url. The code to achieve this 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, and the save_jpg () function converts each picture into a sketch and saves it locally.

Because we want to use opencv to perform various operations 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 construct the save_jpg () function shown below, where the rgb_to_sketch () function encapsulates the second sketch acquisition method mentioned in the first part above.

In the main function, we only need to specify the id number of the topic page we want to get, and build a set of url lists:

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, if programmers want to prevent hair loss programmers are still worried about hair loss, I think we should exercise more and stay up late. Of course, it is not good to see the little sister who is pleasing to the eye.

The above is how to use Python to make a massive sketch of the little sister. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report