user3752033
user3752033

Reputation: 39

OpenCv does not save images as it was constructed?

I am trying to save an image from a pixel array(numpy.ndarray). Suppose my pixel array is myArray. When I type myArray in Python interpreter, it shows value as:

array([[[154, 161, 156],
    [154, 161, 156],
    [154, 160, 156],
    ...,
    [152, 157, 156],
    [151, 156, 155],
    [150, 155, 154]],

   [[155, 161, 156],
    [155, 161, 156],
    [155, 161, 156],
    ...,
    [152, 157, 156],
    [151, 156, 155],
    [150, 155, 154]],

   [[155, 161, 156],
    [155, 161, 156],
    [155, 161, 156],
    ...,
    [152, 157, 156],
    [151, 156, 155],
    [150, 155, 154]],

   ...,

   [[187, 193, 188],
    [187, 193, 188],
    [187, 193, 188],
    ...,
    [189, 194, 193],
    [189, 194, 193],
    [189, 194, 193]],

   [[188, 194, 189],
    [188, 194, 189],
    [188, 194, 189],
    ...,
    [189, 194, 193],
    [189, 194, 193],
    [189, 194, 193]],

   [[188, 194, 189],
    [188, 194, 189],
    [188, 194, 189],
    ...,
    [189, 194, 193],
    [189, 194, 193],
    [189, 194, 193]]], dtype=uint8)

But, when I create a new image from this array by using cv2.imwrite('abc.jpg',myArray) and then read this image using cv2.imread('abc.jpg'), the new array looks different. It looks something like this:

array([[[155, 161, 156],
    [155, 161, 156],
    [155, 161, 156],
    ...,
    [152, 157, 156],
    [151, 156, 155],
    [150, 155, 154]],

   [[155, 161, 156],
    [155, 161, 156],
    [155, 161, 156],
    ...,
    [152, 157, 156],
    [151, 156, 155],
    [150, 155, 154]],

   [[155, 161, 156],
    [155, 161, 156],
    [155, 161, 156],
    ...,
    [152, 157, 156],
    [151, 156, 155],
    [150, 155, 154]],

   ...,

   [[187, 193, 188],
    [187, 193, 188],
    [187, 193, 188],
    ...,
    [189, 194, 193],
    [189, 194, 193],
    [189, 194, 193]],

   [[188, 194, 189],
    [188, 194, 189],
    [188, 194, 189],
    ...,
    [189, 194, 193],
    [189, 194, 193],
    [189, 194, 193]],

   [[188, 194, 189],
    [188, 194, 189],
    [188, 194, 189],
    ...,
    [189, 194, 193],
    [189, 194, 193],
    [189, 194, 193]]], dtype=uint8)

Any idea why this is happening?

Upvotes: 2

Views: 49

Answers (1)

MIRMIX
MIRMIX

Reputation: 1080

This because of the data loss while jpeg data compression. if you want save it lossless save it as lossless png.

JPEG (/ˈdʒeɪpɛɡ/ JAY-peg) is a commonly used method of lossy compression for digital images, particularly for those images produced by digital photography.

Upvotes: 3

Related Questions