Search code examples
pythonpython-3.xpython-imaging-libraryrgbpixel

Changing the value of pixel in an image


I am working with stenography. I need to hide data in pixel of an image.But I am fetching problem when I am trying to update the value of pixel. I tried the code below:

from PIL import Image

im = Image.open('./data/frame398.png')
pix = im.load()
r, g, b = pix[200,200]
print("Pre RGB")
print(r, g, b)
pix[200,200] = 0,0,0  

It should change the value of the pixel to (0,0,0). But it doesn't. If I try the code below:

imx = Image.open('./data/frame398.png')
pixx = imx.load()
r, g, b = pixx[200,200]
print("Post RGB")
print(r, g, b)

I got the output below:

Pre RGB
69 62 65
Post RGB
69 62 65

Instead of (0,0,0) I am getting the old value. What I am doing wrong? I need help.Thanks


Solution

  • You are successfully changing the image, but you need to write it to a file if you want to read it again:

    To save to the same image file just do

    im.save('./data/frame398.png', ‘PNG’)