Use bytes instead of bits

This commit is contained in:
unknown 2015-10-06 21:45:38 -07:00
parent dd4bacf3ab
commit f924170032
2 changed files with 34 additions and 35 deletions

View file

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d4ce1789cfb3ed83ff177fde898526318666dbfb77b95f70b8038d4367c45dca
size 6002883
oid sha256:0a9c758425f9185dd207183b283f639cd6499d042ad75b51e3f7407624aeb8aa
size 6002882

View file

@ -125,8 +125,8 @@ def encode(imagefilename, secretfilename):
raise StegError('The Secret can\'t be 0 bytes.')
secret_extension = os.path.splitext(secretfilename)[1][1:]
secret_content_length = (len(secret) * 8) + (len(secret_extension) * 8) + 8
requiredpixels = math.ceil((secret_content_length + 32) / 3)
secret_content_length = (len(secret)) + (len(secret_extension)) + 1
requiredpixels = math.ceil(((secret_content_length * 8) + 32) / 3)
if totalpixels < requiredpixels:
raise StegError('Image does not have enough pixels to store the Secret'
'Must have at least %d pixels' % requiredpixels)
@ -137,7 +137,7 @@ def encode(imagefilename, secretfilename):
pixel = list(image.getpixel((0, 0)))
# Write secret length
secret_content_length_b = bin(secret_content_length)[2:].rjust(32, '0')
secret_content_length_b = binary(secret_content_length).rjust(32, '0')
for x in range(32):
modify_pixel(secret_content_length_b[x])
increment_pixel()
@ -209,7 +209,7 @@ def decode(imagefilename):
pixel_index += 1
content_length = content_length[:-1]
content_length = int(content_length, 2)
print('Content bits:', content_length)
print('Content bytes:', content_length)
# Continue from the end of the header
# This would have been automatic if I used `increment_pixel`
@ -231,8 +231,8 @@ def decode(imagefilename):
print('Extension:', extension)
# Remove the extension length
content_length -= 8
content_length -= 8 * len(extension)
content_length -= 1
content_length -= len(extension)
# Prepare writes
newname = os.path.splitext(imagefilename)[0]
@ -242,10 +242,9 @@ def decode(imagefilename):
outfile = open(newname, 'wb')
# extract data
content_bytes = content_length // 8
for byte in range(content_bytes):
for byte in range(content_length):
if byte % 1024 == 0:
percentage = (byte + 1) / content_bytes
percentage = (byte + 1) / content_length
percentage = '%07.3f%%\r' % (100 * percentage)
print(percentage, end='')
@ -254,7 +253,7 @@ def decode(imagefilename):
channel = pixel[channel_index]
channel = binary(channel)[-1]
activebyte += channel
if not (byte == content_bytes - 1 and bit == 7):
if not (byte == content_length - 1 and bit == 7):
# If your Image dimensions are at the extreme limit of the Secret size,
# this would otherwise raise IndexError as it tries to grab the next pixel
# off the canvas.