9

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?

scott huang
  • 2,478
  • 4
  • 21
  • 36

2 Answers2

22

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]]]
NeverNervous
  • 606
  • 4
  • 8
  • yes. It works for me too. I also try to save and read jpg twice. the data in image still changes. so the data loss in jpg is real huge. BTW, the file size of png is mucher smaller than jpg in this case, which is very strange, I guess it is because the data is very small, try to compress it like JPG, will increase the size instead of decresing it which is what the algorithm intend to do . – scott huang Nov 18 '17 at 08:52
  • @scotthuang Sure... that data is not even the size of a macro-block (and being single-channel might be relevant too, maybe...). Why don't you read wiki on jpeg to understand this if you are interested? And why no upvote for this answer as it seems you gained something here. – sascha Nov 19 '17 at 02:05
  • I will try wiki later, I am in the middle of project. too many thing is going on. don't want get distracted. I upvote your answer, I wait a day to see if there is other people comment your answer, share some idea... – scott huang Nov 19 '17 at 07:31
0

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)
mduiker
  • 1
  • 2