You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scikit-image调用imsave出现float64转uint8有损转换告警如何解决

Scikit-image保存图像时Lossy conversion告警解决方案

告警原因

你收到的告警原文如下:

Warning: Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.

触发原因是使用as_gray=True参数读取灰度图时,scikit-image会自动将图像像素值归一化为[0,1]区间的float64格式,而PNG格式默认存储的是0-255区间的uint8类型像素,imsave执行时会自动做强制类型转换,因此弹出该告警。

修复方法

方法1:使用scikit-image内置类型转换工具(推荐)

调用skimage提供的img_as_ubyte方法完成合规的类型转换,无需手动计算数值范围,避免出错。
修改后完整代码:

from skimage.io import imread, imshow, imsave 
from skimage import img_as_ubyte

img_grey = imread('C:/Lawn.png', as_gray=True)
# 转换为uint8类型后再保存
imsave('Lawn22.png', img_as_ubyte(img_grey))

img = imread('C:/Lawn.png')
imshow(img)

方法2:手动缩放数值后转换类型

直接对float64格式的像素值做线性缩放后强制转uint8,仅需修改保存代码为:

imsave('Lawn22.png', (img_grey * 255).astype('uint8'))

方法3:屏蔽告警(不推荐)

如果不需要修改存储逻辑,只想临时屏蔽该类告警,可以添加告警过滤规则,注意该方法会屏蔽所有同类型UserWarning,可能遗漏其他有效提示:

import warnings
from skimage.io import imread, imshow, imsave 

# 过滤对应告警
warnings.filterwarnings("ignore", category=UserWarning, message="Lossy conversion from float64 to uint8")
img_grey = imread('C:/Lawn.png', as_gray=True)
imsave('Lawn22.png', img_grey)

img = imread('C:/Lawn.png')
imshow(img)

内容的提问来源于stack exchange,提问作者user15761564

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 07:12:03