''' This is the originall CLL Explorer application that allows users to upload, process, and save images. The application provides the following functionalities: - Upload microscope images. - Adjust image view with zoom and enhancement controls. - Detect and measure cells automatically. - Save analysis results and annotations. The application is divided into the following sections: 1. **Upload Images**: Users can upload microscope images in JPG or PNG format. 2. **Select Image**: Users can select an image from the uploaded files. 3. **Processed Image**: Displays the processed image with zoom and enhancement controls. 4. **Image Controls**: Allows users to adjust the image view with sliders for X and Y coordinates, zoom, contrast, brightness, and sharpness. 5. **Save Options**: Provides options to save the processed image, image description, and image parameters. To run the application: 1. Save the script in a Python file (e.g., app.py). 2. Run the script using the Streamlit command: ```bash streamlit run app.py ''' import streamlit as st from PIL import Image, ImageEnhance import pandas as pd import numpy as np import io import os def zoom_at(img, x, y, zoom): ''' Zoom into an image at a specific location. Parameters: ---------- img : PIL.Image Input image. x : int X-coordinate of the zoom center. y : int Y-coordinate of the zoom center. zoom : float Zoom factor. Returns: ------- PIL.Image Zoomed image. Examples: -------- >>> img = Image.open('image.jpg') >>> zoomed_img = zoom_at(img, x=100, y=100, zoom=2.0) ''' w, h = img.size zoom2 = zoom * 2 img = img.crop((x - w / zoom2, y - h / zoom2, x + w / zoom2, y + h / zoom2)) return img.resize((w, h), Image.LANCZOS) def apply_enhancements(img, x, y, zoom, contrast, brightness, sharpness): ''' Apply zoom and image enhancements to the input image. Parameters: ---------- img : PIL.Image Input image. x : int X-coordinate of the zoom center. y : int Y-coordinate of the zoom center. zoom : float Zoom factor. contrast : float Contrast adjustment factor. brightness : float Brightness adjustment factor. sharpness : float Sharpness adjustment factor. Returns: ------- PIL.Image Enhanced image. ''' zoomed = zoom_at(img, x, y, zoom) enhanced_contrast = ImageEnhance.Contrast(zoomed).enhance(contrast) enhanced_brightness = ImageEnhance.Brightness(enhanced_contrast).enhance(brightness) enhanced_sharpness = ImageEnhance.Sharpness(enhanced_brightness).enhance(sharpness) return enhanced_sharpness st.set_page_config(page_title="CLL Explorer", layout="wide") st.title("CLL Explorer - Malaria Cell Detection") st.markdown(""" ### About This Application This tool assists researchers in analyzing microscope images of any cell type. Or something else entirely. - **Upload** microscope images. - **Adjust** image view with zoom and enhancement controls. - **Detect** and measure cells automatically. - **Save** analysis results and annotations. """) uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, type=["jpg", "png"]) if uploaded_files: img_index = st.selectbox( "Select Image", range(len(uploaded_files)), format_func=lambda x: uploaded_files[x].name ) img_data = uploaded_files[img_index].read() img = Image.open(io.BytesIO(img_data)).resize((500, 500)) # Sidebar for Image Controls with st.sidebar: st.header("Image Controls") x = st.slider("X Coordinate", 0, 500, 250) y = st.slider("Y Coordinate", 0, 500, 250) zoom = st.slider("Zoom", 1, 10, 5) with st.expander("Enhancement Settings", expanded=True): contrast = st.slider("Contrast", 0.0, 5.0, 1.0) brightness = st.slider("Brightness", 0.0, 5.0, 1.0) sharpness = st.slider("Sharpness", 0.0, 2.0, 1.0) if st.button("Apply Adjustments"): processed_img = apply_enhancements(img, x, y, zoom, contrast, brightness, sharpness) st.session_state.processed_img = processed_img else: processed_img = apply_enhancements(img, x, y, zoom, contrast, brightness, sharpness) st.session_state.processed_img = processed_img # Display Processed Image st.subheader("Processed Image") if 'processed_img' in st.session_state: st.image(st.session_state.processed_img, use_column_width=True, caption="Processed Image") else: st.image(processed_img, use_column_width=True, caption="Processed Image") # Display Original Image st.subheader("Original Image") st.image(img, use_column_width=True, caption="Original Image") # Save Options save_image = st.checkbox("Save Processed Image") if save_image: st.session_state.processed_img.save("image-processed.jpg") st.success("Image saved as `image-processed.jpg`") with st.expander("Save Options"): description = st.text_area("Describe the image", "") if st.button("Save Description"): with open("saved_image_description.txt", "w") as f: f.write(description) st.success("Description saved as `saved_image_description.txt`") if st.button("Save Image Parameters"): params = { "coordinates_x": x, "coordinates_y": y, "zoom": zoom, "contrast": contrast, "brightness": brightness, "sharpness": sharpness } with open("saved_image_parameters.json", "w") as f: f.write(pd.DataFrame([params]).to_json(orient="records")) st.success("Image parameters saved as `saved_image_parameters.json`") if st.button("Rename Files"): file_ext = str(np.random.randint(100)) os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg") os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json") os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt") st.success("Files renamed successfully")