scikit-image:使用 imsave 将 ndarray 写入图像,使用 imread 读回,数据不匹配 [英] scikit-image: write a ndarray to image with imsave, read back with imread, data don't match

查看:91
本文介绍了scikit-image:使用 imsave 将 ndarray 写入图像,使用 imread 读回,数据不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是最小的工作示例:

import numpy as np
from skimage.io import imsave, imread

image = np.array([[[109, 232, 173],
                [ 55,  35, 144]],
                [[ 43, 124, 185],
                [234, 127, 246]]], dtype=np.uint8)

imsave("test.jpg", image)
rb_image = imread("test.jpg")
print("original image")
print(image)
print("read back image")
print(rb_image)

运行后,结果是,从文件中读回的ndarray与

after run it, the result is, the ndarray read back from file don't match with

original image
[[[109 232 173]
  [ 55  35 144]]

 [[ 43 124 185]
  [234 127 246]]]
read back image
[[[111 208 255]
  [ 42  61 138]]

 [[ 72 140 201]
  [141 131 218]]]

有人能给我一些建议吗?

can some one give me some suggestiones?

推荐答案

jpeg 是一种有损图像压缩算法,旨在通过去除人眼不易察觉的信息来减小文件大小.这意味着以 jpg 格式保存将节省一些磁盘空间,但会更改数组的像素值.

jpeg is a lossy image compression algorithm, designed to reduce the file size by getting rid of information that is not easily noticeable from the human eye. That means saving in jpg will save some disk space but change the pixel values of your array.

您可以通过保存为无损 png 格式来避免此问题.以下代码段对我有用

You can avoid the problem by saving in lossless png format instead. The following snippet works for me

import numpy as np
from skimage.io import imsave, imread

image = np.array([[[109, 232, 173],
                [ 55,  35, 144]],
                [[ 43, 124, 185],
                [234, 127, 246]]], dtype=np.uint8)

imsave("test.png", image)
rb_image = imread("test.png")
print("original image")
print(image)
print("read back image")
print(rb_image)

这是结果

original image
[[[109 232 173]
  [ 55  35 144]]

 [[ 43 124 185]
  [234 127 246]]]
read back image
[[[109 232 173]
  [ 55  35 144]]

 [[ 43 124 185]
  [234 127 246]]]

这篇关于scikit-image:使用 imsave 将 ndarray 写入图像,使用 imread 读回,数据不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆