Skier8402 commited on
Commit
0e955d7
·
verified ·
1 Parent(s): 386797d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -24
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
- save_image = st.checkbox("Save Processed Image")
155
- if save_image:
156
- st.session_state.processed_img.save("image-processed.jpg")
157
- st.success("Image saved as `image-processed.jpg`")
158
 
159
- with st.expander("Save Options"):
 
160
  description = st.text_area("Describe the image", "")
161
- if st.button("Save Description"):
162
- with open("saved_image_description.txt", "w") as f:
163
- f.write(description)
164
- st.success("Description saved as `saved_image_description.txt`")
165
-
166
- if st.button("Save Image Parameters"):
167
- params = {
168
- "coordinates_x": x,
169
- "coordinates_y": y,
170
- "zoom": zoom,
171
- "contrast": contrast,
172
- "brightness": brightness,
173
- "sharpness": sharpness
174
- }
175
- with open("saved_image_parameters.json", "w") as f:
176
- f.write(pd.DataFrame([params]).to_json(orient="records"))
177
- st.success("Image parameters saved as `saved_image_parameters.json`")
 
 
 
 
 
178
 
179
- if st.button("Rename Files"):
 
 
 
 
 
 
 
 
 
 
 
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.")