add download button for edited image

This commit is contained in:
Carl Zhang
2025-10-03 11:45:14 -05:00
parent 38ce273035
commit 62f712c056
2 changed files with 20 additions and 8 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ files/student_locations.db
*venv/
__pycache__/
*.pyc
*.cpython-312.pyc

View File

@@ -31,7 +31,9 @@ def edit_image_with_ai(image, description):
image_response = Image.open(BytesIO(part.inline_data.data))
return text_response, image_response
def ai_image_editor():
st.markdown("<h1 style='text-align: center; color: #451002;'>AI Image Editor🖼</h1>", unsafe_allow_html=True)
st.markdown("<h5 style='text-align: center;'> Edit and enhance your images with the power of AI! 🎨 </h3>", unsafe_allow_html=True)
@@ -40,7 +42,7 @@ def ai_image_editor():
reference_prompt = """
**Sample Prompt Description:**
Please edit the uploaded image to create a professional profile photo of the person in the picture. Focus on the following:
Edit the uploaded image to create a professional profile photo of the person in the picture. Focus on the following:
- Outfit: Transform the current attire into a fitted, black professional suit. Ensure the fit is flattering and appropriate for a software engineer headshot.
- Facial Features & Hair: Retain the original facial features (eyes, nose, mouth, etc.) and individual hair strands as much as possible. The goal is to achieve a realistic, edited look, not an AI-generated or overly smoothed/perfected appearance. Avoid any artificial alterations to these features.
- Pose Adjustment: Adjust the pose so that the face is facing forward. Make subtle corrections to the body posture to appear slightly straightened and more upright.
@@ -52,16 +54,15 @@ def ai_image_editor():
# user upload an image and provide a description of the edit they want
uploaded_image = st.file_uploader("Upload an image and write prompt description to edit", type=["png", "jpg", "jpeg"], help="Choose an image file (PNG, JPG, JPEG) to upload.")
if uploaded_image is not None:
with st.expander("See Example Prompt for Professional Profile Photo"):
st.info("Note: The example prompt is for reference only. You can provide your own description of the edits you want to make to the image. The quality of the results may vary based on the specificity of your prompt. For more information, see the [Gemini Official Prompting Guide and Strategies](https://ai.google.dev/gemini-api/docs/image-generation#prompt-guide)")
st.markdown(reference_prompt)
with st.spinner("Loading image..."):
# display the uploaded image
image = Image.open(uploaded_image)
st.image(image, width= 300)
st.image(image, width=300)
st.success("Image uploaded successfully!")
with st.expander("See Example Prompt for Professional Profile Photo"):
st.info("Note: The example prompt is for reference only. You can provide your own description of the edits you want to make to the image. The quality of the results may vary based on the specificity of your prompt. For more information, see the [Gemini Official Prompting Guide and Strategies](https://ai.google.dev/gemini-api/docs/image-generation#prompt-guide)")
st.markdown(reference_prompt)
edit_description = st.text_area("Describe the edits you want to make to the image", height="content",)
if st.button("AI Editing"):
if edit_description.strip() == "":
@@ -74,4 +75,14 @@ def ai_image_editor():
st.info(text_response)
if image_response:
st.image(image_response, width=300)
# Convert PIL image to bytes for download
img_byte_arr = BytesIO()
image_response.save(img_byte_arr, format="JPEG")
st.download_button(
label="Download Edited Image",
data=img_byte_arr.getvalue(),
file_name="edited_image.jpeg",
mime="image/jpeg"
)