-
Notifications
You must be signed in to change notification settings - Fork 1
/
3_Packet2Image.py
65 lines (48 loc) · 1.64 KB
/
3_Packet2Image.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
# Author: @vinesmsuic
#
#
import numpy as np
import pandas as pd
import os
import pickle
from PIL import Image
import binascii
from tqdm import tqdm
def to_hex(byte_list):
bytelist = []
for b in byte_list:
bytelist.append(binascii.hexlify(bytes(b)))
return bytelist
def matrix_from_hex(hex_list):
matrice = []
for hex_str in hex_list:
matrix = np.array([int(hex_str[i:i+2],16) for i in range(0, len(hex_str), 2)])
matrix = np.uint8(matrix)
matrice.append(matrix)
matrice = np.stack(matrice, axis=0)
return matrice
def get_save_path(one_row_df, save_folder):
base = os.path.basename(one_row_df['Path'])
filename = os.path.splitext(base)[0]
ImagePath = os.path.join(save_folder,filename)
return ImagePath
def main():
searching_folder = "3_Packet"
saving_folder = "4_Image"
for f in os.listdir(searching_folder):
if f.endswith(".pkl"):
folder = os.path.join(saving_folder, os.path.splitext(f)[0])
if not os.path.exists(folder):
os.makedirs(folder)
print("Saving to: " + str(folder))
file = open(os.path.join(searching_folder, f), 'rb')
df = pickle.load(file)
file.close()
df['Hex'] = df['Bytes'].apply(to_hex)
df['Matrice'] = df['Hex'].apply(matrix_from_hex)
for index, row in tqdm(df.iterrows()):
ImagePath = get_save_path(row, folder)
im = Image.fromarray(row['Matrice'])
im.save(str(ImagePath)+".png")
if __name__ == '__main__':
main()