-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathbuffer_to_img_test.py
131 lines (101 loc) · 11.1 KB
/
buffer_to_img_test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#
# NOTE: This is not a part of the solution, just uploading it so for posterity
#
import struct
from binascii import b2a_uu
buffer = [128,183,199,71,239,246,131,69,63,144,130,69,89,201,122,69,162,41,129,69,104,39,190,69,21,127,102,197,141,57,112,68,0,93,11,197,220,207,229,67,28,72,253,68,0,11,112,197,171,10,112,197,72,72,253,68,243,207,229,67,114,93,11,197,179,57,112,68,68,127,102,197,191,38,190,69,174,40,129,69,26,201,122,69,204,143,130,69,50,246,131,69,43,167,74,69,188,167,235,196,18,209,201,68,94,226,138,67,115,235,19,197,47,74,77,69,241,243,195,196,248,56,194,67,4,13,134,196,200,38,155,195,101,60,114,197,177,229,95,69,152,234,26,197,1,105,134,69,128,33,94,64,88,73,203,68,133,38,134,196,37,145,165,195,114,17,205,196,42,102,56,196,182,200,150,195,89,120,189,196,178,220,80,68,246,111,67,69,252,85,210,67,148,128,209,196,173,196,132,66,224,164,116,67,95,129,235,195,211,113,139,68,165,36,20,68,36,215,249,195,55,144,71,68,110,69,40,197,56,225,254,67,104,104,55,196,137,221,104,68,92,144,20,195,163,77,206,67,225,148,131,68,230,24,5,69,200,33,34,197,0,253,174,67,221,97,102,196,117,54,187,196,78,34,1,196,231,204,30,70,95,39,133,196,39,31,164,196,226,211,36,196,2,252,73,195,213,24,209,196,201,90,88,196,143,120,189,196,15,233,61,69,252,159,226,196,56,231,193,68,92,212,79,196,28,154,93,69,155,81,112,197,73,57,217,68,240,119,152,68,130,216,108,194,154,213,71,68,204,145,60,197,36,198,220,196,226,175,146,195,170,254,159,67,106,13,88,197,64,129,60,196,121,83,154,68,226,180,136,196,249,83,58,196,191,100,244,195,183,168,253,195,244,29,248,68,70,134,109,196,182,58,194,194,214,134,146,195,223,27,198,68,188,136,84,194,93,245,182,194,222,220,125,68,134,252,142,196,69,175,103,196,115,61,164,196,87,188,29,67,140,153,79,68,188,105,132,68,28,63,192,195,209,87,38,68,206,36,155,67,0,67,67,69,52,85,177,196,247,176,72,68,41,226,129,67,193,113,52,196,94,239,54,68,8,43,252,67,29,118,45,197,143,205,25,68,86,255,82,67,103,36,148,196,245,99,103,68,197,249,3,69,47,21,24,196,65,125,21,68,238,60,206,196,162,170,183,67,45,210,62,67,80,246,147,194,166,8,89,68,64,77,195,196,105,223,167,196,220,55,225,65,228,42,118,69,238,44,23,195,232,161,57,68,82,232,174,196,28,118,53,195,174,178,145,67,200,243,157,67,204,68,185,66,210,38,141,196,162,254,131,67,60,16,125,196,190,129,238,195,47,60,51,68,24,15,156,68,202,66,63,196,148,8,171,68,208,122,134,195,174,126,222,68,8,255,145,196,64,243,70,196,99,186,214,196,75,114,13,197,125,150,14,68,249,64,121,197,165,8,70,68,188,109,34,68,41,88,181,195,161,13,124,68,129,218,67,197,104,195,27,196,44,232,108,69,16,131,26,193,51,203,204,67,153,9,247,68,188,238,165,195,175,119,34,197,209,77,56,197,221,237,117,68,82,150,154,67,202,114,215,68,46,152,147,67,4,201,17,196,206,35,237,196,55,94,39,69,127,94,19,69,105,44,248,195,232,14,67,196,48,45,146,195,136,34,144,193,33,62,31,196,81,173,31,68,107,86,121,195,123,117,227,195,228,190,22,69,93,1,160,196,148,98,133,193,93,17,49,196,124,40,110,67,186,196,14,196,96,106,163,68,46,55,207,66,68,6,146,195,76,28,149,195,84,24,177,196,212,255,42,68,221,5,102,68,90,218,141,66,45,180,146,193,82,163,36,68,80,164,92,68,152,35,161,193,105,225,47,195,212,232,186,68,42,168,150,196,21,29,162,196,28,45,25,68,10,107,162,68,214,165,100,196,184,104,109,195,96,47,140,68,131,238,46,67,116,55,173,196,138,21,14,69,7,121,55,197,135,202,27,197,127,80,97,69,18,164,193,68,32,150,95,64,224,177,59,195,248,131,183,196,161,131,140,196,218,109,213,67,177,102,243,196,195,8,9,196,81,5,35,197,195,181,255,195,80,219,63,68,123,78,145,68,185,246,247,68,97,251,89,196,57,240,158,67,244,63,133,196,73,106,134,67,28,120,146,196,187,114,219,68,20,117,63,67,48,5,43,66,152,80,63,68,18,114,228,196,194,121,135,197,221,24,27,69,15,92,191,68,25,252,35,68,136,184,33,69,74,29,188,67,28,46,44,196,112,187,180,66,234,20,154,67,18,11,62,67,26,21,177,194,239,203,15,68,222,29,108,196,207,112,234,196,141,143,179,67,111,199,6,69,169,180,88,196,189,149,45,69,96,130,221,196,190,107,0,197,82,83,112,68,92,28,32,69,88,243,87,197,138,235,73,68,96,13,139,67,177,102,92,196,33,51,157,68,200,52,210,67,186,201,231,194,41,45,44,196,236,197,231,194,195,52,210,67,207,51,157,68,1,103,92,196,113,15,139,67,95,236,73,68,141,243,87,197,56,28,32,69,123,83,112,68,153,107,0,197,97,130,221,196,186,149,45,69,52,181,88,196,97,199,6,69,233,142,179,67,217,112,234,196,76,29,108,196,71,205,15,68,189,21,177,194,107,11,62,67,32,27,154,67,106,196,180,66,73,102,243,196,154,30,188,67,131,184,33,69,21,252,35,68,192,91,191,68,75,25,27,69,133,121,135,197,150,114,228,196,27,81,63,68,124,14,43,66,208,116,63,67,7,115,219,68,65,120,146,196,149,102,134,67,141,63,133,196,3,242,158,67,24,252,89,196,235,246,247,68,34,78,145,68,90,220,63,68,185,182,255,195,93,5,35,197,115,7,9,196,240,163,92,68,222,109,213,67,12,132,140,196,64,132,183,196,51,184,59,195,160,159,96,64,75,163,193,68,177,80,97,69,71,202,27,197,47,121,55,197,26,21,14,69,67,55,173,196,51,237,46,67,176,47,140,68,47,103,109,195,127,165,100,196,216,106,162,68,166,44,25,68,199,28,162,196,91,168,150,196,227,232,186,68,208,222,47,195,248,82,161,193,56,16,67,196,126,163,36,68,32,177,146,193,140,210,141,66,114,6,102,68,205,254,42,68,156,24,177,196,182,27,149,195,6,6,146,195,70,47,207,66,89,106,163,68,27,196,14,196,73,39,110,67,217,16,49,196,145,78,133,193,242,1,160,196,204,190,22,69,200,117,227,195,132,84,121,195,81,173,31,68,28,62,31,196,104,4,144,193,42,45,146,195,17,65,121,197,135,46,248,195,174,94,19,69,92,94,39,69,233,35,237,196,169,201,17,196,221,150,147,67,124,114,215,68,64,148,154,67,170,238,117,68,254,77,56,197,176,119,34,197,195,238,165,195,222,9,247,68,76,202,204,67,80,86,26,193,239,231,108,69,134,194,27,196,86,218,67,197,10,13,124,68,26,90,181,195,2,108,34,68,87,9,70,68,120,42,118,69,83,150,14,68,149,114,13,197,117,185,214,196,147,243,70,196,76,255,145,196,153,126,222,68,250,121,134,195,144,8,171,68,15,66,63,196,200,14,156,68,162,59,51,68,48,128,238,195,159,16,125,196,190,0,132,67,192,38,141,196,192,72,185,66,238,238,157,67,206,177,145,67,57,124,53,195,130,232,174,196,208,160,57,68,198,48,23,195,16,66,67,69,20,80,225,65,79,223,167,196,27,77,195,196,37,9,89,68,68,248,147,194,85,211,62,67,220,171,183,67,160,60,206,196,227,124,21,68,54,23,24,196,169,249,3,69,164,101,103,68,36,37,148,196,219,248,82,67,66,207,25,68,249,117,45,197,214,42,252,67,23,241,54,68,98,114,52,196,28,223,129,67,34,178,72,68,183,85,177,196,200,135,60,196,74,39,155,67,237,86,38,68,2,62,192,195,168,105,132,68,59,153,79,68,120,189,29,67,128,61,164,196,108,175,103,196,214,251,142,196,90,221,125,68,19,243,182,194,128,118,84,194,111,27,198,68,34,134,146,195,34,59,194,194,194,133,109,196,60,29,248,68,54,171,253,195,90,94,244,195,35,83,58,196,120,180,136,196,55,83,154,68,196,204,30,70,116,13,88,197,26,254,159,67,140,174,146,195,13,198,220,196,18,146,60,197,184,213,71,68,54,207,108,194,203,119,152,68,7,57,217,68,19,82,112,197,100,154,93,69,28,213,79,196,212,231,193,68,205,159,226,196,80,233,61,69,150,121,189,196,64,92,88,196,168,25,209,196,72,245,73,195,94,213,36,196,166,31,164,196,219,39,133,196,41,111,67,69,244,33,1,196,154,54,187,196,149,96,102,196,193,252,174,67,219,33,34,197,59,25,5,69,186,148,131,68,183,74,206,67,24,139,20,195,221,220,104,68,135,103,55,196,96,229,254,67,109,69,40,197,206,143,71,68,8,213,249,195,90,35,20,68,119,113,139,68,92,124,235,195,244,166,116,67,181,197,132,66,235,127,209,196,98,84,210,67,198,165,74,69,113,219,80,68,254,120,189,196,143,201,150,195,218,103,56,196,3,18,205,196,157,144,165,195,220,37,134,196,185,73,203,68,64,88,94,64,229,104,134,69,100,234,26,197,7,230,95,69,67,60,114,197,40,38,155,195,190,13,134,196,180,54,194,67,43,244,195,196,5,74,77,69,153,235,19,197,134,223,138,67,120,209,201,68,245,168,235,196,0,0,0,0,0,0,0,0,0,0,0]
print(len(buffer))
import numpy as np
from PIL import Image
import math
def bytes_to_floats(buffer, float_size):
if float_size == 4:
fmt = 'f' # 32-bit float
elif float_size == 8:
fmt = 'd' # 64-bit float
else:
raise ValueError("Unsupported float size")
num_floats = len(buffer) // float_size
floats = struct.unpack(f'{num_floats}{fmt}', buffer)
return floats
def bytes_to_bits(data):
bits = []
for byte in data:
for i in range(8):
bits.append((byte >> (7 - i)) & 1)
return bits
def find_factor_pairs(n):
factors = []
for i in range(1, int(n) + 1):
if n % i == 0:
factors.append((i, n // i))
return factors
def create_grayscale_image(data, width, height):
image_data = np.array(list(data[:width * height]), dtype=np.uint8).reshape((height, width))
image = Image.fromarray(image_data, mode='L')
return image
def create_rgb_image(data, width, height):
image_data = np.array(list(data[:width * height * 3]), dtype=np.uint8).reshape((height, width, 3))
image = Image.fromarray(image_data, mode='RGB')
return image
def create_rgba_image(data, width, height):
image_data = np.array(list(data[:width * height * 4]), dtype=np.uint8).reshape((height, width, 4))
image = Image.fromarray(image_data, mode='RGBA')
return image
def create_bw_image(bits, width, height):
image_data = np.array(bits[:width * height], dtype=np.uint8).reshape((height, width)) * 255
image = Image.fromarray(image_data, mode='L')
return image
def normalize_floats(floats):
min_val = min(floats)
max_val = max(floats)
normalized = [(val - min_val) / (max_val - min_val) * 255 for val in floats]
return np.array(normalized, dtype=np.uint8)
def create_grayscale_image(normalized_data, width, height):
image_data = normalized_data.reshape((height, width))
image = Image.fromarray(image_data, mode='L')
return image
data = bytes(buffer)
float_size = 4 # or 8 for 64-bit floats
# Convert the byte buffer to floats
#floats = bytes_to_floats(data, float_size)
# Normalize the float values
#normalized_data = normalize_floats(floats)
# Find factor pairs of the total number of floats
#num_floats = len(floats)
#factor_pairs = find_factor_pairs(num_floats)
# Convert the list to a byte buffer
# Number of bytes
num_bytes = len(data)
# Convert bytes to bits
bits = bytes_to_bits(data)
# Number of bits
num_bits = len(bits)
#for width, height in factor_pairs:
# image = create_grayscale_image(normalized_data, width, height)
# image.show() # Display the image
bw_factor_pairs = find_factor_pairs(num_bits)
for width, height in bw_factor_pairs:
image = create_bw_image(bits, width, height)
image.show()
image.save(f'bw_image_{width}x{height}.png')
# Create grayscale images
grayscale_factor_pairs = find_factor_pairs(num_bytes)
for width, height in grayscale_factor_pairs:
image = create_grayscale_image(data, width, height)
image.show()
image.save(f'grayscale_image_{width}x{height}.png')
# Create RGB images if the number of bytes is a multiple of 3
if num_bytes % 3 == 0:
rgb_factor_pairs = find_factor_pairs(num_bytes // 3)
for width, height in rgb_factor_pairs:
image = create_rgb_image(data, width, height)
image.show()
image.save(f'rgb_image_{width}x{height}.png')
# Create RGBA images if the number of bytes is a multiple of 4
if num_bytes % 4 == 0:
rgba_factor_pairs = find_factor_pairs(num_bytes // 4)
for width, height in rgba_factor_pairs:
image = create_rgba_image(data, width, height)
image.show()
image.save(f'rgba_image_{width}x{height}.png')