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

    Python中用numpy进行图片处理

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

    numpy图片处理.png

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

    1.图像的数组表示:

    from PIL import Image
    from pylab import *
    from numpy import *
    im = array(Image.open('E:\Python\meinv.jpg'))
    print(im.shape,im.dtype)

    im = array(Image.open('E:\Python\meinv.jpg').convert('L'),'f')
    print(im.shape,im.dtype)


    运行结果:


    (272, 480, 3) uint8 #第一个元组中数据表示图像的行,列,颜色通道数 紧接着的字符
    #串表示元素的数据类型
    (272, 480) float32

    2.灰度变换


    from PIL import Image
    from pylab import *
    from numpy import *
    im = array(Image.open('E:\Python\meinv.jpg'))
    print(im.shape,im.dtype)

    im = array(Image.open('E:\Python\meinv.jpg').convert('L'),'f')
    print(im.shape,im.dtype)

    im2 = 255 - im #对图像进行反相处理
    figure()
    imshow(im2)

    im3 = (100.0/255)*im +100 #将图像像素值变换到100——200区间
    figure()
    imshow(im3)

    im4 = 255.0*(im/255.0)**2 #对图像的像素值求平方后得到的图像
    figure()
    imshow(im4)

    print(int(im4.min()),int(im4.max())) #输出像素的和最小值

    show()

    图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学习网