一个毫无用处的公众号封面生成器

2019/04/10 10:10
阅读数 33

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; padding: 10px; word-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; line-height: 1.75; color: #595959; font-family: Optima-Regular, Optima, PingFangTC-Light, PingFangSC-light, PingFangTC-light; letter-spacing: 2px; background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center center;"><h1 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 25px;"><span style="display: inline-block; font-weight: bold; color: #595959;">一个毫无用处的公众号封面生成器</span></h1> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">对于一个没有任何艺术细胞的人,写公众号最头疼的无异于文章封面了. 要么选不好图,选好图了呢,公众号这个2.35:1的诡异比例还会涉及到裁剪的问题...</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">所以或许你已经发现了,我的文章封面很简单. 纯色图片避免了裁剪的问题,成为那些飚设计感和美感的妖艳货色中的一股清流</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">但是只是纯色的图片,貌似没有灵魂.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">今天要做的事情就是给我的清流般的封面注入灵魂.</p> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">一个极其不规范的类图</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">下面是一个不规范的类图示例,请大家不要学习.</p> <figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px;"><img src="http://wmcoding.oss-cn-hangzhou.aliyuncs.com/wmcoding/20200315/203958733.png" alt="封面生成" style="width: auto; max-width: 100%; border-radius: 6px; display: block; margin: 20px auto; object-fit: contain;"><figcaption style="margin-top: 5px; text-align: center; display: block; font-size: 13px; color: #595959;">封面生成</figcaption></figure> <figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px;"><img src="http://wmcoding.oss-cn-hangzhou.aliyuncs.com/wmcoding/20200315/204615044.png" alt="解码模块" style="width: auto; max-width: 100%; border-radius: 6px; display: block; margin: 20px auto; object-fit: contain;"><figcaption style="margin-top: 5px; text-align: center; display: block; font-size: 13px; color: #595959;">解码模块</figcaption></figure> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">思路总结</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">其实思路很简单</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: circle;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">获取一段文字,解码为UTF-8的二进制串</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">将解码后的二进制串进行md5摘要,获得一个128位的digest</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">通过random.randomint()方法在这个128位的二进制串上 随机选取3个字节做RGB颜色.</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">生成一个253*100的图片保存到本地.同时将相关信息存入mongoDB数据库.</section></li></ul> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">所谓的解码就更简单了,众所周知MD5算法是不可逆的...再加上当初我是随机取的三个字节,所以想通过颜色来解码是不可能的.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">但是!!! 我有一个绝妙的办法...因为之前相关信息已经存到了数据库里,所以我们直接在数据库里检索颜色的RGB值就可以了.(我简直不要太机智)</p> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">知识点总结</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">注意,干货来了!!!</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #DEC6FB; color: #595959;">输入内容的处理</span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">输入内容处理想对比较简单</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #333; background: #f8f8f8; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"> <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">encode</span><span class="hljs-params" style="line-height: 26px;">(self)</span>:</span><br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> hashlib.md5(self.raw_input_str.encode(<span class="hljs-string" style="color: #d14; line-height: 26px;">'utf-8'</span>)).hexdigest()<br><br> <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">color_generator</span><span class="hljs-params" style="line-height: 26px;">(self, raw_digest)</span>:</span><br> r_index = random.randint(<span class="hljs-number" style="color: #008080; line-height: 26px;">0</span>, <span class="hljs-number" style="color: #008080; line-height: 26px;">30</span>)<br> g_index = random.randint(<span class="hljs-number" style="color: #008080; line-height: 26px;">0</span>, <span class="hljs-number" style="color: #008080; line-height: 26px;">30</span>)<br> b_index = random.randint(<span class="hljs-number" style="color: #008080; line-height: 26px;">0</span>, <span class="hljs-number" style="color: #008080; line-height: 26px;">30</span>)<br><br> r_color = int(raw_digest[r_index:r_index + <span class="hljs-number" style="color: #008080; line-height: 26px;">2</span>], base=<span class="hljs-number" style="color: #008080; line-height: 26px;">16</span>)<br> g_color = int(raw_digest[g_index:g_index + <span class="hljs-number" style="color: #008080; line-height: 26px;">2</span>], base=<span class="hljs-number" style="color: #008080; line-height: 26px;">16</span>)<br> b_color = int(raw_digest[b_index:b_index + <span class="hljs-number" style="color: #008080; line-height: 26px;">2</span>], base=<span class="hljs-number" style="color: #008080; line-height: 26px;">16</span>)<br><br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> r_color, g_color, b_color<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">先使用hashlib下的方法m5()方法和hexdigest()方法获取哈希值.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">然后随机一下取出3个字节,分别做R G B颜色就可以了.</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #DEC6FB; color: #595959;">图像处理</span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">之前约等于没有接触过图像处理这一块,所以特地去查了一下python下常用的图像处理模块:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: circle;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><a href="https://numpy.org/" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">NumPy</a> : 这个库是科学计算的一个基础库,主要用来处理N维数组对象以及一些基础数学计算,比如傅里叶变换,线性代数等. 算是一个图片处理的中间库</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><a href="https://opencv-python-tutroals.readthedocs.io/en/latest/" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">OpenCV</a> : 一个专业的计算机视觉库. 可以用于图像视频等的处理,比如人脸识别之类...</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">PIL: Python自带的图像处理库,由于年久失修,目前处于门可罗雀的状态</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><a href="https://pillow.readthedocs.io/en/stable/index.html" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">Pillow</a>: PIL的志愿者版本,由于PIL已经很久没有维护,功能简单而且只支持到python2.x. 所以志愿者们在PIL的基础上开发出了pillow,支持更多的功能且支持python3</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><a href="https://pillow.readthedocs.io/en/stable/" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">Pillow-SIMD</a>: PIL究极进化版,速度更快,功能更强. SIMD指的是single instruction multiple data(单指令,多数据),是一种对海量数据进行相同操作的算法. 据说速度可以媲美OpenCV</section></li></ul> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">pillow是PIL的一个分支,而pillow-simd是pillow的一个分支.因为我也是第一次接触图像处理. 我发现很神奇的是 pillow的文档和pillow-simd的文档竟然是一个. 但是在安装的时候 又是两个包...没有搞明白怎么回事. 如果有大神知道的话,麻烦留言解惑.</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <h4 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 18px;"><span>pillow模块的基本使用</span></h4> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">这次我采用的是Pillow,原因很简单,杀鸡焉用牛刀. 只是需要生成一个纯色的小图片而已. 安装很简单,如果使用Anaconda的话,它已经集成了pillow包. 如果使用的是纯python3的环境,<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">pip install pillow</code>就可以了.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">在引入包的时候需要注意. 虽然我们用的是pillow,但是它的包名依旧是PIL. 所以在引入包的时候需要注意.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #333; background: #f8f8f8; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">from</span> PIL <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">import</span> Image<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">这次使用的是最基础的方法. 主要就是创建图像. 相关方法的具体文档,请<a href="https://pillow.readthedocs.io/en/stable/reference/Image.html" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">查阅这里</a></p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: circle;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">PIL.Image.open(fp, mode='r')</code></p> <ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: square;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><strong style="color: #595959; font-weight: bold;"><span>「</span>作用:<span>」</span></strong> 打开一个图像文件.</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><strong style="color: #595959; font-weight: bold;"><span>「</span>参数:<span>」</span></strong> <ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: square;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">fp:文件句柄或者文件路径</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">mode: 和文件操作时候的mode一样,r表示只读,w表示只写. w+表示读写</section></li></ul> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><strong style="color: #595959; font-weight: bold;"><span>「</span>返回值:<span>」</span></strong> Image对象</section></li></ul> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">PIL.Image.new(mode, size, color=0)</code></p> <ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: square;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><strong style="color: #595959; font-weight: bold;"><span>「</span>作用:<span>」</span></strong> 创建一个Image对象.</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><strong style="color: #595959; font-weight: bold;"><span>「</span>参数:<span>」</span></strong> <ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: square;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">mode:图像模式,具体参考文档.</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">size:图像大小</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;">color:颜色</section></li></ul> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><strong style="color: #595959; font-weight: bold;"><span>「</span>返回值:<span>」</span></strong> Image对象</section></li></ul> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">Image.save(fp, format=None, **params)</code></p> <ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: square;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><strong style="color: #595959; font-weight: bold;"><span>「</span>作用:<span>」</span></strong> 保存图像到文件</section></li></ul> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">Image.show(title=None, command=None)</code></p> <ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: square;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><strong style="color: #595959; font-weight: bold;"><span>「</span>作用:<span>」</span></strong> 调用本地看图软件打开图像</section></li></ul> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; font-size: 14px; font-weight: normal; color: #595959;"><p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">Image.close()</code></p> </section></li></ul> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">这里要说的是,既然有open方法也有close方法. 这个Image对象也是支持with上下文管理器的.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">所以打开一个图像的时候可以直接使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">with...as...</code>语法</p> <h4 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 18px;"><span>pillow模块的简单示例</span></h4> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>打开一个图片文件并显示<span>」</span></strong></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #333; background: #f8f8f8; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">show_img</span><span class="hljs-params" style="line-height: 26px;">()</span><br> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">with</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">Image</span>.<span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">open</span><span class="hljs-params" style="line-height: 26px;">(self.file_path)</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">as</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">img</span>:</span><br> img.show()<br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> img<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>生成一个新的纯色图片并保存到文件<span>」</span></strong></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #333; background: #f8f8f8; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">new_a_image</span><span class="hljs-params" style="line-height: 26px;">()</span>:</span><br> img = Image.new(<span class="hljs-string" style="color: #d14; line-height: 26px;">'RGB'</span>,(<span class="hljs-number" style="color: #008080; line-height: 26px;">253</span>,<span class="hljs-number" style="color: #008080; line-height: 26px;">100</span>),(<span class="hljs-number" style="color: #008080; line-height: 26px;">240</span>,<span class="hljs-number" style="color: #008080; line-height: 26px;">233</span>,<span class="hljs-number" style="color: #008080; line-height: 26px;">204</span>))<br> img.save(<span class="hljs-string" style="color: #d14; line-height: 26px;">'demo.jpg'</span>)<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><strong style="color: #595959; font-weight: bold;"><span>「</span>获取某个像素点的RGB值<span>」</span></strong></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #333; background: #f8f8f8; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"> <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">get_color_from_file</span><span class="hljs-params" style="line-height: 26px;">(self)</span>:</span><br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">with</span> Image.open(self.file_path) <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">as</span> img:<br> color = img.getpixel((<span class="hljs-number" style="color: #008080; line-height: 26px;">100</span>, <span class="hljs-number" style="color: #008080; line-height: 26px;">100</span>))<br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> color<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #DEC6FB; color: #595959;">mongoDB数据库基本操作</span></h3> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">MongoDB是一款开源的,高性能的非关系型数据库. 也就是传说中的NoSQL数据库中的热门款. 主要以key-value为作为存储结构.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">具体的介绍可以参见这篇博文: <a href="https://www.cnblogs.com/shaosks/p/5644129.html" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">Mongodb介绍</a></p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">这里简单介绍一下mongoDB的操作.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">MongoDB的安装和Robo 3T的安装使用等,已经写进了我的小本本. 有空再来写. 今天先简单介绍一下mongoDB的基本操作.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">在python中,我们是使用<strong style="color: #595959; font-weight: bold;"><span>「</span>pymongo<span>」</span></strong>这个模块来操作MongoDB数据库的.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">基本使用很简单,放一小段代码一看就懂.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #333; background: #f8f8f8; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">import</span> pymongo<br><br>MONGO_URL = <span class="hljs-string" style="color: #d14; line-height: 26px;">'localhost'</span><br>MONGO_DB = <span class="hljs-string" style="color: #d14; line-height: 26px;">'wmcoding_cover'</span><br>MONGO_TABLE = <span class="hljs-string" style="color: #d14; line-height: 26px;">'cover'</span><br><br><br><span class="hljs-comment" style="color: #998; font-style: italic; line-height: 26px;"># 声明连接客户端</span><br>client = pymongo.MongoClient(MONGO_URL, connect=<span class="hljs-literal" style="color: #008080; line-height: 26px;">False</span>) <br><span class="hljs-comment" style="color: #998; font-style: italic; line-height: 26px;"># 声明要操作的数据库</span><br>db = client[MONGO_DB]<br><br><span class="hljs-comment" style="color: #998; font-style: italic; line-height: 26px;"># 保存数据到mongoDB. 直接使用insert_one()方法就可以了</span><br><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">save_to_mongo</span><span class="hljs-params" style="line-height: 26px;">(self, digest, color, feeling, file_path)</span>:</span><br> res = {<br> <span class="hljs-string" style="color: #d14; line-height: 26px;">'digest'</span>: digest,<br> <span class="hljs-string" style="color: #d14; line-height: 26px;">'color'</span>: color,<br> <span class="hljs-string" style="color: #d14; line-height: 26px;">'dscp'</span>: feeling,<br> <span class="hljs-string" style="color: #d14; line-height: 26px;">'path'</span>: file_path<br> }<br><br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">if</span> db[MONGO_TABLE].insert_one(res):<br> print(<span class="hljs-string" style="color: #d14; line-height: 26px;">'cover save to database successful!'</span>)<br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> <span class="hljs-literal" style="color: #008080; line-height: 26px;">True</span><br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> <span class="hljs-literal" style="color: #008080; line-height: 26px;">False</span><br><br><span class="hljs-comment" style="color: #998; font-style: italic; line-height: 26px;"># 同样检索的话,使用find_one()方法,获取一条记录. 该记录会以列表的形式展现</span><br><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold; line-height: 26px;">decode</span><span class="hljs-params" style="line-height: 26px;">(self)</span>:</span><br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">if</span> self.file_path:<br> self.color = self.get_color_from_file()<br> res = db[MONGO_TABLE].find_one({<span class="hljs-string" style="color: #d14; line-height: 26px;">'color'</span>: list(self.color)})<br> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> res[<span class="hljs-string" style="color: #d14; line-height: 26px;">'dscp'</span>]<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">更多关于mongoDB的操作可以参见崔大佬的文章:<a href="https://juejin.im/post/5addbd0e518825671f2f62ee" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">Python操作MongoDB看这一篇就够了</a></p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">代码已经上传到了github,感兴趣的可以拿来看一看. <a href="https://github.com/thecatcher/coverGenerator" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">Cover Generator</a></p> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 24px; text-align: left; margin: 20px 10px 0px 0px;"><span style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">后话</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">现在这个版本比较简陋.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">下一步的计划是给它加个GUI的界面.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">再下一步的计划是改成在线版并且跟微信公众号对接.</p> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">实现上传封面图后每个关注了公众号的用户都可以看到封面背后的"优雅且富有诗韵"的文字...</p> </section>

原文出处:https://www.cnblogs.com/thecatcher/p/12500831.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部