imagen editor
To upload files, please first save the app
import streamlit as st
import pandas as pd
from PIL import Image
import numpy as np
import base64
from io import BytesIO
st.title("Image Editor")
st.write("Upload an image and edit it in a table format")
# Initialize session state for image data
if "image_data" not in st.session_state:
st.session_state.image_data = None
# File uploader
uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg'])
if uploaded_file is not None:
# Load and display the original image
image = Image.open(uploaded_file)
st.subheader("Original Image")
st.image(image, caption="Uploaded Image", use_column_width=True)
# Convert image to base64 for display in data editor
buffered = BytesIO()
image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
img_data_url = f"data:image/png;base64,{img_str}"
# Create DataFrame with image information
image_info = {
"Property": ["Original Image", "Width", "Height", "Mode", "Format"],
"Value": [img_data_url, str(image.width), str(image.height), image.mode, image.format or "PNG"],
"Editable": [False, True, True, False, False]
}
df = pd.DataFrame(image_info)
st.subheader("Image Properties Editor")
# Configure columns
column_config = {
"Property": st.column_config.TextColumn("Property", disabled=True),
"Value": st.column_config.Column("Value"),
"Editable": st.column_config.CheckboxColumn("Can Edit"),
"Original Image": st.column_config.ImageColumn("Preview", width="medium")
}
# If the first row contains image data, configure it as image column
if len(df) > 0 and df.iloc[0]["Property"] == "Original Image":
column_config["Value"] = st.column_config.ImageColumn("Value", width="large")
# Data editor
edited_df = st.data_editor(
df,
column_config=column_config,
hide_index=True,
disabled=["Property"],
key="image_editor"
)
# Process edited values
try:
new_width = int(edited_df.iloc[1]["Value"])
new_height = int(edited_df.iloc[2]["Value"])
if new_width != image.width or new_height != image.height:
st.subheader("Resized Image")
resized_image = image.resize((new_width, new_height))
st.image(resized_image, caption=f"Resized to {new_width}x{new_height}", use_column_width=True)
# Provide download option
buffered_resized = BytesIO()
resized_image.save(buffered_resized, format="PNG")
st.download_button(
label="Download Resized Image",
data=buffered_resized.getvalue(),
file_name=f"resized_{new_width}x{new_height}.png",
mime="image/png"
)
except ValueError:
st.error("Please enter valid numbers for width and height")
# Additional image editing features
st.subheader("Additional Editing Options")
if uploaded_file is not None:
col1, col2 = st.columns(2)
with col1:
if st.button("Convert to Grayscale"):
gray_image = image.convert('L')
st.image(gray_image, caption="Grayscale", use_column_width=True)
# Download grayscale
buffered_gray = BytesIO()
gray_image.save(buffered_gray, format="PNG")
st.download_button(
label="Download Grayscale",
data=buffered_gray.getvalue(),
file_name="grayscale.png",
mime="image/png"
)
with col2:
rotation = st.slider("Rotate Image", -180, 180, 0)
if rotation != 0:
rotated_image = image.rotate(rotation, expand=True)
st.image(rotated_image, caption=f"Rotated {rotation}°", use_column_width=True)
# Download rotated
buffered_rotated = BytesIO()
rotated_image.save(buffered_rotated, format="PNG")
st.download_button(
label="Download Rotated",
data=buffered_rotated.getvalue(),
file_name=f"rotated_{rotation}.png",
mime="image/png"
)
# Sample images for testing
st.subheader("Sample Images")
st.write("You can also try with these sample images:")
sample_images = [
{
"name": "Sample 1",
"url": "https://via.placeholder.com/300x200/FF6B6B/FFFFFF?text=Sample+1"
},
{
"name": "Sample 2",
"url": "https://via.placeholder.com/400x300/4ECDC4/FFFFFF?text=Sample+2"
},
{
"name": "Sample 3",
"url": "https://via.placeholder.com/200x200/45B7D1/FFFFFF?text=Sample+3"
}
]
sample_df = pd.DataFrame(sample_images)
sample_df["preview"] = sample_df["url"]
st.data_editor(
sample_df,
column_config={
"name": "Image Name",
"url": "URL",
"preview": st.column_config.ImageColumn("Preview", width="medium")
},
hide_index=True,
disabled=True
)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?