phil Dee
phil Dee

Reputation: 11

PIL (Python 3.4) modifies RGV value

Here is a code that create a little 10×10 picture, all green, in jpg format:

import PIL.Image as img
im = img.new('RGB', (10,10), (0,255,0))
print(np.array(im))

[[[  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]]
...

So, it is ok. But if the file is saved then opened:

im.save(chemin+"essai.jpg")  
it = np.array(img.open(chemin+"essai.jpg"))
print(it)

array([[[  0, 255,   1],
        [  0, 255,   1],
        [  0, 255,   1],
        [  0, 255,   1],
        [  0, 255,   1],
        [  0, 255,   1],
        [  0, 255,   1],
        [  0, 255,   1],
        [  0, 255,   1],
        [  0, 255,   1]],
...

Why is there 1 in the Blue component?

It is the same if I do a red picture [255,0,0] which gives after saving and opening: [254,0,0]. So why 254?

It seems to be when saving and not when loading because if I open the saved file in a software like photoshop, the color is already modified.

If someone has an answer. (And sorry for my bad english). Thank you!

Upvotes: 1

Views: 67

Answers (1)

K L
K L

Reputation: 306

JPG is a lossy format. Lossless image formats like PNG work.

im.save(chemin+"essai.png")

Supported image file formats are documented here.

Upvotes: 3

Related Questions