cr8 commited on
Commit
901181b
·
verified ·
1 Parent(s): bf8eb41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -38
app.py CHANGED
@@ -45,15 +45,14 @@ def process_image(image, outline_size, outline_color):
45
  # Overlay original image
46
  outlined_img = Image.alpha_composite(outlined_img, processed_img)
47
 
48
-
49
  # Save the result as bytes for Gradio
50
  output_buffer = io.BytesIO()
51
  outlined_img.save(output_buffer, format="PNG", dpi=(300, 300)) # Set DPI here
52
  output_buffer.seek(0)
53
- img_data = base64.b64encode(output_buffer.read()).decode("utf-8")
54
- data_url = f"data:image/png;base64,{img_data}"
55
 
56
- return data_url
57
 
58
  except Exception as e:
59
  print(f"Error processing image: {e}") # Log the error
@@ -62,7 +61,7 @@ def process_image(image, outline_size, outline_color):
62
  # Gradio Interface
63
  with gr.Blocks(title="Sticker Maker") as interface:
64
  gr.Markdown("# Sticker Maker")
65
- gr.Markdown("Upload an image to remove the background and add a white outline.")
66
 
67
  with gr.Row():
68
  with gr.Column():
@@ -78,45 +77,22 @@ with gr.Blocks(title="Sticker Maker") as interface:
78
  create_btn = gr.Button("Create Sticker", variant="primary")
79
 
80
  with gr.Column():
81
- # Use HTML to create an image and a download link
82
- image_output_html = gr.HTML(
83
- """
84
- <div style="position: relative;">
85
- <img id="sticker-preview" src="" style="max-width: 100%; max-height: 400px;">
86
- <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>
87
- </div>
88
- """
89
- )
90
 
91
  def update_image(image, outline_size, outline_color):
92
- try:
93
- data_url = process_image(image, outline_size, outline_color)
94
- if data_url:
95
- return f"""
96
- <div style="position: relative;">
97
- <img id="sticker-preview" src="{data_url}" style="max-width: 100%; max-height: 400px;">
98
- <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>
99
- </div>
100
- """
101
- else:
102
- return """
103
- <div style="position: relative;">
104
- <img id="sticker-preview" src="" style="max-width: 100%; max-height: 400px;">
105
- <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>
106
- </div>
107
- """
108
- except Exception as e:
109
- print(f"Error in update_image: {e}") # Log the error
110
- return f"""
111
- <div style="position: relative;">
112
- Error: {e}
113
- </div>
114
- """
115
 
116
  create_btn.click(
117
  fn=update_image,
118
  inputs=[image_upload, outline_size, outline_color],
119
- outputs=image_output_html,
120
  )
121
 
122
  if __name__ == "__main__":
 
45
  # Overlay original image
46
  outlined_img = Image.alpha_composite(outlined_img, processed_img)
47
 
 
48
  # Save the result as bytes for Gradio
49
  output_buffer = io.BytesIO()
50
  outlined_img.save(output_buffer, format="PNG", dpi=(300, 300)) # Set DPI here
51
  output_buffer.seek(0)
52
+ #img_data = base64.b64encode(output_buffer.read()).decode("utf-8")
53
+ #data_url = f"data:image/png;base64,{img_data}"
54
 
55
+ return output_buffer # Return the image as bytes
56
 
57
  except Exception as e:
58
  print(f"Error processing image: {e}") # Log the error
 
61
  # Gradio Interface
62
  with gr.Blocks(title="Sticker Maker") as interface:
63
  gr.Markdown("# Sticker Maker")
64
+ gr.Markdown("Upload an image to remove the background and add an outline.")
65
 
66
  with gr.Row():
67
  with gr.Column():
 
77
  create_btn = gr.Button("Create Sticker", variant="primary")
78
 
79
  with gr.Column():
80
+ # Use gr.Image for the preview pane
81
+ image_output = gr.Image(label="Sticker Preview", type="pil", image_mode="RGBA")
 
 
 
 
 
 
 
82
 
83
  def update_image(image, outline_size, outline_color):
84
+ processed_image = process_image(image, outline_size, outline_color)
85
+ if processed_image is None:
86
+ return None # Return None if there was an error
87
+ else:
88
+ # Load the image from bytes
89
+ img = Image.open(io.BytesIO(processed_image.getvalue()))
90
+ return img #Return the PIL Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  create_btn.click(
93
  fn=update_image,
94
  inputs=[image_upload, outline_size, outline_color],
95
+ outputs=image_output,
96
  )
97
 
98
  if __name__ == "__main__":