蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!

《蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!》 蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!

前面我插嘴一句,加群:700341555获取Python入门学习教程!

《蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!》 蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!

GitHub上有个很有意思的项目,这个项目就是将一张图片转换成一个网页,例如这张蒙娜丽莎的微笑转换成网页之后的效果对比:

《蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!》 蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!

左边是原图,右边是网页的效果,网页其实是由文字“爱”组成的。 具体的实现原理就是将图片切成很多个N*N的小块,然后将每个小块的像素平均值当做合成像素的颜色,最后渲染到 HTML 页面中对应位置的文字颜色。

安装

直接通过 GitHub 进行安装

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pip install git+https://github.com/lzjun567/img2html
</pre>

命令行使用方式

安装完成后,可以直接使用命令 img2html 执行,-i 和 -o 两个参数是必须指定的,前者表示要转换的图片,后者表示输出的文件名,如果没有指定 o 参数,那么 HTML 将直接输出在控制台,其它参数都是可选的。

《蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!》 蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!

代码调用

《蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!》 蒙娜丽莎的微笑欺骗了多少人?今天我用Python解析蒙娜丽莎!

Python3版本链接:https://github.com/lzjun567/img2html

主要修改的地方有两处,一个是字符串不兼容的问题,第二个是修复了 itertools.cycle 没有next方法的问题。整个项目就一个文件,可以 Fork 下来读读他的源代码。

    原文作者:浪里小白龙q
    原文地址: https://www.jianshu.com/p/7d146d79fcae
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞