当前位置: 首页 > 图灵资讯 > 行业资讯> Python中用numpy进行图片处理

Python中用numpy进行图片处理

发布时间:2025-12-01 20:58:36

numpy图片处理.png

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

1.图像的数组表示:

from PIL import Imagefrom 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 Imagefrom 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 #figure()将图像素值转换为100-200区间imshow(im3)im4 = 255.0*(im/255.0)**2 #图像figure()在对图像的像素值求平方后获得imshow(im4)print(int(im4.min()),int(im4.max())) #输出像素和最小值show()

图1:图像的反相

图2:100-2000的像素值

图3:像素值的平方

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

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

相关文章

Python中用numpy进行图片处理

Python中用numpy进行图片处理

2025-12-01
Python中没有xlrd怎么办?

Python中没有xlrd怎么办?

2025-12-01
Python中xlrd读取的报错处理

Python中xlrd读取的报错处理

2025-12-01
Python中如何用xlwt录入表格日期

Python中如何用xlwt录入表格日期

2025-12-01
centos中安装python3异常怎么办?

centos中安装python3异常怎么办?

2025-12-01
python中if嵌套的练习题有哪些?

python中if嵌套的练习题有哪些?

2025-12-01