Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,6 +28,8 @@ import pandas as pd
|
|
28 |
import numpy as np
|
29 |
import io
|
30 |
import os
|
|
|
|
|
31 |
|
32 |
def zoom_at(img, x, y, zoom):
|
33 |
'''
|
@@ -95,6 +97,51 @@ def apply_enhancements(img, x, y, zoom, contrast, brightness, sharpness):
|
|
95 |
enhanced_sharpness = ImageEnhance.Sharpness(enhanced_brightness).enhance(sharpness)
|
96 |
return enhanced_sharpness
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
st.set_page_config(page_title="CLL Explorer", layout="wide")
|
99 |
st.title("CLL Explorer: Cell Image Analysis Prep Tool")
|
100 |
|
@@ -151,34 +198,55 @@ if uploaded_files:
|
|
151 |
st.image(img, use_column_width=True, caption="Original Image")
|
152 |
|
153 |
# Save Options
|
154 |
-
|
155 |
-
|
156 |
-
st.session_state.processed_img.save("image-processed.jpg")
|
157 |
-
st.success("Image saved as `image-processed.jpg`")
|
158 |
|
159 |
-
|
|
|
160 |
description = st.text_area("Describe the image", "")
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
file_ext = str(np.random.randint(100))
|
181 |
os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg")
|
|
|
|
|
182 |
os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json")
|
|
|
|
|
183 |
os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt")
|
184 |
-
st.success("Files renamed successfully")
|
|
|
|
|
|
28 |
import numpy as np
|
29 |
import io
|
30 |
import os
|
31 |
+
import tempfile
|
32 |
+
import zipfile
|
33 |
|
34 |
def zoom_at(img, x, y, zoom):
|
35 |
'''
|
|
|
97 |
enhanced_sharpness = ImageEnhance.Sharpness(enhanced_brightness).enhance(sharpness)
|
98 |
return enhanced_sharpness
|
99 |
|
100 |
+
def create_zip(processed_img, description, params):
|
101 |
+
'''
|
102 |
+
Create a zip archive containing the processed image and annotations.
|
103 |
+
|
104 |
+
Parameters:
|
105 |
+
----------
|
106 |
+
processed_img : PIL.Image
|
107 |
+
The processed image.
|
108 |
+
description : str
|
109 |
+
Description of the image.
|
110 |
+
params : dict
|
111 |
+
Image parameters.
|
112 |
+
|
113 |
+
Returns:
|
114 |
+
-------
|
115 |
+
bytes
|
116 |
+
Byte content of the zip file.
|
117 |
+
'''
|
118 |
+
# Create a temporary directory
|
119 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
120 |
+
# Define file paths
|
121 |
+
img_path = os.path.join(tmpdirname, "processed_image.jpg")
|
122 |
+
desc_path = os.path.join(tmpdirname, "description.txt")
|
123 |
+
params_path = os.path.join(tmpdirname, "parameters.json")
|
124 |
+
|
125 |
+
# Save processed image
|
126 |
+
processed_img.save(img_path)
|
127 |
+
|
128 |
+
# Save description
|
129 |
+
with open(desc_path, "w") as f:
|
130 |
+
f.write(description)
|
131 |
+
|
132 |
+
# Save parameters
|
133 |
+
pd.DataFrame([params]).to_json(params_path, orient="records")
|
134 |
+
|
135 |
+
# Create a zip file in memory
|
136 |
+
zip_buffer = io.BytesIO()
|
137 |
+
with zipfile.ZipFile(zip_buffer, "w") as zipf:
|
138 |
+
zipf.write(img_path, arcname="processed_image.jpg")
|
139 |
+
zipf.write(desc_path, arcname="description.txt")
|
140 |
+
zipf.write(params_path, arcname="parameters.json")
|
141 |
+
|
142 |
+
zip_buffer.seek(0)
|
143 |
+
return zip_buffer
|
144 |
+
|
145 |
st.set_page_config(page_title="CLL Explorer", layout="wide")
|
146 |
st.title("CLL Explorer: Cell Image Analysis Prep Tool")
|
147 |
|
|
|
198 |
st.image(img, use_column_width=True, caption="Original Image")
|
199 |
|
200 |
# Save Options
|
201 |
+
st.markdown("---")
|
202 |
+
st.subheader("Save and Export Options")
|
|
|
|
|
203 |
|
204 |
+
# Description and Parameters Inputs
|
205 |
+
with st.expander("Add Annotations", expanded=True):
|
206 |
description = st.text_area("Describe the image", "")
|
207 |
+
params = {
|
208 |
+
"coordinates_x": x,
|
209 |
+
"coordinates_y": y,
|
210 |
+
"zoom": zoom,
|
211 |
+
"contrast": contrast,
|
212 |
+
"brightness": brightness,
|
213 |
+
"sharpness": sharpness
|
214 |
+
}
|
215 |
+
|
216 |
+
# Create a button to prepare the zip file
|
217 |
+
if st.button("Prepare Download"):
|
218 |
+
if 'processed_img' in st.session_state and description:
|
219 |
+
zip_buffer = create_zip(st.session_state.processed_img, description, params)
|
220 |
+
st.download_button(
|
221 |
+
label="Download Zip",
|
222 |
+
data=zip_buffer,
|
223 |
+
file_name="processed_image_and_annotations.zip",
|
224 |
+
mime="application/zip"
|
225 |
+
)
|
226 |
+
st.success("Zip file is ready for download.")
|
227 |
+
else:
|
228 |
+
st.warning("Ensure that the processed image is available and description is provided.")
|
229 |
|
230 |
+
# Optional: Separate Save Image Checkbox
|
231 |
+
save_image = st.checkbox("Save Processed Image Locally")
|
232 |
+
if save_image:
|
233 |
+
if 'processed_img' in st.session_state:
|
234 |
+
st.session_state.processed_img.save("image-processed.jpg")
|
235 |
+
st.success("Image saved as `image-processed.jpg`")
|
236 |
+
else:
|
237 |
+
st.warning("No processed image to save.")
|
238 |
+
|
239 |
+
# Optional: Rename Files
|
240 |
+
if st.button("Rename Files"):
|
241 |
+
if 'processed_img' in st.session_state:
|
242 |
file_ext = str(np.random.randint(100))
|
243 |
os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg")
|
244 |
+
with open("saved_image_parameters.json", "w") as f:
|
245 |
+
f.write(pd.DataFrame([params]).to_json(orient="records"))
|
246 |
os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json")
|
247 |
+
with open("saved_image_description.txt", "w") as f:
|
248 |
+
f.write(description)
|
249 |
os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt")
|
250 |
+
st.success("Files renamed successfully")
|
251 |
+
else:
|
252 |
+
st.warning("No processed image to rename.")
|