-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.py
32 lines (28 loc) · 1.13 KB
/
encode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from PIL import Image
# Function to encode a message into an image
def encode_image(image_path, message, output_path):
img = Image.open(image_path)
binary_message = ''.join(format(ord(char), '08b')
for char in message) # Convert message to binary
# Append null terminator to the binary message
binary_message += '00000000'
index = 0
encoded_pixels = []
for pixel in img.getdata():
if index < len(binary_message):
red, green, blue = list(pixel)
red = red & ~1 | int(binary_message[index])
index += 1
if index < len(binary_message):
green = green & ~1 | int(binary_message[index])
index += 1
if index < len(binary_message):
blue = blue & ~1 | int(binary_message[index])
index += 1
encoded_pixels.append((red, green, blue))
else:
encoded_pixels.append(pixel)
# Create a new image with the encoded pixels
encoded_img = Image.new(img.mode, img.size)
encoded_img.putdata(encoded_pixels)
encoded_img.save(output_path)