Use bytes instead of bits
This commit is contained in:
parent
dd4bacf3ab
commit
f924170032
2 changed files with 34 additions and 35 deletions
|
@ -1,3 +1,3 @@
|
|||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:d4ce1789cfb3ed83ff177fde898526318666dbfb77b95f70b8038d4367c45dca
|
||||
size 6002883
|
||||
oid sha256:0a9c758425f9185dd207183b283f639cd6499d042ad75b51e3f7407624aeb8aa
|
||||
size 6002882
|
||||
|
|
|
@ -38,9 +38,9 @@ For each extension character, subtract 1 byte from secret size
|
|||
349,540 | 463 x 755 (349,565) | 131,072 bytes (128 kb)
|
||||
1,398,116 | 1146 x 1120 (1,398,120) | 524,288 bytes (512 kb)
|
||||
2,796,217 | 1621 x 1725 (2,796,225) | 1,048,576 bytes (1 mb)
|
||||
11,184,825 | 3500 x 3200 (11,200,000) | 4,194,304 bytes (4 mb)
|
||||
44,739,257 | 6700 x 6700 (44,890,000) | 16,777,216 bytes (16 mb)
|
||||
89,478,500 | 9500 x 9500 (90,250,000) | 33,554,432 bytes (32 mb)
|
||||
11,184,825 | 3500 x 3200 (11,200,000) | 4,194,304 bytes (4 mb)
|
||||
44,739,257 | 6700 x 6700 (44,890,000) | 16,777,216 bytes (16 mb)
|
||||
89,478,500 | 9500 x 9500 (90,250,000) | 33,554,432 bytes (32 mb)
|
||||
|
||||
'''
|
||||
from PIL import Image
|
||||
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue