-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagecipher.py
More file actions
60 lines (49 loc) · 1.95 KB
/
imagecipher.py
File metadata and controls
60 lines (49 loc) · 1.95 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from PIL import Image
import os
def text_to_image(text, image_filename):
# Check if the image file exists
if os.path.isfile(image_filename):
# If the file exists, open the preexisting image and convert it to RGB
image = Image.open(image_filename).convert("RGB")
else:
# If the file does not exist, create a new image with a default size
image = Image.new("RGB", (200, 200), "white")
pixels = list(image.getdata())
width, height = image.size
# Flatten the pixel list for easy manipulation
flat_pixels = [list(pixel) for pixel in pixels]
# Encode the text into the image
text += chr(0) # Null terminator for the end of the message
index = 0
for i in range(len(flat_pixels)):
if index < len(text):
flat_pixels[i][0] = ord(text[index])
index += 1
else:
break
# If the message is longer than the image can hold, throw an error
if index < len(text):
raise ValueError("The text is too long to fit in the provided image.")
# Convert the flat pixel list back to tuples
new_pixels = [tuple(pixel) for pixel in flat_pixels]
# Create a new image with the modified pixels
new_image = Image.new(image.mode, (width, height))
new_image.putdata(new_pixels)
# Save the new image
new_image.save(image_filename)
print(f"Image saved as {image_filename}")
def image_to_text(image_filename):
try:
# Open the image and convert it to RGB
image = Image.open(image_filename).convert("RGB")
pixels = list(image.getdata())
# Decode the text from the image
chars = []
for pixel in pixels:
char = chr(pixel[0])
if char == chr(0): # Null terminator indicates the end of the message
break
chars.append(char)
return ''.join(chars)
except Exception as e:
return f"Error: {e}"