wenjun99 commited on
Commit
2d22901
·
verified ·
1 Parent(s): 181eb38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -15,12 +15,12 @@ def string_to_binary_labels(s: str) -> list[int]:
15
  return bits
16
 
17
  def image_to_binary_labels(img: Image.Image, max_pixels: int = 256) -> list[int]:
18
- img = img.convert("L")
19
  img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels))))
20
  img_array = np.array(img)
21
  flat = img_array.flatten()
22
- binarized = (flat > 128).astype(int)
23
- return binarized.tolist()
24
 
25
  def binary_labels_to_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
26
  total_pixels = len(binary_labels)
@@ -35,12 +35,27 @@ def binary_labels_to_image(binary_labels: list[int], width: int = None, height:
35
  img = Image.fromarray(image_array, mode='L')
36
  return img
37
 
38
- def colorize_grayscale(img: Image.Image) -> Image.Image:
39
- img_array = np.array(img) / 255.0
40
- colored_array = cm.viridis(img_array)[:, :, :3]
41
- colored_array = (colored_array * 255).astype(np.uint8)
42
- color_img = Image.fromarray(colored_array, mode='RGB')
43
- return color_img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  mutation_site_headers = [
46
  3244, 3297, 3350, 3399, 3455, 3509, 3562, 3614,
@@ -128,12 +143,13 @@ with tab2:
128
  )
129
 
130
  st.subheader("Reconstruct Image from Binary Labels")
131
- if st.button("Reconstruct Image"):
132
- reconstructed_img = binary_labels_to_image(binary_labels)
133
- st.image(reconstructed_img, caption="Reconstructed Grayscale Image", use_column_width=True)
134
 
135
- st.subheader("Colorized Image (Fake Color)")
136
- colorized_img = colorize_grayscale(reconstructed_img)
137
- st.image(colorized_img, caption="Colorized Reconstructed Image", use_column_width=True)
 
 
 
138
 
139
- # Future: integrate DNA editor mapping for each mutation site here
 
15
  return bits
16
 
17
  def image_to_binary_labels(img: Image.Image, max_pixels: int = 256) -> list[int]:
18
+ img = img.convert("RGB")
19
  img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels))))
20
  img_array = np.array(img)
21
  flat = img_array.flatten()
22
+ binarized = [(pixel >> bit) & 1 for pixel in flat for bit in range(7, -1, -1)]
23
+ return binarized
24
 
25
  def binary_labels_to_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
26
  total_pixels = len(binary_labels)
 
35
  img = Image.fromarray(image_array, mode='L')
36
  return img
37
 
38
+ def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
39
+ total_pixels = len(binary_labels) // 24
40
+ if width is None or height is None:
41
+ side = int(np.ceil(np.sqrt(total_pixels)))
42
+ width = height = side
43
+ needed_pixels = width * height
44
+ needed_bits = needed_pixels * 24
45
+ if len(binary_labels) < needed_bits:
46
+ binary_labels += [0] * (needed_bits - len(binary_labels))
47
+ pixels = []
48
+ for i in range(0, needed_bits, 24):
49
+ r_bits = binary_labels[i:i+8]
50
+ g_bits = binary_labels[i+8:i+16]
51
+ b_bits = binary_labels[i+16:i+24]
52
+ r = sum(b << (7-j) for j, b in enumerate(r_bits))
53
+ g = sum(b << (7-j) for j, b in enumerate(g_bits))
54
+ b = sum(b << (7-j) for j, b in enumerate(b_bits))
55
+ pixels.append((r, g, b))
56
+ array = np.array(pixels, dtype=np.uint8).reshape((height, width, 3))
57
+ img = Image.fromarray(array, mode='RGB')
58
+ return img
59
 
60
  mutation_site_headers = [
61
  3244, 3297, 3350, 3399, 3455, 3509, 3562, 3614,
 
143
  )
144
 
145
  st.subheader("Reconstruct Image from Binary Labels")
146
+ option = st.radio("Choose Reconstruction Mode", ["Grayscale", "True Color (RGB)"])
 
 
147
 
148
+ if st.button("Reconstruct Image"):
149
+ if option == "Grayscale":
150
+ reconstructed_img = binary_labels_to_image(binary_labels)
151
+ else:
152
+ reconstructed_img = binary_labels_to_rgb_image(binary_labels)
153
+ st.image(reconstructed_img, caption="Reconstructed Image", use_column_width=True)
154
 
155
+ # Future: integrate DNA editor mapping for each mutation site here