[13:11] Вы:

from PIL import Image def text_to_bits(text): # Convert text to a string of bits (8 bits per character) return ''.join(format(ord(c), '08b') for c in text) def bits_to_text(bits): chars = [] for i in range(0, len(bits), 8): byte = bits[i:i+8] if len(byte) < 8: break chars.append(chr(int(byte, 2))) return ''.join(chars) def encode_lsb(input_path, output_path, message, delimiter='00000000'): img = Image.open(input_path) if img.mode != 'RGB': img = img.convert('RGB') pixels = list(img.getdata()) bitstream = text_to_bits(message) + delimiter # end delimiter total_bits = len(bitstream) if total_bits > len(pixels) 3: raise ValueError("Message too long to encode in the given image.") new_pixels = [] bit_idx = 0 for (r, g, b) in pixels: if bit_idx < total_bits: r = (r & 0xFE) | int(bitstream[bit_idx]) bit_idx += 1 if bit_idx < total_bits: g = (g & 0xFE) | int(bitstream[bit_idx]) bit_idx += 1 if bit_idx < total_bits: b = (b & 0xFE) | int(bitstream[bit_idx]) bit_idx += 1 new_pixels.append((r, g, b)) if bit_idx >= total_bits: # Append the rest unchanged idx = len(new_pixels) new_pixels.extend(pixels[idx:]) break img_out = Image.new(img.mode, img.size) img_out.putdata(new_pixels[:img.size[0] img.size[1]]) img_out.save(output_path) print(f"Encoded message saved to {output_path}") def decode_lsb(input_path, delimiter='00000000'): img = Image.open(input_path) if img.mode != 'RGB': img = img.convert('RGB') pixels = list(img.getdata()) bits = [] for (r, g, b) in pixels: bits.append(str(r & 1)) bits.append(str(g & 1)) bits.append(str(b & 1)) # Optional: stop early if you detect the delimiter pattern if len(bits) >= len(delimiter) and ''.join(bits[-len(delimiter):]) == delimiter: break bitstring = ''.join(bits) # Remove final delimiter before decoding if delimiter in bitstring: bitstring = bitstring[:bitstring.index(delimiter)] text = bits_to_text(bitstring) return text

[13:11] GPT:

This Python script uses the Least Significant Bit (LSB) technique to encode and decode messages in images. Here's a breakdown of what each function does:

text_to_bits(text)

  • Converts a string of text into a string of bits (binary).
  • Each character is represented by 8 bits using format(ord(c), '08b').

bits_to_text(bits)

  • Converts a string of bits back into a readable string of text.
  • It processes the bits in chunks of 8, converting each chunk into a character.

encode_lsb(input_path, output_path, message, delimiter='00000000')

  • Encodes a message into an image using the LSB technique.
  • Opens the image specified by input_path.
  • Converts the image to RGB mode if it’s not already.
  • Converts the message to a bitstream and appends an 8-bit delimiter to mark the end of the message.
  • Checks if the message can fit into the image based on the number of available bits.
  • Iterates over each pixel, modifying the least significant bit of each color channel with bits from the message.
  • Saves the modified image to output_path.

decode_lsb(input_path, delimiter='00000000')

  • Decodes a message from an image.
  • Opens the image and ensures it’s in RGB mode.
  • Extracts the least significant bit from each color channel of each pixel.
  • Stops reading bits once it detects the delimiter.
  • Converts the bitstream back into text, excluding the delimiter.

Usage

To encode a message:

encode_lsb('input_image.png', 'output_image.png', 'Your secret message')

To decode a message:

decoded_message = decode_lsb('output_image.png')
print(decoded_message)

Notes

  • The delimiter is a sequence of eight zeros ('00000000') used to mark the end of the message. You can change this if needed, but it must match between encoding and decoding functions.
  • The script assumes the input image is large enough to contain the message. If it's not, it raises a ValueError.
  • This method is relatively simple and may not be secure against more sophisticated steganalysis techniques.