Spaces:
Running
Running
Update app.py
Browse files44 unmodified lines4546474849505152535455565758592 unmodified lines626364656667689 unmodified lines7879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112244 unmodified lines# Overlay original imageoutlined_img = Image.alpha_composite(outlined_img, processed_img)# Save the result as bytes for Gradiooutput_buffer = io.BytesIO()outlined_img.save(output_buffer, format="PNG", dpi=(300, 300)) # Set DPI hereoutput_buffer.seek(0)img_data = base64.b64encode(output_buffer.read()).decode("utf-8")data_url = f"data:image/png;base64,{img_data}"return data_urlexcept Exception as e:print(f"Error processing image: {e}") # Log the error2 unmodified lines# Gradio Interfacewith gr.Blocks(title="Sticker Maker") as interface:gr.Markdown("# Sticker Maker")gr.Markdown("Upload an image to remove the background and add a white outline.")with gr.Row():with gr.Column():9 unmodified linescreate_btn = gr.Button("Create Sticker", variant="primary")with gr.Column():# Use HTML to create an image and a download linkimage_output_html = gr.HTML("""<div style="position: relative;"><img id="sticker-preview" src="" style="max-width: 100%; max-height: 400px;"><a id="download-link" style="position: absolute; top: 10px; right: 10px; background-color: rgba(255, 255, 255, 0.7); padding: 5px; border-radius: 5px; display:none;" download="sticker.png">Download</a></div>""")def update_image(image, outline_size, outline_color):try:data_url = process_image(image, outline_size, outline_color)if data_url:return f"""<div style="position: relative;"><img id="sticker-preview" src="{data_url}" style="max-width: 100%; max-height: 400px;"><a id="download-link" href="{data_url}" download="sticker.png" style="position: absolute; top: 10px; right: 10px; background-color: rgba(255, 255, 255, 0.7); padding: 5px; border-radius: 5px;">Download</a></div>"""else:return """<div style="position: relative;"><img id="sticker-preview" src="" style="max-width: 100%; max-height: 400px;"><a id="download-link" style="position: absolute; top: 10px; right: 10px; background-color: rgba(255, 255, 255, 0.7); padding: 5px; border-radius: 5px; display:none;" download="sticker.png">Download</a></div>"""except Exception as e:print(f"Error in update_image: {e}") # Log the errorreturn f"""<div style="position: relative;">Error: {e}</div>"""create_btn.click(fn=update_image,inputs=[image_upload, outline_size, outline_color],outputs=image_output_html,)if __name__ == "__main__":44 unmodified lines45464748495051525354555657582 unmodified lines616263646566679 unmodified lines7778798081828384858687888990919293949596979844 unmodified lines# Overlay original imageoutlined_img = Image.alpha_composite(outlined_img, processed_img)# Save the result as bytes for Gradiooutput_buffer = io.BytesIO()outlined_img.save(output_buffer, format="PNG", dpi=(300, 300)) # Set DPI hereoutput_buffer.seek(0)#img_data = base64.b64encode(output_buffer.read()).decode("utf-8")#data_url = f"data:image/png;base64,{img_data}"return output_buffer # Return the image as bytesexcept Exception as e:print(f"Error processing image: {e}") # Log the error2 unmodified lines# Gradio Interfacewith gr.Blocks(title="Sticker Maker") as interface:gr.Markdown("# Sticker Maker")gr.Markdown("Upload an image to remove the background and add an outline.")with gr.Row():with gr.Column():9 unmodified linescreate_btn = gr.Button("Create Sticker", variant="primary")with gr.Column():# Use gr.Image for the preview paneimage_output = gr.Image(label="Sticker Preview", type="pil", image_mode="RGBA")def update_image(image, outline_size, outline_color):processed_image = process_image(image, outline_size, outline_color)if processed_image is None:return None # Return None if there was an errorelse:# Load the image from bytesimg = Image.open(io.BytesIO(processed_image.getvalue()))return img #Return the PIL Imagecreate_btn.click(fn=update_image,inputs=[image_upload, outline_size, outline_color],outputs=image_output,)if __name__ == "__main__":
app.py+14-38