Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,36 +28,6 @@ streamlit run cell_exp_past.py
|
|
| 28 |
associated files.
|
| 29 |
'''
|
| 30 |
|
| 31 |
-
'''
|
| 32 |
-
an image processing tool that allows users to upload microscope images,
|
| 33 |
-
adjust the view with zoom and enhancement controls, and save the processed
|
| 34 |
-
image along with annotations. The tool uses OpenCV for image processing and
|
| 35 |
-
PIL for image enhancements. The processed image can be saved locally or
|
| 36 |
-
exported as a zip file containing the processed image, description, and
|
| 37 |
-
parameters. The tool also provides options to rename the processed image and
|
| 38 |
-
associated files.
|
| 39 |
-
|
| 40 |
-
The tool consists of the following components:
|
| 41 |
-
1. File Uploader: Allows users to upload microscope images in JPG or PNG format.
|
| 42 |
-
2. Image Controls: Provides sliders to adjust the zoom, contrast, brightness, and sharpness of the image.
|
| 43 |
-
3. Processed Image Display: Displays the processed image after applying the adjustments.
|
| 44 |
-
4. Original Image Display: Displays the original image uploaded by the user.
|
| 45 |
-
5. Save and Export Options: Allows users to add annotations, prepare a zip file for download, save the processed image locally, and rename the processed image and associated files.
|
| 46 |
-
|
| 47 |
-
To run the tool:
|
| 48 |
-
1. Save the script as `cell_exp_past.py`.
|
| 49 |
-
2. Run the script in a Python environment.
|
| 50 |
-
```python
|
| 51 |
-
streamlit run cell_exp_past.py
|
| 52 |
-
```
|
| 53 |
-
3. Open the provided local URL in a web browser.
|
| 54 |
-
4. Upload microscope images and adjust the image view.
|
| 55 |
-
5. Apply adjustments and save the processed image with annotations.
|
| 56 |
-
6. Download the processed image and annotations as a zip file.
|
| 57 |
-
7. Save the processed image locally or rename the processed image and
|
| 58 |
-
associated files.
|
| 59 |
-
'''
|
| 60 |
-
|
| 61 |
import streamlit as st
|
| 62 |
from PIL import Image, ImageEnhance
|
| 63 |
import pandas as pd
|
|
@@ -97,8 +67,10 @@ if uploaded_files:
|
|
| 97 |
img_sharp = ImageEnhance.Sharpness(img_bright).enhance(sharpness)
|
| 98 |
|
| 99 |
if save_image:
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
st.image(img_sharp, caption="Processed Image", use_container_width=True)
|
| 104 |
|
|
@@ -107,6 +79,7 @@ if uploaded_files:
|
|
| 107 |
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
|
| 108 |
f.write(description)
|
| 109 |
desc_file = f.name
|
|
|
|
| 110 |
st.success("Description saved.")
|
| 111 |
|
| 112 |
if st.button("Save Image Parameters"):
|
|
@@ -121,16 +94,36 @@ if uploaded_files:
|
|
| 121 |
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.json') as f:
|
| 122 |
json.dump(params, f)
|
| 123 |
params_file = f.name
|
|
|
|
| 124 |
st.success("Parameters saved.")
|
| 125 |
|
| 126 |
if st.button("Rename Files"):
|
| 127 |
file_ext = str(np.random.randint(100))
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
os.
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
st.success("Files renamed successfully")
|
| 135 |
|
| 136 |
if st.button("Export to ZIP"):
|
|
@@ -142,4 +135,3 @@ if uploaded_files:
|
|
| 142 |
z.write(params_file, os.path.basename(params_file))
|
| 143 |
with open(zipf.name, 'rb') as f:
|
| 144 |
st.download_button("Download ZIP", f, "annotations.zip")
|
| 145 |
-
|
|
|
|
| 28 |
associated files.
|
| 29 |
'''
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
import streamlit as st
|
| 32 |
from PIL import Image, ImageEnhance
|
| 33 |
import pandas as pd
|
|
|
|
| 67 |
img_sharp = ImageEnhance.Sharpness(img_bright).enhance(sharpness)
|
| 68 |
|
| 69 |
if save_image:
|
| 70 |
+
processed_image_path = "image-processed.jpg"
|
| 71 |
+
img_sharp.save(processed_image_path)
|
| 72 |
+
st.session_state['processed_image_path'] = processed_image_path
|
| 73 |
+
st.success(f"Image saved as {processed_image_path}")
|
| 74 |
|
| 75 |
st.image(img_sharp, caption="Processed Image", use_container_width=True)
|
| 76 |
|
|
|
|
| 79 |
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
|
| 80 |
f.write(description)
|
| 81 |
desc_file = f.name
|
| 82 |
+
st.session_state['desc_file'] = desc_file
|
| 83 |
st.success("Description saved.")
|
| 84 |
|
| 85 |
if st.button("Save Image Parameters"):
|
|
|
|
| 94 |
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.json') as f:
|
| 95 |
json.dump(params, f)
|
| 96 |
params_file = f.name
|
| 97 |
+
st.session_state['params_file'] = params_file
|
| 98 |
st.success("Parameters saved.")
|
| 99 |
|
| 100 |
if st.button("Rename Files"):
|
| 101 |
file_ext = str(np.random.randint(100))
|
| 102 |
+
processed_image_path = st.session_state.get('processed_image_path', None)
|
| 103 |
+
desc_file = st.session_state.get('desc_file', None)
|
| 104 |
+
params_file = st.session_state.get('params_file', None)
|
| 105 |
+
|
| 106 |
+
if processed_image_path and os.path.exists(processed_image_path):
|
| 107 |
+
try:
|
| 108 |
+
new_image_name = f"img_processed{file_ext}.jpg"
|
| 109 |
+
os.rename(processed_image_path, new_image_name)
|
| 110 |
+
except FileNotFoundError:
|
| 111 |
+
st.error(f"{processed_image_path} not found.")
|
| 112 |
+
else:
|
| 113 |
+
st.error("Processed image not found.")
|
| 114 |
+
|
| 115 |
+
if params_file and os.path.exists(params_file):
|
| 116 |
+
new_params_name = f"saved_image_parameters{file_ext}.json"
|
| 117 |
+
os.rename(params_file, new_params_name)
|
| 118 |
+
else:
|
| 119 |
+
st.error("Saved image parameters file not found.")
|
| 120 |
+
|
| 121 |
+
if desc_file and os.path.exists(desc_file):
|
| 122 |
+
new_desc_name = f"saved_image_description{file_ext}.txt"
|
| 123 |
+
os.rename(desc_file, new_desc_name)
|
| 124 |
+
else:
|
| 125 |
+
st.error("Saved image description file not found.")
|
| 126 |
+
|
| 127 |
st.success("Files renamed successfully")
|
| 128 |
|
| 129 |
if st.button("Export to ZIP"):
|
|
|
|
| 135 |
z.write(params_file, os.path.basename(params_file))
|
| 136 |
with open(zipf.name, 'rb') as f:
|
| 137 |
st.download_button("Download ZIP", f, "annotations.zip")
|
|
|