Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,39 +2,27 @@ 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)
|
16 |
-
# Extract 8-bit binary representation (MSB first)
|
17 |
char_bits = [(ascii_code >> bit) & 1 for bit in range(7, -1, -1)]
|
18 |
bits.extend(char_bits)
|
19 |
return bits
|
20 |
|
21 |
def image_to_binary_labels(img: Image.Image, max_pixels: int = 256) -> list[int]:
|
22 |
-
""
|
23 |
-
|
24 |
-
Convert to grayscale, resize to limit pixel count, and binarize (threshold 128).
|
25 |
-
"""
|
26 |
-
img = img.convert("L") # Convert to grayscale
|
27 |
-
img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels)))) # Resize while maintaining aspect ratio
|
28 |
img_array = np.array(img)
|
29 |
flat = img_array.flatten()
|
30 |
-
binarized = (flat > 128).astype(int)
|
31 |
return binarized.tolist()
|
32 |
|
33 |
def binary_labels_to_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
|
34 |
-
"""
|
35 |
-
Convert binary labels (0/1) back into an image.
|
36 |
-
If width and height are not provided, assumes a square image.
|
37 |
-
"""
|
38 |
total_pixels = len(binary_labels)
|
39 |
if width is None or height is None:
|
40 |
side = int(np.ceil(np.sqrt(total_pixels)))
|
@@ -47,7 +35,13 @@ def binary_labels_to_image(binary_labels: list[int], width: int = None, height:
|
|
47 |
img = Image.fromarray(image_array, mode='L')
|
48 |
return img
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
mutation_site_headers = [
|
52 |
3244, 3297, 3350, 3399, 3455, 3509, 3562, 3614,
|
53 |
3665, 3720, 3773, 3824, 3879, 3933, 3985, 4039,
|
@@ -57,7 +51,6 @@ mutation_site_headers = [
|
|
57 |
|
58 |
st.title("ASCII & Binary Label Converter")
|
59 |
|
60 |
-
# Create tabs
|
61 |
tab1, tab2 = st.tabs(["Text to Binary Labels", "Image to Binary Labels"])
|
62 |
|
63 |
with tab1:
|
@@ -107,7 +100,7 @@ with tab2:
|
|
107 |
img = Image.open(uploaded_file)
|
108 |
st.image(img, caption="Uploaded Image", use_column_width=True)
|
109 |
|
110 |
-
max_pixels = st.slider("Max number of pixels to encode", min_value=32, max_value=
|
111 |
|
112 |
binary_labels = image_to_binary_labels(img, max_pixels=max_pixels)
|
113 |
|
@@ -137,6 +130,10 @@ with tab2:
|
|
137 |
st.subheader("Reconstruct Image from Binary Labels")
|
138 |
if st.button("Reconstruct Image"):
|
139 |
reconstructed_img = binary_labels_to_image(binary_labels)
|
140 |
-
st.image(reconstructed_img, caption="Reconstructed Image", use_column_width=True)
|
|
|
|
|
|
|
|
|
141 |
|
142 |
# Future: integrate DNA editor mapping for each mutation site here
|
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
+
from matplotlib import cm
|
6 |
|
7 |
# Simple app: convert user input into ASCII codes and binary labels
|
8 |
|
9 |
def string_to_binary_labels(s: str) -> list[int]:
|
|
|
|
|
|
|
|
|
10 |
bits: list[int] = []
|
11 |
for char in s:
|
12 |
ascii_code = ord(char)
|
|
|
13 |
char_bits = [(ascii_code >> bit) & 1 for bit in range(7, -1, -1)]
|
14 |
bits.extend(char_bits)
|
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)
|
27 |
if width is None or height is None:
|
28 |
side = int(np.ceil(np.sqrt(total_pixels)))
|
|
|
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,
|
47 |
3665, 3720, 3773, 3824, 3879, 3933, 3985, 4039,
|
|
|
51 |
|
52 |
st.title("ASCII & Binary Label Converter")
|
53 |
|
|
|
54 |
tab1, tab2 = st.tabs(["Text to Binary Labels", "Image to Binary Labels"])
|
55 |
|
56 |
with tab1:
|
|
|
100 |
img = Image.open(uploaded_file)
|
101 |
st.image(img, caption="Uploaded Image", use_column_width=True)
|
102 |
|
103 |
+
max_pixels = st.slider("Max number of pixels to encode", min_value=32, max_value=2056, value=1024, step=32)
|
104 |
|
105 |
binary_labels = image_to_binary_labels(img, max_pixels=max_pixels)
|
106 |
|
|
|
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
|