wenjun99 commited on
Commit
bcb215e
·
verified ·
1 Parent(s): a33de8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -44
app.py CHANGED
@@ -68,50 +68,6 @@ def binary_labels_to_string(bits: list[int]) -> str:
68
  chars.append(voyager_table.get(val, '?'))
69
  return ''.join(chars)
70
 
71
- def clean_image(img: Image.Image, min_size: int = 256) -> Image.Image:
72
- img = img.convert("RGB")
73
- if img.width < min_size or img.height < min_size:
74
- img = img.resize((min_size, min_size))
75
- img = img.filter(ImageFilter.GaussianBlur(radius=1))
76
- return img
77
-
78
- def image_to_binary_labels_rgb(img: Image.Image, max_pixels: int = 256) -> list[int]:
79
- img = clean_image(img)
80
- img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels))))
81
- img_array = np.array(img)
82
- flat_pixels = img_array.reshape(-1, 3)
83
-
84
- bits = []
85
- for pixel in flat_pixels:
86
- for channel in pixel:
87
- channel_bits = [(channel >> bit) & 1 for bit in range(7, -1, -1)]
88
- bits.extend(channel_bits)
89
- return bits
90
-
91
- def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
92
- total_pixels = len(binary_labels) // 24
93
- if width is None or height is None:
94
- side = int(np.ceil(np.sqrt(total_pixels)))
95
- width = height = side
96
-
97
- needed_pixels = width * height
98
- needed_bits = needed_pixels * 24
99
- if len(binary_labels) < needed_bits:
100
- binary_labels += [0] * (needed_bits - len(binary_labels))
101
-
102
- pixels = []
103
- for i in range(0, needed_bits, 24):
104
- r_bits = binary_labels[i:i+8]
105
- g_bits = binary_labels[i+8:i+16]
106
- b_bits = binary_labels[i+16:i+24]
107
- r = sum(b << (7-j) for j, b in enumerate(r_bits))
108
- g = sum(b << (7-j) for j, b in enumerate(g_bits))
109
- b = sum(b << (7-j) for j, b in enumerate(b_bits))
110
- pixels.append((r, g, b))
111
-
112
- array = np.array(pixels, dtype=np.uint8).reshape((height, width, 3))
113
- img = Image.fromarray(array, mode='RGB')
114
- return img
115
 
116
  # === Streamlit App ===
117
 
 
68
  chars.append(voyager_table.get(val, '?'))
69
  return ''.join(chars)
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  # === Streamlit App ===
73