-
Notifications
You must be signed in to change notification settings - Fork 0
/
PicHide.py
170 lines (148 loc) · 5.15 KB
/
PicHide.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from PIL import Image, ImageOps
import stepic
import os
class Hidder(object):
#*********************** Coding ****************************
# hide zip in image
# srcPath: input image path
# targetPath: hidden zip path
# outputPath: final output path, default: ./final.jpg/png/bmp/...etc
def hideZip(self, srcPath, targetPath, outputPath):
# open the file
try:
im_src = open(srcPath, 'rb')
except IOError:
print 'can not open the %s' % srcPath
exit(0)
try:
zip_src = open(targetPath, 'rb')
except IOError:
print 'can not open the %s' % targetPath
exit(0)
try:
im_dest = open(outputPath, 'wb')
except IOError:
print 'can not create the %s' % outputPath
im_dest.write(im_src.read() + zip_src.read())
im_dest.close()
zip_src.close()
im_src.close()
# hide image in image
# srcPath: input image path
# targetPath: hidden image path
# outputPath: final output path, default: ./final.jpg/png/bmp/...etc
def hideImage(self, srcPath, targetPath, outputPath, s=4):
try:
im_src = open(srcPath, 'rb')
except IOError:
print 'can not open the %s' % srcPath
exit(0)
try:
im_dest = open(outputPath, 'wb')
except IOError:
print 'can not open the %s' % outputPath
exit(0)
im_dest.write(im_src.read())
im_dest.close()
im_src.close()
try:
src = Image.open(outputPath)
except IOError:
print 'can not open the %s' % srcPath
exit(0)
key = ImageOps.autocontrast(Image.open(targetPath).resize(src.size))
for x in range(src.size[0]):
for y in range(src.size[1]):
p = src.getpixel((x,y))
q = key.getpixel((x,y))
r = p[0] - (p[0] % s) + (s * q[0] / 255)
g = p[1] - (p[1] % s) + (s * q[1] / 255)
b = p[2] - (p[2] % s) + (s * q[2] / 255)
src.putpixel((x,y), (r, g, b))
src.save(outputPath)
# hide text in image
# srcPath: input image path
# targetPath: hidden text path
# outputPath: final output path, default: ./final.jpg/png/bmp/...etc
def hideText(self, srcPath, targetPath, outputPath):
try:
text_src = open(targetPath, 'rb')
except IOError:
print 'can not open the %s' % targetPath
exit(0)
self.hideString(srcPath, text_src.read(), outputPath)
text_src.close()
# hide text in image
# srcPath: input image path
# targetPath: hidden text path
# outputPath: final output path, default: ./final.jpg/png/bmp/...etc
def hideString(self, srcPath, string, outputPath):
try:
im_src = Image.open(srcPath)
except IOError:
print 'can not open the %s' % srcPath
exit(0)
im_src = stepic.encode(im_src, string)
im_src.save(outputPath)
im_src.close()
#*********************** Decode ****************************
# display zip in image
# srcPath: input image path
# targetPath: output zip path, default:
def displayZip(self, srcPath, targetPath):
os.system('unzip -o ' + srcPath + ' -d ' + targetPath)
# display Image in image
# srcPath: input image path
# targetPath: output zip path, default:
def displayImage(self, srcPath, targetPath, s=4):
try:
im_src = open(srcPath, 'rb')
except IOError:
print 'can not open the %s' % srcPath
exit(0)
try:
im_dest = open(targetPath, 'wb')
except IOError:
print 'can not open the %s' % targetPath
exit(0)
im_dest.write(im_src.read())
im_src.close()
im_dest.close()
try:
src = Image.open(targetPath)
except IOError:
print 'can not open the %s' % targetPath
exit(0)
for x in range(src.size[0]):
for y in range(src.size[1]):
p = src.getpixel((x,y))
r = (p[0] % s) * 255 / s;
g = (p[1] % s) * 255 / s;
b = (p[2] % s) * 255 / s;
src.putpixel((x,y), (r, g, b))
src.save(targetPath);
# display text in image
# srcPath: input image path
def displayText(self, srcPath, targetPath):
try:
text_dest = open(targetPath, 'wb')
except IOError:
print 'can not create the %s' % targetPath
exit(0)
text_dest.write(self.displayString(srcPath, False))
text_dest.close()
# display string in image
# srcPath: input image path
# targetPath: output zip path, default:
def displayString(self, srcPath, isDisplay=True):
try:
im_src = Image.open(srcPath)
except IOError:
print 'can not open the %s' % srcPath
exit(0)
# im_src.show()
str_dest = stepic.decode(im_src)
data = str_dest.decode()
if isDisplay:
print data
return data