Anshuman
Anshuman

Reputation: 67

Replaced the value of pixel from an image to a different value, but after getting the color value, it is different

>>> from PIL import Image
>>> im = Image.open("E:\\aaa.jpeg")
>>> color = im.getpixel((100,100))
>>> print  color
   (235, 229, 205)
>>> im.putpixel((100,100),(1,1,1))
>>> im.save("E:\\new.jpeg")
>>> im=Image.open("E:\\new.jpeg")
>>> color=im.getpixel((100,100))
>>> print color
   (8, 1, 0)

The value should have been (1,1,1)....but it shows(8,1,0)

Upvotes: 1

Views: 120

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121514

JPEG is a lossy format.

On save, your pixel data is compressed with an algorithm that does not preserve precise pixel information. When reading and decompressing that data, there is no guarantee that specific pixels will still have the exact same colour value. (8, 1, 0) is close enough, as far as the JPEG compression is concerned.

Use a different format that preserves pixel data exactly like PNG, if this is important to your application.

Upvotes: 2

Related Questions