Update app.py

This commit is contained in:
pliny
2024-10-12 18:38:46 -04:00
committed by GitHub
parent bf4a94d563
commit fd6ebaa6a5

16
app.py
View File

@@ -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):