-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.py
More file actions
44 lines (41 loc) · 1.3 KB
/
encode.py
File metadata and controls
44 lines (41 loc) · 1.3 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
from PIL import Image
import sys
def encode_image(img, msg):
"""
use the red portion of an image (r, g, b) tuple to
hide the msg string characters as ASCII values
red value of the first pixel is used for length of string
"""
length = len(msg)
# limit length of message to 255
if length > 255:
print("text too long! (don't exeed 255 characters)")
return False
if img.mode != 'RGB':
print("image mode needs to be RGB")
return False
# use a copy of image to hide the text in
encoded = img.copy()
width, height = img.size
index = 0
for row in range(height):
for col in range(width):
r, g, b = img.getpixel((col, row))
# first value is length of msg
if row == 0 and col == 0 and index < length:
asc = length
elif index <= length:
c = msg[index -1]
asc = ord(c)
else:
asc = r
encoded.putpixel((col, row), (asc, g , b))
index += 1
return encoded
image = Image.open(sys.argv[1])
text_to_add = sys.argv[2]
img_encoded = encode_image(image, text_to_add)
if img_encoded:
output = "generated.png"
img_encoded.save(output)
print("your message will be hidden {}".format(output))