首页 > 解决方案 > 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

标签: pythonpython-3.xpython-imaging-libraryrgbpixel

解决方案


您已成功更改图像,但如果您想再次读取它,则需要将其写入文件:

要保存到同一个图像文件,只需执行

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

推荐阅读