简体   繁体   中英

python's putpixel() isn't working

So I was working on this school project (I know really basic programming, and python is the only language I know) where I need to change my pixel colour to encode a message in a picture, but PIL's putpixel doesn't seem to be working, here is my code.

PS: all my PIL information is self taught and English isn't my main language so if you could talk simplified I'd be grateful

from PIL import Image
e=input('file and location?  ')
img=Image.open(e)
pmap=img.load()
imy=img.height
imx=img.width
if int(input('1 for encoding, 2 for decoding   '))==1:
    a=input('Your message?   ')
    for i in range(len(a)):
        r , g , b=img.getpixel((i+10,imy//2))
        img.putpixel((i+10,imy//2),(ord(a[i]),g,b))
    r,g,b=img.getpixel((len(a)+10,imy//2))
    img.putpixel((len(a)+10,imy//2),(999,g,b)) #999 is the stop code in     decoding
else:
    r=u=0
    m=''
    while r!=999:
        r , g , b=img.getpixel((10+u,imy//2))
        m+=chr(r)
        u+=1
    print(m[:len(a)-1])
img.save(e)

please bare in mind that I'm not looking to make a visual difference and I've already done debugging.There are also no errors,putpixel is not working for some reason though. as I said, I'm new to programming, so sorry if it includes stupid mistakes.

After using your code and trying it out on an image, putpixel is working as expected. The change in the pixels is very hard to see and that may be why you believe that it isn't working. Believe me, it is working, you just can't see it.

However, there are two problems I see with your code:

1) 999 is not encodable

999 can not be encoded in a single pixel. The maximum value for a pixel is 255 (The range is 0-255). You need to choose a different stop code/sequence. I recommend changing the stop code to 255 .

2) When decoding, a has never been defined

You need to get the length of the message by another means. I suggest doing this with a counter:

counter = 0
while something:
    counter += 1

# do something with count here

All in all, a working version of your code would look like:

e=input('file and location?  ')
img=Image.open(e)
pmap=img.load()
imy=img.height
imx=img.width
if int(input('1 for encoding, 2 for decoding   '))==1:
    a=input('Your message?   ')
    for i in range(len(a)):
        r , g , b= img.getpixel((i+10,imy//2))
        img.putpixel((i+10,imy//2),(ord(a[i]),g,b))
    r,g,b=img.getpixel((len(a)+10,imy//2))
    img.putpixel((len(a)+10,imy//2),(255,g,b)) #255 is the stop code in     decoding
else:
    r=u=0
    m=''
    message_length=0
    while r!=255:
        message_length+=1
        r , g , b=img.getpixel((10+u,imy//2))
        m+=chr(r)
        u+=1
    print(m[:message_length-1])
img.save(e) 

The difference is there, but it's just a few single pixels. If I calculate the difference between original and new image, you'll see it in the middle left, stored in test2.png. In order to enhance contrast I have "equalized" the image.

from PIL import Image, ImageChops, ImageOps

img=Image.open("image.jpg")
pmap=img.load()
img2=img.copy()
imy=img.height
imx=img.width
if int(input('1 for encoding, 2 for decoding   '))==1:
    a=input('Your message?   ')
    for i in range(len(a)):
        r , g , b=img.getpixel((i+10,imy//2))
        img.putpixel((i+10,imy//2),(ord(a[i]),g,b))
    r,g,b=img.getpixel((len(a)+10,imy//2))
    img.putpixel((len(a)+10,imy//2),(999,g,b)) #999 is the stop code in     decoding
else:
    r=u=0
    m=''
    while r!=999:
        r , g , b=img.getpixel((10+u,imy//2))
        m+=chr(r)
        u+=1
    print(m[:len(a)-1])
img.save("test.png")
img3=ImageChops.difference(img, img2)
img3=ImageOps.equalize(img3)
img3.save("test2.png")

This is the result: 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM