-
Notifications
You must be signed in to change notification settings - Fork 1
/
steganography.py
134 lines (109 loc) · 4.07 KB
/
steganography.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
import readline
import os
import math
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def Main():
readline.parse_and_bind("tab: complete")
mode = int(input("モードを選択してください 1:モノクロエンコード 2:カラーエンコード 3:デコード\n>"))
if mode == 1:
MonoEncode()
elif mode == 2:
ColorEncode()
elif mode == 3:
Decode()
def MonoEncode():
imgFile = input("対象の画像ファイルのパスを入力してください(相対パス対応)\n>")
text = input("隠したい文字を入力してください\n>")
cwd = os.getcwd()
imgFilePath = os.path.join(cwd,imgFile)
img1 = Image.open(imgFilePath)
img2 = AutoImageCreate(imgFilePath,text)
print("import img success")
img1 = img1.convert("L")
print("mono img success")
img1 = img1.point(lambda x: x-1 if x%2 == 1 else x)
print("even img success")
img = MonoMergeImg(img1,img2).convert("RGB")
print("merge img success")
saveName = input("保存するファイル名を決めてください (hidden.png)\n>")
if not saveName:
saveName = "hidden.png"
saveFilePath = os.path.join(cwd,saveName)
img.save(saveFilePath,format="png")
def MonoMergeImg(img1,img2):
width,height = img1.size
img2data = img2.getdata()
for h in range(height):
hcache = h * width
for w in range(width):
if img2data[hcache+w] == 1:
pix = img1.getpixel((w,h))
img1.putpixel((w,h),pix+1)
return img1
def ColorEncode():
imgFile = input("対象の画像ファイルのパスを入力してください(相対パス対応)\n>")
text = input("隠したい文字を入力してください\n>")
cwd = os.getcwd()
imgFilePath = os.path.join(cwd,imgFile)
img1 = Image.open(imgFilePath).convert("RGB")
img2 = AutoImageCreate(imgFilePath,text)
print("import img success")
img1 = EvenImg(img1)
print("even img success")
img1 = ColorMergeImg(img1,img2)
print("merge img success")
img1.save('hidden.png')
def EvenImg(img):
r,g,b = img.split()
b = b.point(lambda x: x-1 if x%2 == 1 else x)
img = Image.merge("RGB",(r,g,b))
return img
def ColorMergeImg(img1,img2):
width,height = img1.size
img2data = img2.getdata()
for h in range(height):
hcache = h * width
for w in range(width):
if img2data[hcache+w] == 1:
r,g,b = img1.getpixel((w,h))
img1.putpixel((w,h),(r,g,b+1))
return img1
def AutoImageCreate(image_path,text):
# 使うフォント,サイズ,描くテキストの設定
ttfontname = "/usr/share/fonts/fonts-go/Go-Mono-Bold.ttf"
fontsize = 100
# 画像サイズ,背景色,フォントの色を設定
img = Image.open(image_path)
canvasSize = img.size
background = 0
textColor = 1
# 文字を描く画像の作成
img = Image.new('L', canvasSize, background)
draw = ImageDraw.Draw(img)
# 用意した画像に文字列を描く
font = ImageFont.truetype(ttfontname, fontsize)
textWidth, textHeight = draw.textsize(text,font=font)
textTopLeft = (canvasSize[0]//2-textWidth//2, canvasSize[1]//2-textHeight//2)
draw.text(textTopLeft, text, fill=textColor, font=font)
return img
def Decode():
imgFile = input("対象の画像ファイルを選択してください\n>")
cwd = os.getcwd()
imgFilePath = os.path.join(cwd,imgFile)
img = Image.open(imgFilePath).split()[2]
img = img.point(lambda x: x%2)
print("modulo img success")
img = img.point(lambda x: x*255).convert("L")
print("x255 img success")
img.show()
saveFlag = input("ファイルを保存しますか? y/n\n>")
if saveFlag == "y" or saveFlag == "yes":
saveName = input("保存するファイル名を決めてください (default:appear_key.png)\n>")
if not saveName:
saveName = "appear_key.png"
saveFilePath = os.path.join(cwd,saveName)
img.save(saveFilePath,format="png")
if __name__ == "__main__":
Main()