write me a python function that can embed plaintext into a .png image, and then another…
- User
write me a python function that can embed plaintext into a .png image, and then another function that can decode that plaintext out of the image.
Assistant
Sure! Here's an example implementation of two functions,
embed_textanddecode_text, that you can use to embed plaintext into a PNG image and then decode that plaintext out of the image:import base64 from PIL import Image def embed_text(text, image_path, output_path): """ Embed plaintext into a PNG image and save it to a file. """ # Create a new image with the text embedded image = Image.open(image_path) draw = ImageDraw.Draw(image) draw.text((0, 0), text, fill='rgba(255, 255, 255, 0.5)') # Set the text color to white and lightly transparent image = image.convert('RGBA') # Convert the image to RGBA mode (transparency) # Encode the image as a base64 string and save it to a file image_data = base64.b64encode(image.tobytes()).decode() with open(output_path, 'wb') as f: f.write(image_data) def decode_text(image_path, output_path): """ Decode plaintext from a PNG image and save it to a file. """ # Open the image file image = Image.open(image_path) # Get the image data as a base64 string image_data = image.tobytes() # Decode the base64 string to get the image data in bytes image_data = base64.b64decode(image_data) # Save the image data to a file with open(output_path, 'wb') as f: f.write(image_data)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.