gustavozf's picture
Update app.py
795776e verified
import os
import tempfile
import gradio as gr
from PIL import Image
# Create a temporary directory for output files
temp_dir = tempfile.mkdtemp()
def merge_images(egc_fpath, enc_fpath):
# Open the images
if egc_fpath is None or enc_fpath is None:
return None, "⚠️ Please upload both images."
try:
egc = Image.open(egc_fpath).convert("HSV")
enc = Image.open(enc_fpath).convert("HSV")
egc = egc.split()[2] # Extract the values
enc = enc.split()[2] # Extract the values
# Create a new image with the same size as the input images
merged_image = Image.merge("RGB", (egc, enc, Image.new("L", egc.size)))
egc_name = os.path.splitext(os.path.basename(egc_fpath))[0]
enc_name = os.path.splitext(os.path.basename(enc_fpath))[0]
merged_name = f"{egc_name}_{enc_name}_merged.jpg"
fout_path = os.path.join(temp_dir, merged_name)
merged_image.save(fout_path, "JPEG")
except Exception as e:
return None, f"⚠️ Error: {str(e)}"
return fout_path, f"✅ Image created with success!",
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown(
"""
# ENS Image Merger
Upload images of Enteric Neurons and Enteric Glial Cells ir order to
create a merged image.
"""
)
alert = gr.Markdown("")
with gr.Row():
with gr.Column():
egc_image = gr.Image(type="filepath", label="Enteric Glial Cell")
with gr.Column():
enc_image = gr.Image(type="filepath", label="Enteric Neuron")
with gr.Column():
merged_image = gr.Image(type="filepath", label="Merged Image")
with gr.Row():
clear_button = gr.Button("Clear")
merge_button = gr.Button("Merge", variant="primary")
clear_button.click(
lambda: (None, None, None),
inputs=[],
outputs=[egc_image, enc_image, merged_image])
# Merge button
merge_button.click(
merge_images,
inputs=[egc_image, enc_image],
outputs=[merged_image, alert])
demo.launch()