-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.py
More file actions
76 lines (60 loc) · 2.5 KB
/
Copy pathencode.py
File metadata and controls
76 lines (60 loc) · 2.5 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
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
from math import ceil
import random
import sys
from PIL import Image
MAX_COMPRESSION_DELTA = 16
MESSAGE_END_MARKER = b"\x00"
def nearest_neighbour_upscale(pixel_data, width, height, scale):
pixel_data_2d = [pixel_data[i:i + width // scale]
for i in range(0, len(pixel_data), width // scale)]
new_pixel_data_2d = []
for row in pixel_data_2d:
new_row = []
for pixel in row:
new_row += [pixel] * scale
if len(new_row) < width:
new_row += [row[-1] for _ in range(width - len(new_row))]
new_pixel_data_2d += [new_row] * scale
if len(new_pixel_data_2d) < height:
new_pixel_data_2d += [new_pixel_data_2d[-1]
for _ in range(height - len(new_pixel_data_2d))]
new_pixel_data = [pixel for row in new_pixel_data_2d for pixel in row]
return new_pixel_data
def rand_colour_channel():
return random.randint(0, 255)
def message_to_pixels(message, bits_available):
binary = "".join(f"{char:08b}" for char in message)
colours = []
for bit in binary:
if bit == "0":
colours.append(random.randint(0, 128 - MAX_COMPRESSION_DELTA))
else:
colours.append(random.randint(128 + MAX_COMPRESSION_DELTA, 255))
colours += [rand_colour_channel()
for _ in range(bits_available - len(colours))]
assert len(colours) % 3 == 0
return [(colours[i], colours[i + 1], colours[i + 2]) for i in range(0, len(colours), 3)]
if __name__ == "__main__":
if len(sys.argv) != 5 and len(sys.argv) != 6:
print("Usage: %s <width> <height> <message> <output_image> [colour_size]" %
sys.argv[0])
sys.exit(1)
width = int(sys.argv[1])
height = int(sys.argv[2])
message = sys.argv[3].encode()
output_image = sys.argv[4]
colour_size = 1 if len(sys.argv) == 5 else int(sys.argv[5])
message += MESSAGE_END_MARKER
bits_available = (width // colour_size) * (height // colour_size) * 3
bits_required = ceil(len(message) * 8 / 6) * 6
if bits_required > bits_available:
print(
f"Message too long to encode in image. Need {bits_required} bits, but only have {bits_available} bits available.")
sys.exit(1)
pixel_data = message_to_pixels(message, bits_available)
if colour_size > 1:
pixel_data = nearest_neighbour_upscale(
pixel_data, width, height, colour_size)
image = Image.new("RGB", (width, height))
image.putdata(pixel_data)
image.save(output_image)