-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
145 lines (117 loc) · 4 KB
/
main.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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
main.py
This script implements a simple steganography tool for hiding messages in images and also provides metadata of encoded image.
Author: Pavan Shanmukha Madhav Gunda
Date: 2024 - 09 - 28
Usage:
- Run the script to interactively encode or decode messages and view the metadata.
- Ensure all the libraries are installed.
"""
import cv2
from bitarray import bitarray
import requests
import json
def Encoder(Source, Message, Destination):
img = cv2.imread(Source)
if img is None:
print("Error: Image not found")
return
# Image details for metadata
height, width, _ = img.shape
total_pixels = width * height
ba = bitarray()
ba.frombytes(Message.encode('utf-8'))
ba.extend('0' * 40)
req_pixels = len(ba)
if req_pixels > total_pixels * 3:
print("ERROR: Need larger file size")
return
index = 0
for p in range(height):
for q in range(width):
if index < req_pixels:
for c in range(3):
if index < req_pixels:
img[p][q][c] = (img[p][q][c] & 0xFE) | ba[index]
index += 1
cv2.imwrite(Destination, img)
print("Image Encoded Successfully")
# Return image metadata
return {
"width": width,
"height": height,
"message": Message,
"image_path": Destination # You might want to specify the URL if hosted somewhere
}
def send_metadata(metadata, token):
# Send metadata to API
url = "https://api.apyhub.com/processor/image/metadata/file"
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
response = requests.post(url, headers=headers, data=json.dumps(metadata))
# Debugging output
print("Sending request to:", url)
print("Headers:", headers)
print("Payload:", json.dumps(metadata))
if response.status_code == 200:
print("Metadata sent successfully:", response.json())
else:
print("Failed to send metadata:", response.status_code, response.text)
def Decoder(Source):
img = cv2.imread(Source)
height, width, _ = img.shape
total_pixels = width * height
ba = bitarray()
for p in range(height):
for q in range(width):
for c in range(3):
ba.append(img[p][q][c] & 1)
message_bytes = bytearray()
for i in range(0, len(ba), 8):
byte = ba[i:i+8].tobytes()
message_bytes.append(int.from_bytes(byte, byteorder='big'))
padding_index = message_bytes.find(0)
if padding_index != -1:
message = message_bytes[:padding_index].decode('utf-8')
print("Hidden Message:", message)
else:
print("No Hidden Message Found")
def MetaStego():
token = "<YOUR_APYHUB_TOKEN_HERE>" # Place your token here
metadata = None # To store the metadata
while True:
print("--Welcome to MetaStego--")
print("1: Encoder")
print("2: Decoder")
print("3: Send Metadata")
print("4: Exit")
func = input("Choose an option: ")
if func == '1':
print("Enter Source Image Path")
src = input()
print("Enter Message to Hide")
message = input()
print("Enter Destination Image Path")
dest = input()
print("Encoding...")
metadata = Encoder(src, message, dest) # Store the returned metadata
elif func == '2':
print("Enter Source Image Path")
src = input()
print("Decoding...")
Decoder(src)
elif func == '3':
if metadata is None:
print("ERROR: No metadata available. Please encode an image first.")
else:
print("Sending metadata...")
send_metadata(metadata, token)
elif func == '4':
print("Exiting MetaStego...")
break
else:
print("ERROR: Invalid option chosen")
if __name__ == "__main__":
MetaStego()