Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageEnhance
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
def zoom_at(img, x, y, zoom):
|
| 9 |
+
w, h = img.size
|
| 10 |
+
zoom2 = zoom * 2
|
| 11 |
+
img = img.crop((x - w / zoom2, y - h / zoom2,
|
| 12 |
+
x + w / zoom2, y + h / zoom2))
|
| 13 |
+
return img.resize((w, h), Image.LANCZOS)
|
| 14 |
+
|
| 15 |
+
st.title("Cell Explorer")
|
| 16 |
+
|
| 17 |
+
uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, type="jpg")
|
| 18 |
+
|
| 19 |
+
if uploaded_files:
|
| 20 |
+
img_index = st.selectbox("Select Image", range(len(uploaded_files)))
|
| 21 |
+
x = st.slider("X Coordinate", 0, 500, 205)
|
| 22 |
+
y = st.slider("Y Coordinate", 0, 500, 250)
|
| 23 |
+
zoom = st.slider("Zoom", 1, 10, 5)
|
| 24 |
+
contrast = st.slider("Contrast", 0.0, 5.0, 1.0)
|
| 25 |
+
brightness = st.slider("Brightness", 0.0, 5.0, 1.0)
|
| 26 |
+
sharpness = st.slider("Sharpness", 0.0, 2.0, 1.0)
|
| 27 |
+
save_image = st.checkbox("Save Image")
|
| 28 |
+
|
| 29 |
+
img_data = uploaded_files[img_index].read()
|
| 30 |
+
img = Image.open(io.BytesIO(img_data)).resize((500, 500))
|
| 31 |
+
img_zoomed = zoom_at(img, x, y, zoom)
|
| 32 |
+
img_contrast = ImageEnhance.Contrast(img_zoomed).enhance(contrast)
|
| 33 |
+
img_bright = ImageEnhance.Brightness(img_contrast).enhance(brightness)
|
| 34 |
+
img_sharp = ImageEnhance.Sharpness(img_bright).enhance(sharpness)
|
| 35 |
+
|
| 36 |
+
if save_image:
|
| 37 |
+
img_sharp.save("image-processed.jpg")
|
| 38 |
+
st.success("Image saved as image-processed.jpg")
|
| 39 |
+
|
| 40 |
+
st.image(img_sharp, caption="Processed Image", use_column_width=True)
|
| 41 |
+
|
| 42 |
+
description = st.text_area("Describe the image", "")
|
| 43 |
+
if st.button("Save Description"):
|
| 44 |
+
with open("saved_image_description.txt", "w") as f:
|
| 45 |
+
f.write(description)
|
| 46 |
+
st.success("Description saved as saved_image_description.txt")
|
| 47 |
+
|
| 48 |
+
if st.button("Save Image Parameters"):
|
| 49 |
+
params = {
|
| 50 |
+
"coordinates_x": x,
|
| 51 |
+
"coordinates_y": y,
|
| 52 |
+
"zoom": zoom,
|
| 53 |
+
"contrast": contrast,
|
| 54 |
+
"brightness": brightness,
|
| 55 |
+
"sharpness": sharpness
|
| 56 |
+
}
|
| 57 |
+
with open("saved_image_parameters.json", "w") as f:
|
| 58 |
+
f.write(pd.DataFrame([params]).to_json(orient="records"))
|
| 59 |
+
st.success("Image parameters saved as saved_image_parameters.json")
|
| 60 |
+
|
| 61 |
+
if st.button("Rename Files"):
|
| 62 |
+
file_ext = str(np.random.randint(100))
|
| 63 |
+
os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg")
|
| 64 |
+
os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json")
|
| 65 |
+
os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt")
|
| 66 |
+
st.success("Files renamed successfully")
|