• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python使用scipy保存图片的一些注意点

python 搞代码 4年前 (2022-01-09) 28次浏览 已收录 0个评论

首先我们载入一张灰度图片,一般灰度图片像素为0-255.

可以发现该图片的最大像素为254,最小像素为2。一般处理图片时会转化为double类型。

我们将图片使用scipy保存为pgm格式。

然后我们重新读取该图片信息。

其像素值发生了变化,自动标准化到了0-255范围,最小值变为0,最大值变为255.

所以,使用scipy保存图像时,加上2个参数,cmin和cmax。就可以了

重新读取图片。结果正确

补充:from scipy import misc 读取和保存图片

from scipy import misc

def imread(name, flatten=False, mode=None)
    pass
"""
读取一张图片返回array,uint8,"""
def imsave(name, arr, format=None):
    pass
"""
Save an array as an image. 自动的调用将图片转成0-255范围内的
arr : ndarray, MxN or MxNx3 or MxNx4
        Array containing image values.  If the shape is ``MxN``, the array
        represents a grey-level image.  Shape ``MxNx3`` stores the red, green
        and blue bands along the last dimension.  An alpha layer may be
        included,<a>本文来源gao*daima.com搞@代#码&网6</a> specified as the last colour band of an ``MxNx4`` array.
"""

注意上面对图片读取和保存的函数会被弃用,使用如下代替:

import imageio
imageio.imread()
imageio.imwrite()

以上为个人经验,希望能给大家一个参考,也希望大家多多支持搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python使用scipy保存图片的一些注意点

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址