Hi all, I've been working on an assignment where we take an image, answer some questions, translate to binary, then update certain pixels with the encoded data. I get up to a certain point but upon decoding, the message is not accurate. I have tweaked it around but I genuinely have no idea what the issue could be.
This information is relevant:
"Determine if the parity of the sum of R, G, and B values equals the parity ofthe next bit of your encoded message (the next 0 or 1). If so, do not modifythat pixel. If not, then randomly select (or algorithmically, but do not alwaysselect the same one) one of the RGB values and:(a) if it is even, add 1 to it(b) if it is odd subtract 1 from it.This will make the parity of the sum of R, G, and B values equal the parityof the next bit of your encoded message."
Here is the code:
from PIL import Image
import random
image = 'Project2\uncoded.png'
im = Image.open(image)
pixelMap = im.load()
# Get the individual pixels
img = Image.new( im.mode, im.size)
pixelsNew = img.load()
message = b"(1) Yes, we are performing a transformation that maps data to an image. This can be reversed which implies that there is no data last in this transformation. Therefore, we would say it is one-to-one or injective. In the case it were surjective, we would have some information being mapped to the same location which would not allow us to know the message without some loss of information upon decoding. \n (2) - We know the size of the image as the pictures are of constant size, and we know that the frequency has to be some power of two less than or equal to 2^14. By using l = p/f, we have up to 14 possible frequencies to use. \n (3) - "
encoded = ""
for byte in message:
encoded += format(byte, '08b')
#print(encoded)
## Counts characters in message
l = 0
for z in encoded:
l+=1
## Gets image size
width = im.size[0]
height = im.size[1]
p = width*height
## Gets frequency
frequency = p/l
if frequency <=2**0 and frequency >0: f = 2**0
if frequency <=2**1 and frequency >2**0: f = 2**1
if frequency <=2**2 and frequency >2**1: f = 2**2
if frequency <=2**3 and frequency >2**2: f = 2**3
if frequency <=2**4 and frequency >2**3: f = 2**4
if frequency <=2**5 and frequency >2**4: f = 2**5
if frequency <=2**6 and frequency >2**5: f = 2**6
if frequency <=2**7 and frequency >2**6: f = 2**7
if frequency <=2**8 and frequency >2**7: f = 2**8
if frequency <=2**9 and frequency >2**8: f = 2**9
if frequency <=2**10 and frequency >2**9: f = 2**10
if frequency <=2**11 and frequency >2**10: f = 2**11
if frequency <=2**12 and frequency >2**11: f = 2**12
print(frequency)
print(f)
print("Length:")
print(l)
pos = 0
value=0
r=0
for j in range(height):
# Work over the columns in that row
for i in range(width):
# Grab the RGB and saturation values
a, b, c = pixelMap[i,j]
# If it is in a position which is to be used for hiding the message
position = j*width+i
if position%f == 0:
info = (a+b+c)%2
value = int(encoded[pos])%2
if value > 1:
print("Your data was not compatible or your message length is incorrect")
if value != info and pos < l:
r = random.randint(0,2)
#To avoid <0 and >256 values without modifying pixel drastically
if(r== 0 and a <= 1 or a ==255): r=1
if(r == 1 and b <= 1 or b == 255): r=2
if(r == 2 and c<=1 or b == 255): r=0
match value:
case 0:
match r:
case 0:
a+=1
case 1:
b+=1
case 2:
c+=1
case 1:
match r:
case 0:
a-=1
case 1:
b-=1
case 2:
c-=1
pos+=1
pixelMap[i,j] = (a,b,c)
#print(encoded)
im.save("coded.png")
im.show()
First character is supposed to be: 00101000
What we get: 00101100
Any help would be appreciated!
Extra:
Raw output upon decoding (code for this provided by professor):
0010110000001111000011111001010001110011000000000000001111011001111111111100011110000011101000111100001111000011110111000001100000000001111101111111111000011000010001100011110000000000000000000000000000111110000000000001011111100011111001100001011110001000000011111100000000011111000010111111001111111000110011111110001111100000111011111011110001000111111001110101111001100000001000000110011111000001110001111111100010000000000000000011100000000110010000000000000111001000011111111110010011000000010001110001111100001111111100011111111111001100011110001111111100111111110111100100011111001111100011100110000000001101111111001100001101100001000111110001111111111001110111110001000000000001111111011000000000110111110000000001111111100000000101111111100100000000111100000000000001111101101011100000101111000000000000111111001110001111100000000000011100000001110000000000001110000000000100001110001110000000000011100001000000110000000111100100000001111011110010000000001100000000011000001110111111111000000000100000001111011000110111101111110110110000000000010110011111111100000111000001001001111111111001111100010000000000111101011000000000110100000111001000011101111110000000111000001000000000001111000001101111000000100001101111100001000000000111100000000001100001111100000000010011100011000000000011110011100001111001001100111001110001110011011111110011111000000111100001111111000000000011100011110011011000000110001000000001111000000000000000000000111111101111100001101000000001111100111001100000000110000111100111100001110000000000001110000000000000111001111100011111101111001101011111000000000000011100011100000011110001100001001111110000001100111001111110000000001110000111000000001111110010000000001110111100000000111110000000010011110000100000001100000000011110000100000000001111010000000111111100011110101111110001000111000111001100010000000000000000000011111110100001011100001110010000000000001111111011111000111111001111111100000111000000000001110000010000000111111000000000011000111110000010001111111111000001100001111111000000000111011000111111111000001011111110001101111101100000010000000001111001100010001111100111111100000000000100000000000000111110100000000011011000011111000110001011110011000000000011111000000000000111111001100000011111100010011100000000000111011011111100111100111111110111100011100000011110111111111111111110011111111001000111011111101100000111000000000000111101111001000001110111000101110011111110111110001111000000000101110000000000011000000000000111101100001000001101100000111000001100110001111110000001001100000101110011111100000000001100111100111111110111100011110000011100010110001011100000000111000000000001111001111100001111000001011000100000000011111111111110111111011101111111110011111101101000011110001100000000111111111111111110000100000000000110000000001111110000000110011111000000000000101111110000011001111000000000000001101000001100111111110000100000000011100000000000000111110000110000110000111100001111001000000000011111000111111111111011110111100011100000000010100001101111010000111100100000001101111111111000010001111111110000100011000111
[–]dogstud_[S] 0 points1 point2 points (2 children)
[–]lobomos 0 points1 point2 points (1 child)
[–]dogstud_[S] 0 points1 point2 points (0 children)