scikit-image: write a ndarray to image with imsave, read back with imread, data don't match

Viewed 16971

here is the minimum working example:

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)

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?

2 Answers

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.

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)

and this is the result

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 uses PIL underneath, have you tried using straight PIL

In Example

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

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

   Image.fromarray(image).save()
   rb_image = imread("test.jpg")
   print("original image")
   print(image)
   print("read back image")
   print(rb_image)
Related