Goddess said took a photo album ended up sketches want? Very simple to use Python on the line!

Sketch as a near-perfect performance practices has its own unique charm, with the development of digital technology, the sketch is no longer a professional painting division of the patent, this article today to talk about how to obtain small quantities using python sister sketch portrait. The article is divided in two parts:

  • The first part describes two sketches using python to generate ideas

  • The second section describes how to get the bulk sketch

First, get two sketches of ideas

Two ideas described in this section are achieved based on opencv, does not involve deep learning content. The basic idea is to read a picture chart, and then converted into sketch through various transformations. To facilitate the presentation, we go first to a small photograph of her sister as the experimental material.

1, comic style

First is the first method, the core idea of ​​this approach is to use a technique called "threshold" of this technique is the difference between the gray scale image and the background object based on the pixel level of segmentation .

If you want to put into a picture showing only black and white sketch, you need to be a binary operation, opencv provides two binarization methods of operation: threshold () and adaptiveThreshold (). Compared threshold (), adaptiveThreshold () depending on the luminance distribution can be locally adjusted automatically area image, it is called adaptive binarization. Below this picture is a color picture of the effect after the binarization operation.

The concept mentioned above may be more obscure, did not understand it does not matter, here we will talk about how the actual operation.

The first step is read and converted to grayscale images. This step is considered a routine operation, I believe used opencv students have written similar code.

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

The second step, adaptiveThreshold () method for image binarization operation, function parameters often used to set the adaptive binarization threshold algorithms and the like.

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 operation of the above steps, we got a new black and white picture, take a look at photo quality after conversion.

From the picture after the conversion, although probably contour is no problem, but the effect is far from ideal, is not able to call sketch. This is mainly because adaptiveThreshold () operation will be binarized in the local region of each of the small picture, and therefore for some high resolution, color-coded images more delicate, there will be such a case where the above-dense.

这个问题解决起来其实也很简单,只要在进行二值化之前加入下面这行代码对原图进行模糊化就可以了。

img_gray = cv2.medianBlur(img_gray, 5)

再来看看这次生成的素描图(下图),是不是看起来舒服多了,还有一种手绘漫画的感觉。

2、写实风格

通过上面这种方法,虽然最终也获得了一幅还算不错的素描图,但是看起来多少有些“失真”,为了获取看起来更加真实的素描图,我们尝试另外一种方法。

这种方法的核心思想是通过“底片融合”的方式获取原图中一些比较重要的线条,具体实现步骤如下:

第一步,跟上面的方法一样,使用opencv读取图片并生成灰度图。

第二步,对灰度图进行模糊化操作。经过试验,使用上面提到的中值滤波函数cv2.medianBlur()进行模糊化操作最终得到的素描图效果并不好,这里我们尝试使用高斯滤波进行图片模糊化,代码如下:

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

其中,参数ksize表示高斯核的大小,sigmaX和sigmaY分别表示高斯核在 X 和 Y 方向上的标准差。

第三步,使用cv2.divide()方法对原图和模糊图像进行融合,cv2.divide()本质上进行的是两幅图像素级别的除法操作,其得到的结果可以简单理解为两幅图之间有明显差异的部分。来看代码:

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

第四步,保存生成的图片,代码跟上一个方法中一样,我们直接来看获取到的素描图效果。

从结果来看,这种方法获得的素描图线条更加细腻,素描效果也更好。

二、批量获取小姐姐素描画像

在这一部分,我们要实现批量获取小姐姐素描画像的功能,基于上文中两种素描图效果比对,这里采用第二种方法来实现图片到素描图的转换。

那么,接下来要解决的就是图片源的问题了。最近很多项目都成功实现了从抖音或者知乎获取漂亮小姐姐这一操作,其实除了这些平台之外还有好多网站能获取到漂亮小姐姐的图片。

网站的具体内容我就不在文中展示了,为了指定图片爬取的思路,大概讲下页面结构:网站的主页罗列了N个主题,每个主题页面中都包含了M张小姐姐的图片,结构示意图如下:

各页面url的构建也很明了,例如下图中的页面url是http://www.waxjj.cn/2794.html,其中2794就是主题页的ID号。查看页面的html代码(下图),发现每张图片都在一个<li>标签下面。

遇到这种情况,一般来说我们可以通过某种解析器来获取每张图片的url。但是,经过仔细观察发现整个网页的html代码中只有涉及图片url的部分带有完整的http连接,因此可以考虑使用正则表达式来提取图片url,实现这部分功能的代码如下。

在上面这段代码中,我们提取主题页的ID作为待保存图片名称的一部分,save_jpg()函数中会把每张图片转换为素描图并保存到本地。

由于我们要使用opencv对抓取到的图片进行各种运算转换,因此使用requests获取的图片必须先保存到本地,再用opencv重新读入后才行。基于上述思想,我们构建了如下所示的save_jpg()函数,其中rgb_to_sketch()函数是对上文第一部分中所说的第二种素描图的获取方法进行的封装。

而在主函数中,我们只需要指定想要获取主题页面的id号,构建一组url列表就可以了:

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

以上就是完整代码,在学习Python的过程中,往往因为没有资料或者没人指导从而导致自己不想学下去了,因此我特意准备了个群 592539176 ,群里有大量的PDF书籍、教程都给大家免费使用!不管是学习到哪个阶段的小伙伴都可以获取到自己相对应的资料!来看看运行后的效果吧~~

其实程序员要想防止脱发,我觉得还是要多锻炼身体,少熬夜,当然多看看养眼的小姐姐也不是不错的!

你说呢

Guess you like

Origin www.cnblogs.com/chengxyuan/p/12112831.html