-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixelGhost.py
More file actions
162 lines (141 loc) · 5.57 KB
/
PixelGhost.py
File metadata and controls
162 lines (141 loc) · 5.57 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# PixelGhost
# A tool which hides the messages in the image using LSB method.
# Author - WireBits
import os
import argparse
from PIL import Image
def encode_message(originalImage, stegImage, message):
if not os.path.exists(originalImage):
print("Original Image does not exist!")
return
if not message:
print("Enter some message to hide!")
return
image = Image.open(originalImage)
message += '\x00'
encImage = encodeMessageInPixels(image.copy(), message)
encImage.save(stegImage)
print("Message encoded successfully!")
def encode_file(originalImage, stegImage, filepath):
if not os.path.exists(originalImage):
print("Original Image does not exist!")
return
if not os.path.exists(filepath):
print("File to hide does not exist!")
return
with open(filepath, 'rb') as file:
file_data = file.read()
file_data = file_data.decode('latin-1') + '\x00'
image = Image.open(originalImage)
encImage = encodeMessageInPixels(image.copy(), file_data)
encImage.save(stegImage)
print("File encoded successfully!")
def decode_message(stegImage, outputFile=None):
if not os.path.exists(stegImage):
print("Steg Image does not exist!")
return
image = Image.open(stegImage)
hidden_message = decode_image(image)
hidden_message = hidden_message.replace('\n', '')
if outputFile:
if not outputFile.lower().endswith('.txt'):
outputFile += '.txt'
with open(outputFile, 'w', encoding='latin-1') as file:
file.write(hidden_message)
print("Hidden message saved locally!")
else:
print("Hidden Message: ", hidden_message)
def encodeMessageInPixels(conImage, hdata):
imgSize = conImage.size[0]
(x, y) = (0, 0)
for pixel in pixelsModification(conImage.getdata(), hdata):
conImage.putpixel((x, y), pixel)
if x == imgSize - 1:
x = 0
y += 1
else:
x += 1
return conImage
def pixelsModification(picElement, hiddenData):
dataList = generateData(hiddenData)
dataLen = len(dataList)
imageData = iter(picElement)
for i in range(dataLen):
picElement = [value for value in imageData.__next__()[:3] +
imageData.__next__()[:3] +
imageData.__next__()[:3]]
for j in range(8):
if dataList[i][j] == '0' and picElement[j] % 2 != 0:
picElement[j] -= 1
elif dataList[i][j] == '1' and picElement[j] % 2 == 0:
picElement[j] += 1
picElement = tuple([min(max(0, val), 255) for val in picElement])
yield picElement[0:3]
yield picElement[3:6]
yield picElement[6:9]
def generateData(hidData):
newData = []
for z in hidData:
newData.append(format(ord(z), '08b'))
return newData
def decode_image(cipImage):
imgData = iter(cipImage.getdata())
data = ''
while True:
try:
pixels = [value for value in imgData.__next__()[:3] +
imgData.__next__()[:3] +
imgData.__next__()[:3]]
except StopIteration:
break
binaryString = ''
for w in pixels[:8]:
if w % 2 == 0:
binaryString += '0'
else:
binaryString += '1'
char = chr(int(binaryString, 2))
if char == '\x00':
break
data += char
return data
def main():
parser = argparse.ArgumentParser(description='PixelGhost')
parser.add_argument('-e', '--encode', action='store_true', help='Encode message into an image')
parser.add_argument('-d', '--decode', action='store_true', help='Decode message from an image')
parser.add_argument('-i', '--input', type=str, help='Input image file')
parser.add_argument('-m', '--message', type=str, help='Message to encode')
parser.add_argument('-f', '--file', type=str, help='File to hide inside the image')
parser.add_argument('-o', '--output', type=str, help='Output image file with encoded message or output file for decoded data')
parser.add_argument('-s', '--save', type=str, help='File to save the decoded hidden message (optional, for use with -d)')
args = parser.parse_args()
if args.encode:
if not args.output:
print("Please specify an output file name.")
return
if not args.output.lower().endswith('.png'):
args.output += '.png'
if args.file:
if not args.file.lower().endswith('.txt'):
print("Error: Only .txt files are allowed with the -f option.")
return
if not args.input:
print("Please specify input image files for encoding the file.")
return
encode_file(args.input, args.output, args.file)
elif args.message:
if not args.input:
print("Please specify input image files for encoding the message.")
return
encode_message(args.input, args.output, args.message)
else:
print("Please specify a message or file to encode.")
elif args.decode:
if not args.input:
print("Please specify an input image file for decoding.")
return
decode_message(args.input, args.save)
else:
print("Type python PixelGhost.py -h for help!")
if __name__ == "__main__":
main()