• 技术文章 >常见问题 >Python常见问题

    Python中用numpy进行图片处理

    2021-03-24 16:25:14原创5762

    numpy图片处理.png

    其实在Python中,我们也可以用numpy进行图片处理,今天一起来学习下吧。

    1.图像的数组表示:

    1

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">from PIL import Image<br>from pylab import *<br>from numpy import *<br>im = array(Image.open('E:\Python\meinv.jpg'))<br>print(im.shape,im.dtype)<br><br>im = array(Image.open('E:\Python\meinv.jpg').convert('L'),'f')<br>print(im.shape,im.dtype)<br></span></p>


    运行结果:


    1

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">(272, 480, 3) uint8        #第一个元组中数据表示图像的行,列,颜色通道数 紧接着的字符 <br>                                     #串表示元素的数据类型<br>(272, 480) float32<br></span></p>

    2.灰度变换


    1

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">from PIL import Image<br>from pylab import *<br>from numpy import *<br>im = array(Image.open('E:\Python\meinv.jpg'))<br>print(im.shape,im.dtype)<br><br>im = array(Image.open('E:\Python\meinv.jpg').convert('L'),'f')<br>print(im.shape,im.dtype)<br><br>im2 = 255 - im       #对图像进行反相处理<br>figure()<br>imshow(im2)<br><br>im3 = (100.0/255)*im +100      #将图像像素值变换到100——200区间<br>figure()<br>imshow(im3)<br><br>im4 = 255.0*(im/255.0)**2         #对图像的像素值求平方后得到的图像<br>figure()<br>imshow(im4)<br><br>print(int(im4.min()),int(im4.max()))    #输出像素的和最小值<br><br>show()<br></span></p>

    图1:图像的反相



    图2:像素值变到100——200



    图3:像素值的平方



    以上就是Python中用numpy进行图片处理的方法,学会的小伙伴赶紧行动起来吧~更多Python学习推荐:PyThon学习网教学中心

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:numpy图片处理
    上一篇:Python基础:numpy中的常见函数有哪些 下一篇:Python中没有xlrd怎么办?

    相关文章推荐

    • Python中numpy怎样按行或列提取矩阵• Python基础:numpy中空值怎样设置• Python实用之numpy中空数组的创建• Python之numpy中mask选取子集• Python基础:numpy中vstack和hstack函数• Python中如何用numpy解决梯度下降最小值• Python基础:numpy中的常见函数有哪些• Python中numpy求函数的导数实现方法• Python中numpy如何进行降序?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网