From fd6ebaa6a573159df9a480caa7af6263bd20dc50 Mon Sep 17 00:00:00 2001 From: pliny <133052465+elder-plinius@users.noreply.github.com> Date: Sat, 12 Oct 2024 18:38:46 -0400 Subject: [PATCH] Update app.py --- app.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 8507490..ff6b202 100644 --- a/app.py +++ b/app.py @@ -19,12 +19,12 @@ def encode_text_into_image(image_path, text, output_path): img = Image.open(image_path) img = img.convert("RGBA") # Ensure image has alpha channel - # Convert text to binary, make sure it fits into the image, and apply controlled redundancy + # Convert text to binary, ensure it fits in the image binary_text = ''.join(format(ord(char), '08b') for char in text) + '00000000' # Add terminator width, height = img.size pixel_capacity = width * height * 3 # Capacity based on RGB channels - # Ensure the binary message fits + # Ensure the binary message fits within the pixel capacity if len(binary_text) > pixel_capacity: raise ValueError("The message is too long for this image.") @@ -34,15 +34,15 @@ def encode_text_into_image(image_path, text, output_path): if index < len(binary_text): r, g, b, a = img.getpixel((x, y)) - # Modify the least significant bits of R, G, B channels - r = (r & 0xFE) | int(binary_text[index]) - g = (g & 0xFE) | int(binary_text[(index + 1) % len(binary_text)]) - b = (b & 0xFE) | int(binary_text[(index + 2) % len(binary_text)]) + # Only modify the least significant bit (LSB) of R, G, and B channels + r = (r & 0xFE) | int(binary_text[index]) # LSB of red + g = (g & 0xFE) | int(binary_text[(index + 1) % len(binary_text)]) # LSB of green + b = (b & 0xFE) | int(binary_text[(index + 2) % len(binary_text)]) # LSB of blue img.putpixel((x, y), (r, g, b, a)) - index += 3 # Move to the next set of bits + index += 3 # Move to the next set of 3 bits - # Save the encoded image + # Save the encoded image before any compression img.save(output_path, optimize=True, format="PNG") def compress_image_if_needed(output_image_path):