美文网首页大数据 爬虫Python AI SqlPython小哥哥
用Python帮你上马,哪里无码打哪里 !

用Python帮你上马,哪里无码打哪里 !

作者: 14e61d025165 | 来源:发表于2019-06-20 15:30 被阅读0次

目录

0 引言

1 环境

2 需求分析

3 代码实现

4 代码全景展示

5 后记

0 引言

所谓的像素图,就是对图像做一个颗粒化的效果,使其产生一种妙不可言的朦胧感。费话不多说,先来看一张效果图。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561015775707 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

▲效果图

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561015775712 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

▲原图

怎么样,效果还不错吧?现在,我们用Python来实现这种像素化的效果。

1 环境

操作系统:Windows

Python版本:3.7.3 Python学习交流群:1004391443

2 需求分析

一个最简单的实现思路,在打开图片后,把图片分割成一些像素块,再对这些像素块中的图像信息进行处理(修改图像中的RGB值)即可。

这里我们使用Numpy库和PIL库来实现这个需求,后者用来图像的读取与保存,涉及到的所有图像处理动作均借助Numpy来实现。

有关NumPy模块、PIL模块的介绍,可参考如下。

<bi style="box-sizing: border-box; display: block;">NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。</bi><bi style="box-sizing: border-box; display: block;">PIL(Python Imaging Library)是Python常用的图像处理库,而Pillow是PIL的一个友好Fork,提供了了广泛的文件格式支持,强大的图像处理能力,主要包括图像储存、图像显示、格式转换以及基本的图像处理操作等。</bi>

这两个模块非Python内置,都属于第三方模块,可直接采用如下方式进行安装

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; 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 numpy
pip install Pillow
</pre>

注意,要想使用PIL模块,是需要直接install Pillow模块的。

3 代码实现

首先导入我们要用到的模块

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import numpy as np
from PIL import Image
</pre>

接下来,我们要处理图片,首先得打开一张图片,如下

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">data = Image.open("P:\Personal\LuoShen.xpg")
</pre>

然后把图像转换化Numpy数组进行下一步的处理

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">im1 = np.array(data)
</pre>

这里处理的核心思想,也很简单,主要通过中间值的RGB,对所选范围块的RGB进行重新赋值。

im1[y:y + pixel, x:x + pixel] = im1[y + (pixel // 2)][x + (pixel // 2)] 这里的x、y是分别指的我们图像的横向、纵向像素点的坐标值、而pixel指的是我们要以多大的像素块,来处理这张图像,我们设置的单位像素块(Pixel数值)越小,生成的像素图越精确。

当然了,若单位像素块设置的太小,生成图像就看不出效果了,至于多大的数值合适,需要自行尝试。不同尺寸的图像,要达到最佳的像素化的显示效果,所需要设置的单位像素块的大小也是不同的,实践出真知。

我们需要图像的指定一个处理范围,并对该范围内的每一个坐标(像素)点进行像素化的处理。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">for y in range(Start_coordinate[1], End_coordinate[1], pixel):
for x in range(Start_coordinate[0], End_coordinate[0], pixel):
pass
</pre>

在处理完成之后,我们再把Numpy数组转换回图像。

im2 = Image.fromarray(im1.astype(np.uint8)) 最后展示出处理后的图像

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">im2.show()
</pre>

4 代码全景展示

import numpy as np from PIL import Image def to_pixelBlock(pixel, Start_coordinate, End_coordinate): ''' :param pixel: 单位像素块的元素大小 :param Start_coordinate: 处理的起始坐标(像素)点,元组形式 :param End_coordinate: 处理的终止坐标(像素)点,元组形式 :return: 通过中间值的RGB,对所选范围块的RGB进行重新赋值,设置的单位像素块(Pixel数值)越小,生成的像素图越精确 ''' # 读取图片,并由 PIL image 转换为 NumPy array im1 = np.array(Image.open("P:\Personal\LuoShen.jpg")) # 遍历所要处理范围内的所有坐标(像素)点 for y in range(Start_coordinate[1], End_coordinate[1], pixel): for x in range(Start_coordinate[0], End_coordinate[0], pixel): # 通过中间值的RGB,对所选范围块的RGB进行重新赋值 im1[y:y + pixel, x:x + pixel] = im1[y + (pixel // 2)][x + (pixel // 2)] # 将NumPy array 转换为 PIL image im2 = Image.fromarray(im1.astype(np.uint8)) # 展示处理后的图像 im2.show() if name == 'main': # 设置好要处理的像素范围,并以多大的像素块来生成最终效果图 to_pixelBlock(10, (0, 0), (1280, 800) 5 后记

本文使用了PIL加上Numpy的配合,短短几行代码实现了图像像素化的处理。当然这只是一种简单地实现,要想实现更丰富的处理效果,还可以借助CV2来实现。

好了,以上就是本篇全部内容。

后台回复:「马赛克」,获取本文所涉及的完整代码。

推荐阅读:

618大促,我把知识星球的价格调错了……

上次混脸熟的中奖读者是:「Mona_Lisa」,请于今天22:00前联系我哦。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561015775755 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

今天混脸熟的规则是:你怕虫吗……?我昨天家里飞进来一个巨大的虫,跟我巴掌差不多大……

相关文章

  • 用Python帮你上马,哪里无码打哪里 !

    目录 0 引言 1 环境 2 需求分析 3 代码实现 4 代码全景展示 5 后记 0 引言 所谓的像素图,就是对图...

  • 用Python帮你打码,哪里无码打哪里

    0 引言 所谓的像素图,就是对图像做一个颗粒化的效果,使其产生一种妙不可言的朦胧感。费话不多说,先来看一张效果图。...

  • 醋打哪里酸 盐打哪里咸

    为什么你的世界是不属于我的呢?其实很简单你不可能将你的所有时间都交给我来使用和适用。即使你给予了也是需要1:6 的...

  • 万事皆有其理

    词的写作叫“填词”,就是按照词谱,看哪里用平声字,哪里用仄声字,哪里平仄均可用,哪里断句,哪里用韵,然后把...

  • 超全Python速查表登上GitHub热榜,标星4600!

    哪里不会, 抄 查哪里。 GitHub上出现一份对Python用户非常友好的资源: Python Cheatshe...

  • 下雨了

    下雨了 你在哪里 我能帮你撑伞吗?

  • Anaconda:剪切板数据读取

    Python和R都是免费的开源软件,这类软件有一个对小白新手非常友好的特点,那就是……哪里不会查哪里,随用随学……...

  • 《素心凈心禪拍療愈》問答140

    问:肠炎可以打吗? 师:可以。哪里有炎症,直接打哪里。 问:肠区在哪里? 师:肚脐以下三公分。有人问,说给人家,肚...

  • 曾经爱过的联盟

    如果有一天我不玩英雄联盟了,我会卸下所有符文,清空所有天赋,用盖伦打一局人机,梦从哪里开始就从哪里结束。

  • 早安护肤分享

    早安护肤分享 1、用最贵的化妆品,熬最长的夜 2、医美注射,哪里不好打哪里 3、大牌爆款,买买买,用了大牌爆款我就...

网友评论

    本文标题:用Python帮你上马,哪里无码打哪里 !

    本文链接:https://www.haomeiwen.com/subject/dicjqctx.html