Advertisement
xosski

Binary encoding/decoding images

Jan 17th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. Encapsulating
  2. from PIL import Image
  3.  
  4. def embed_data(image_path, data, output_path):
  5. image = Image.open(image_path)
  6. binary_data = ''.join(format(ord(char), '08b') for char in data)
  7. data_len = len(binary_data)
  8. pixels = list(image.getdata())
  9.  
  10. for i in range(data_len):
  11. pixel = list(pixels[i])
  12. pixel[0] = pixel[0] & ~1 | int(binary_data[i])
  13. pixels[i] = tuple(pixel)
  14.  
  15. image.putdata(pixels)
  16. image.save(output_path)
  17.  
  18. # Example usage
  19. executable_code = """
  20. import os
  21. os.system('echo Hello, World!')
  22. """
  23. embed_data('input_image.png', executable_code, 'output_image.png')
  24. ////////////////////////////////////////
  25. from PIL import Image
  26.  
  27. def extract_data(image_path):
  28. image = Image.open(image_path)
  29. binary_data = ""
  30. pixels = list(image.getdata())
  31.  
  32. for pixel in pixels:
  33. binary_data += str(pixel[0] & 1) # Extract LSB of red channel
  34.  
  35. byte_data = [binary_data[i:i+8] for i in range(0, len(binary_data), 8)]
  36. decoded_data = ''.join([chr(int(byte, 2)) for byte in byte_data])
  37. return decoded_data
  38.  
  39. # Example usage
  40. hidden_code = extract_data('output_image.png')
  41. exec(hidden_code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement