-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
36 lines (35 loc) · 1.49 KB
/
start.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
import tkinter as tk
from tkinter import filedialog
import numpy as np
from PIL import Image
import re,struct
def encode_file():
for filepath in filedialog.askopenfilenames():
with open(filepath, "rb") as file:
data = file.read()
file_bytes = struct.pack(">Q", len(data)) + data
num_padding_bytes = 2048 - len(file_bytes) % 2048
image = np.frombuffer(file_bytes + b"\0" * num_padding_bytes, dtype=np.uint8).reshape((-1, 2048))
for i in range(0, image.shape[0], 2048):
Image.fromarray(image[i:i+2048], "L").save(f"{filepath}.{i//2048}.png")
print("The files have been successfully encoded!")
def decode_file():
for filepath in filedialog.askopenfilenames():
if ".0.png" not in filepath: continue
image_data = []
i = 0
while True:
try: image_data.append(np.array(Image.open(f"{filepath[:-6]}.{i}.png")))
except: break
i += 1
file_bytes = np.concatenate(image_data).tobytes()
file_size = int.from_bytes(file_bytes[:8], 'big')
with open(re.sub(r"\.\d+\.png$", "", filepath), "wb") as output_file:
output_file.write(file_bytes[8:file_size+8])
print("The files have been successfully decoded!")
root = tk.Tk()
root.title("Encoding/decoding a file")
root.geometry('100x75')
tk.Button(text="Encode the file", command=encode_file).pack()
tk.Button(text="Decode the file", command=decode_file).pack()
root.mainloop()