简体   繁体   中英

How do I get the least significant bits of many bytes?

I'm trying to get lsb from the line of an image,I managed to get here:

from PIL import Image
import sys

challengeImg = Image.open('image.png')
pixels = challengeImg.load()
for x in range(2944):
    red = (pixels[x,310][0])
    bred = format(red,"b")
    #print(green)
    #print(bred)
    green = (pixels[x,310][1])
    bgreen = format(green,"b")
    #print(bgreen)
    #print(green)

Well, until then I'm fine but now my problem, I managed to create the following code:

num = 10100001
n = 0
lsb = num >> n &1
print(lsb)

It works, but only with one byte, I suppose that with for I can achieve something but I am very beginner and I have not managed to make it work, how I can do to extract the lsb from each byte in the line of pixels of the red channel (or green, I guess it's the same procedure)?

It occurs to me that I could use a dictionary to group the bits in bytes (1: 10011001, 2: 01100110 ...) and then use the loop to apply the lsb code in each byte, anyway I do not know how I can do this and i dont think it's the best way (maybe it's not even valid).

I have a .png image of 2944x1912 that contains information hidden in the least significant bits, the first code that I put is the script that I am developing, and so far what it does is get the information of the pixels of the red channel in the line 310 and transform them into binary.

The second code is the code to get the lsb of a byte which I need to implement in the first code, so the second code should somehow group all the bits in 8 and select the last one for I save in a variable, resulting in (2944/8 = 368 = 368 bytes.)

The solution that came to me might not be the most optimal. I'll look for a better solution if it does not suffice, but in the meanwhile:

num = 10100001
num_string = str(num)
lsb_string = num_string[len(num_string)-1]
lsb = int(lsb_string)
print(lsb)

# output: 1

It works, thats the code;

from PIL import Image
import sys

challengeImg = Image.open('challenge.png')
pixels = challengeImg.load()
for x in range(2944):
    red = (pixels[x,310][0])
    bred = format(red,"b")
    #print(green)
    #print(bred)
    green = (pixels[x,310][1])
    bgreen = format(green,"b")
    #print(bgreen)
    #print(green)
    rnum = format(red,"b")
    rnum_string = str(rnum)
    rlsb_string = rnum_string[len(rnum_string)-1]
    rlsb = int(rlsb_string)
    print(rlsb, end="")

Thanks!

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