-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPIXELEncoder.py
More file actions
36 lines (36 loc) · 1.22 KB
/
Copy pathPIXELEncoder.py
File metadata and controls
36 lines (36 loc) · 1.22 KB
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 os
from PIL import Image
def create_metadata(length):
return length.to_bytes(4, byteorder='big')
def generate_palette():
palette = []
for i in range(256):
r = (i * 5) % 256
g = (i * 7) % 256
b = (i * 13) % 256
palette.extend([r, g, b])
return palette
def data_to_color_index(data_byte):
return data_byte
def encode(data):
metadata = create_metadata(len(data))
binary_data = bytearray(metadata) + bytearray(data)
palette = generate_palette()
pixel_indices = [data_to_color_index(byte) for byte in binary_data]
width = int(len(pixel_indices) ** 0.5) + 1
height = (len(pixel_indices) // width) + 1
img = Image.new('P', (width, height), 0)
img.putpalette(palette)
img.putdata(pixel_indices)
return img
def save_file():
print(f"Welcome to PIXEL!")
input_path = input("Enter the path to the file you want to encode: ")
with open(input_path, "rb") as f:
file_data = f.read()
img = encode(file_data)
base_name, ext = os.path.splitext(input_path)
output_path = f"{base_name}{ext}.png"
img.save(output_path, "PNG", optimize=True)
print(f"Encoded file saved as: {output_path}")
save_file()