Skier8402 commited on
Commit
07dd5b0
·
verified ·
1 Parent(s): a71afbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -75
app.py CHANGED
@@ -19,7 +19,7 @@ To run the application:
19
  2. Run the script using the Streamlit command:
20
  ```bash
21
  streamlit run app.py
22
- ```
23
  '''
24
 
25
  import streamlit as st
@@ -30,18 +30,44 @@ import io
30
  import os
31
 
32
  def zoom_at(img, x, y, zoom):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  w, h = img.size
34
  zoom2 = zoom * 2
35
  img = img.crop((x - w / zoom2, y - h / zoom2,
36
  x + w / zoom2, y + h / zoom2))
37
  return img.resize((w, h), Image.LANCZOS)
38
 
39
- st.set_page_config(page_title="Cell Explorer", layout="wide")
40
- st.title("CLL Explorer - Annotate: Upload, Process, and Save Images")
41
 
42
  st.markdown("""
43
  ### About This Application
44
- This tool assists researchers in analyzing microscope images of blood cells for malaria detection:
45
  - **Upload** microscope images.
46
  - **Adjust** image view with zoom and enhancement controls.
47
  - **Detect** and measure cells automatically.
@@ -51,78 +77,68 @@ This tool assists researchers in analyzing microscope images of blood cells for
51
  uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, type=["jpg", "png"])
52
 
53
  if uploaded_files:
54
- img_index = st.selectbox("Select Image", range(len(uploaded_files)), format_func=lambda x: uploaded_files[x].name)
 
 
 
 
55
  img_data = uploaded_files[img_index].read()
56
  img = Image.open(io.BytesIO(img_data)).resize((500, 500))
57
 
58
- # Display the original image first
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  st.subheader("Original Image")
60
- st.image(img, caption="Original Image", use_column_width=True)
61
-
62
- # Arrange layout for processed image and controls
63
- processed_col, controls_col = st.columns([3, 2])
64
-
65
- with processed_col:
66
- st.subheader("Processed Image")
67
- img_zoomed = zoom_at(img, 250, 250, 5)
68
- img_contrast = ImageEnhance.Contrast(img_zoomed).enhance(1.0)
69
- img_bright = ImageEnhance.Brightness(img_contrast).enhance(1.0)
70
- img_sharp = ImageEnhance.Sharpness(img_bright).enhance(1.0)
71
- st.image(img_sharp, use_column_width=True)
72
-
73
- with controls_col:
74
- st.subheader("Image Controls")
75
- x = st.slider("X Coordinate", 0, 500, 250)
76
- y = st.slider("Y Coordinate", 0, 500, 250)
77
- zoom = st.slider("Zoom", 1, 10, 5)
78
-
79
- with st.expander("Enhancement Settings"):
80
- contrast = st.slider("Contrast", 0.0, 5.0, 1.0)
81
- brightness = st.slider("Brightness", 0.0, 5.0, 1.0)
82
- sharpness = st.slider("Sharpness", 0.0, 2.0, 1.0)
83
-
84
- if st.button("Apply Adjustments"):
85
- img_zoomed = zoom_at(img, x, y, zoom)
86
- img_contrast = ImageEnhance.Contrast(img_zoomed).enhance(contrast)
87
- img_bright = ImageEnhance.Brightness(img_contrast).enhance(brightness)
88
- img_sharp = ImageEnhance.Sharpness(img_bright).enhance(sharpness)
89
- st.image(img_sharp, use_column_width=True, caption="Processed Image")
90
-
91
- actions_col, save_col = st.columns([2, 3])
92
-
93
- with actions_col:
94
- save_image = st.checkbox("Save Image")
95
- if save_image:
96
- img_sharp.save("image-processed.jpg")
97
- st.success("Image saved as `image-processed.jpg`")
98
-
99
- with st.expander("Save Options"):
100
- description = st.text_area("Describe the image", "")
101
- if st.button("Save Description"):
102
- with open("saved_image_description.txt", "w") as f:
103
- f.write(description)
104
- st.success("Description saved as `saved_image_description.txt`")
105
-
106
- if st.button("Save Image Parameters"):
107
- params = {
108
- "coordinates_x": x,
109
- "coordinates_y": y,
110
- "zoom": zoom,
111
- "contrast": contrast,
112
- "brightness": brightness,
113
- "sharpness": sharpness
114
- }
115
- with open("saved_image_parameters.json", "w") as f:
116
- f.write(pd.DataFrame([params]).to_json(orient="records"))
117
- st.success("Image parameters saved as `saved_image_parameters.json`")
118
-
119
- if st.button("Rename Files"):
120
- file_ext = str(np.random.randint(100))
121
- os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg")
122
- os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json")
123
- os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt")
124
- st.success("Files renamed successfully")
125
-
126
- # Display Original Image Below Processed Image
127
- st.markdown("### Original Image")
128
- st.image(img, use_column_width=True)
 
19
  2. Run the script using the Streamlit command:
20
  ```bash
21
  streamlit run app.py
22
+
23
  '''
24
 
25
  import streamlit as st
 
30
  import os
31
 
32
  def zoom_at(img, x, y, zoom):
33
+ '''
34
+ Zoom into an image at a specific location.
35
+
36
+ Parameters:
37
+ ----------
38
+ img : PIL.Image
39
+ Input image.
40
+
41
+ x : int
42
+ X-coordinate of the zoom center.
43
+
44
+ y : int
45
+ Y-coordinate of the zoom center.
46
+
47
+ zoom : float
48
+ Zoom factor.
49
+
50
+ Returns:
51
+ -------
52
+ PIL.Image
53
+ Zoomed image.
54
+
55
+ Examples:
56
+ --------
57
+ >>> img = Image.open('image.jpg')
58
+ >>> zoomed_img = zoom_at(img, x=100, y=100, zoom=2.0)
59
+ '''
60
  w, h = img.size
61
  zoom2 = zoom * 2
62
  img = img.crop((x - w / zoom2, y - h / zoom2,
63
  x + w / zoom2, y + h / zoom2))
64
  return img.resize((w, h), Image.LANCZOS)
65
 
66
+ st.set_page_config(page_title="CLL Explorer", layout="wide")
67
+ st.title("CLL Explorer - Malaria Cell Detection")
68
 
69
  st.markdown("""
70
  ### About This Application
 
71
  - **Upload** microscope images.
72
  - **Adjust** image view with zoom and enhancement controls.
73
  - **Detect** and measure cells automatically.
 
77
  uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, type=["jpg", "png"])
78
 
79
  if uploaded_files:
80
+ img_index = st.selectbox(
81
+ "Select Image",
82
+ range(len(uploaded_files)),
83
+ format_func=lambda x: uploaded_files[x].name
84
+ )
85
  img_data = uploaded_files[img_index].read()
86
  img = Image.open(io.BytesIO(img_data)).resize((500, 500))
87
 
88
+ # Display Processed Image first
89
+ st.subheader("Processed Image")
90
+ x = st.slider("X Coordinate", 0, 500, 250)
91
+ y = st.slider("Y Coordinate", 0, 500, 250)
92
+ zoom = st.slider("Zoom", 1, 10, 5)
93
+
94
+ with st.expander("Enhancement Settings", expanded=True):
95
+ contrast = st.slider("Contrast", 0.0, 5.0, 1.0)
96
+ brightness = st.slider("Brightness", 0.0, 5.0, 1.0)
97
+ sharpness = st.slider("Sharpness", 0.0, 2.0, 1.0)
98
+
99
+ def apply_enhancements(image):
100
+ zoomed = zoom_at(image, x, y, zoom)
101
+ enhanced_contrast = ImageEnhance.Contrast(zoomed).enhance(contrast)
102
+ enhanced_bright = ImageEnhance.Brightness(enhanced_contrast).enhance(brightness)
103
+ enhanced_sharp = ImageEnhance.Sharpness(enhanced_bright).enhance(sharpness)
104
+ return enhanced_sharp
105
+
106
+ processed_img = apply_enhancements(img)
107
+ st.image(processed_img, caption="Processed Image")
108
+
109
+ # Show Original Image next
110
  st.subheader("Original Image")
111
+ st.image(img, caption="Original Image")
112
+
113
+ # Save Options
114
+ save_image = st.checkbox("Save Processed Image")
115
+ if save_image:
116
+ processed_img.save("image-processed.jpg")
117
+ st.success("Image saved as `image-processed.jpg`")
118
+
119
+ with st.expander("Save Options"):
120
+ description = st.text_area("Describe the image", "")
121
+ if st.button("Save Description"):
122
+ with open("saved_image_description.txt", "w") as f:
123
+ f.write(description)
124
+ st.success("Description saved as `saved_image_description.txt`")
125
+
126
+ if st.button("Save Image Parameters"):
127
+ params = {
128
+ "coordinates_x": x,
129
+ "coordinates_y": y,
130
+ "zoom": zoom,
131
+ "contrast": contrast,
132
+ "brightness": brightness,
133
+ "sharpness": sharpness
134
+ }
135
+ with open("saved_image_parameters.json", "w") as f:
136
+ f.write(pd.DataFrame([params]).to_json(orient="records"))
137
+ st.success("Image parameters saved as `saved_image_parameters.json`")
138
+
139
+ if st.button("Rename Files"):
140
+ file_ext = str(np.random.randint(100))
141
+ os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg")
142
+ os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json")
143
+ os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt")
144
+ st.success("Files renamed successfully")