-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_image.py
62 lines (53 loc) · 2.04 KB
/
binary_image.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
import os
from PIL import Image
# 이미지를 받아와 바이너리 값으로 바꾸어 리턴
def getBinaryData(filename):
binaryValues = []
file = open(filename, "rb")
data = file.read(1)
while data !=b"":
try:
binaryValues.append(ord(data))
except TypeError:
pass
data = file.read(1)
return binaryValues
# 바이너리 값을 받아와 바이너리의 길이별로 가로 세로 비율을 지정하여 저장경로에 바이너리 이미지로 저장
def createGreyScaleImageSpecificWith(dataSet, outputfilename, width=0):
if width == 0:
size = len(dataSet)
if size < 10240:
width = 32
elif 10240 <= size <= 10240 * 3:
width = 64
elif 10240 * 3 <= size <= 10240 * 6:
width = 128
elif 10240 * 6 <= size <= 10240 * 10:
width = 256
elif 10240 * 10 <= size <= 10240 * 20:
width = 384
elif 10240 * 20 <= size <= 10240 * 50:
width = 512
elif 10240 * 50 <= size <= 10240 * 100:
width = 768
else:
width = 1024
height = int(size / width) + 1
image = Image.new('L', (width, height))
image.putdata(dataSet)
imagename = outputfilename + ".png"
image.save(imagename)
print(imagename + "GreyScale image created")
# 불러올 이미지의 경로 지정
targetdir = r"E:\정보보호\viruses-2010-05-18"
# 저장할 이미지의 경로 지정
save_dir = r"E:\정보보호\aaa"
files = os.listdir(targetdir)
# 파일의 개수만큼 반복
for i in range (0, len(files)):
file_full_path = targetdir + "/" + files[i]
save_path = save_dir + "/" + files[i]
# getBinaryData 함수를 사용하여 해당 파일의 바이너리 값를 받아옴
binaryData = getBinaryData(file_full_path)
# createGreyScaleImageSpecificWith 함수를 사용하여 바이너리값을 이미지로 변환하여 저장
createGreyScaleImageSpecificWith(binaryData, save_path)