What Python code is used to encode a message in an image?
Here's a simple example of how you could encode a message in an image using Python:
from PIL import Image
# Open the image
img = Image.open("image.png")
# Get the pixel data as a list of tuples
pixels = list(img.getdata())
# Encode the message into the least significant bit of each pixel
encoded_pixels = [(r & ~3) + ord(c) % 4 for (r, g, b), c in zip(pixels, message)]
# Create a new image with the encoded pixels
img.putdata(encoded_pixels)
# Save the image
img.save("encoded_image.png")
In this example, the message is encoded into the least significant bit of each pixel by replacing the last two bits of each red component of the pixels with two bits of the message. This encoding is lossless, so the original image can be recovered.
Please note that this is just a simple example and not a robust solution for hiding data in an image. There are many better ways to encode data in images, including using steganography techniques.