wenjun99 commited on
Commit
07a5d20
·
verified ·
1 Parent(s): d3f0a8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -1,15 +1,11 @@
1
  import streamlit as st
2
- from PIL import Image
3
  import numpy as np
4
  import pandas as pd
5
 
6
  # Simple app: convert user input into ASCII codes and binary labels
7
 
8
  def string_to_binary_labels(s: str) -> list[int]:
9
- """
10
- Convert a string into a flat list of binary labels (0 or 1) representing
11
- each character's 8-bit ASCII code.
12
- """
13
  bits: list[int] = []
14
  for char in s:
15
  ascii_code = ord(char)
@@ -17,27 +13,30 @@ def string_to_binary_labels(s: str) -> list[int]:
17
  bits.extend(char_bits)
18
  return bits
19
 
20
- def image_to_binary_labels_rgb(img: Image.Image, max_pixels: int = 256) -> list[int]:
21
  """
22
- Convert an RGB image to binary labels (0/1).
23
- Store full RGB values (24 bits per pixel).
24
  """
25
  img = img.convert("RGB")
 
 
 
 
 
 
 
26
  img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels))))
27
  img_array = np.array(img)
28
  flat_pixels = img_array.reshape(-1, 3)
29
 
30
  bits = []
31
  for pixel in flat_pixels:
32
- for channel in pixel: # R, G, B
33
  channel_bits = [(channel >> bit) & 1 for bit in range(7, -1, -1)]
34
  bits.extend(channel_bits)
35
  return bits
36
 
37
  def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
38
- """
39
- Convert binary labels (0/1) into an RGB image.
40
- """
41
  total_pixels = len(binary_labels) // 24
42
  if width is None or height is None:
43
  side = int(np.ceil(np.sqrt(total_pixels)))
@@ -106,7 +105,7 @@ with tab1:
106
 
107
  df = pd.DataFrame(table_data, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
108
  st.dataframe(df)
109
-
110
  st.download_button(
111
  label="Download Binary Labels Table as CSV",
112
  data=df.to_csv(index=False),
@@ -114,7 +113,6 @@ with tab1:
114
  mime="text/csv"
115
  )
116
 
117
-
118
  with tab2:
119
  st.write("Upload an image (JPG or PNG) to convert it into binary labels:")
120
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
@@ -123,7 +121,7 @@ with tab2:
123
  img = Image.open(uploaded_file)
124
  st.image(img, caption="Uploaded Image", use_column_width=True)
125
 
126
- max_pixels = st.slider("Max number of pixels to encode", min_value=4096, max_value=16384, value=8192, step=32)
127
 
128
  binary_labels = image_to_binary_labels_rgb(img, max_pixels=max_pixels)
129
 
@@ -148,9 +146,9 @@ with tab2:
148
  st.image(reconstructed_img, caption="Reconstructed Image", use_column_width=True)
149
 
150
  st.download_button(
151
- label="Download Image Binary Labels as CSV",
152
- data=','.join(str(b) for b in binary_labels),
153
- file_name="image_binary_labels.csv",
154
  mime="text/csv"
155
  )
156
 
 
1
  import streamlit as st
2
+ from PIL import Image, ImageFilter
3
  import numpy as np
4
  import pandas as pd
5
 
6
  # Simple app: convert user input into ASCII codes and binary labels
7
 
8
  def string_to_binary_labels(s: str) -> list[int]:
 
 
 
 
9
  bits: list[int] = []
10
  for char in s:
11
  ascii_code = ord(char)
 
13
  bits.extend(char_bits)
14
  return bits
15
 
16
+ def clean_image(img: Image.Image, min_size: int = 256) -> Image.Image:
17
  """
18
+ Preprocess image to reduce noise: resize if needed and apply light blur.
 
19
  """
20
  img = img.convert("RGB")
21
+ if img.width < min_size or img.height < min_size:
22
+ img = img.resize((min_size, min_size))
23
+ img = img.filter(ImageFilter.GaussianBlur(radius=1))
24
+ return img
25
+
26
+ def image_to_binary_labels_rgb(img: Image.Image, max_pixels: int = 256) -> list[int]:
27
+ img = clean_image(img)
28
  img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels))))
29
  img_array = np.array(img)
30
  flat_pixels = img_array.reshape(-1, 3)
31
 
32
  bits = []
33
  for pixel in flat_pixels:
34
+ for channel in pixel:
35
  channel_bits = [(channel >> bit) & 1 for bit in range(7, -1, -1)]
36
  bits.extend(channel_bits)
37
  return bits
38
 
39
  def binary_labels_to_rgb_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
 
 
 
40
  total_pixels = len(binary_labels) // 24
41
  if width is None or height is None:
42
  side = int(np.ceil(np.sqrt(total_pixels)))
 
105
 
106
  df = pd.DataFrame(table_data, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
107
  st.dataframe(df)
108
+
109
  st.download_button(
110
  label="Download Binary Labels Table as CSV",
111
  data=df.to_csv(index=False),
 
113
  mime="text/csv"
114
  )
115
 
 
116
  with tab2:
117
  st.write("Upload an image (JPG or PNG) to convert it into binary labels:")
118
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
 
121
  img = Image.open(uploaded_file)
122
  st.image(img, caption="Uploaded Image", use_column_width=True)
123
 
124
+ max_pixels = st.slider("Max number of pixels to encode", min_value=32, max_value=1024, value=256, step=32)
125
 
126
  binary_labels = image_to_binary_labels_rgb(img, max_pixels=max_pixels)
127
 
 
146
  st.image(reconstructed_img, caption="Reconstructed Image", use_column_width=True)
147
 
148
  st.download_button(
149
+ label="Download Image Binary Labels Table as CSV",
150
+ data=df.to_csv(index=False),
151
+ file_name="image_binary_labels_table.csv",
152
  mime="text/csv"
153
  )
154