cr8 commited on
Commit
b3ad321
·
verified ·
1 Parent(s): afaf3da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -42
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import gradio as gr
2
- from PIL import Image
3
  import rembg
4
  import io
 
5
 
6
  def process_image(image, outline_size):
7
  if image is None:
8
- return None, None # Return None for both image and file
9
 
10
  # Convert image to bytes for rembg
11
  img_byte_arr = io.BytesIO()
@@ -16,35 +17,34 @@ def process_image(image, outline_size):
16
  processed_bytes = rembg.remove(img_byte_arr.getvalue())
17
  processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
18
 
19
- # Add white outline
20
- if outline_size > 0:
21
- background = Image.new(
22
- "RGBA",
23
- (
24
- processed_img.width + 2 * outline_size,
25
- processed_img.height + 2 * outline_size,
26
- ),
27
- (0, 0, 0, 0), # Transparent background
28
- )
29
- background.paste(processed_img, (outline_size, outline_size), processed_img)
30
-
31
- # Draw white outline
32
- for x in range(background.width):
33
- for y in range(background.height):
34
- if (x < outline_size or y < outline_size or x >= background.width - outline_size or y >= background.height - outline_size):
35
- if background.getpixel((x, y))[3] == 0: #Check if pixel is originally transparent
36
- background.putpixel((x, y), (255, 255, 255, 255)) #Set to white if so
37
- final_img = background
38
-
39
- else:
40
- final_img = processed_img
41
-
42
- # Save as PNG to a BytesIO object
43
  output_buffer = io.BytesIO()
44
- final_img.save(output_buffer, format="PNG")
45
  output_buffer.seek(0)
 
 
46
 
47
- return final_img, output_buffer
48
 
49
  # Gradio Interface
50
  with gr.Blocks(title="Sticker Maker") as interface:
@@ -60,25 +60,38 @@ with gr.Blocks(title="Sticker Maker") as interface:
60
  create_btn = gr.Button("Create Sticker", variant="primary")
61
 
62
  with gr.Column():
63
- output_image = gr.Image(label="Preview", type="pil", interactive=False)
64
- download_link = gr.HTML("<a style='display: none;' id='download-link'>Download Sticker</a>")
65
-
66
- def update_outputs(image, outline_size):
67
- processed_image, image_bytes = process_image(image, outline_size)
68
- if processed_image is None:
69
- return None, "<a style='display: none;' id='download-link'>Download Sticker</a>"
 
 
70
 
71
- # Create a data URL for the image
72
- data_url = f"data:image/png;base64,{image_bytes.getvalue().hex()}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- # Create the download link
75
- download_html = f"<a href='{data_url}' download='sticker.png'>Download Sticker</a>"
76
- return processed_image, download_html
77
 
78
  create_btn.click(
79
- fn=update_outputs,
80
  inputs=[image_upload, outline_size],
81
- outputs=[output_image, download_link],
82
  )
83
 
84
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from PIL import Image, ImageFilter
3
  import rembg
4
  import io
5
+ import base64
6
 
7
  def process_image(image, outline_size):
8
  if image is None:
9
+ return None
10
 
11
  # Convert image to bytes for rembg
12
  img_byte_arr = io.BytesIO()
 
17
  processed_bytes = rembg.remove(img_byte_arr.getvalue())
18
  processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
19
 
20
+ # Create outline mask
21
+ alpha = processed_img.split()[-1]
22
+ alpha = Image.merge('L', [alpha])
23
+ blurred = alpha.filter(ImageFilter.GaussianBlur(radius=outline_size))
24
+ new_alpha = blurred.point(lambda x: 0 if x == 0 else 255) # Binary mask
25
+
26
+ # Apply outline to the transparent image
27
+ outlined_img = Image.new("RGBA", processed_img.size, (0, 0, 0, 0)) # Fully transparent
28
+ pixels = outlined_img.load()
29
+
30
+ alpha_pixels = new_alpha.load()
31
+ for x in range(outlined_img.width):
32
+ for y in range(outlined_img.height):
33
+ if alpha_pixels[x, y] > 0:
34
+ pixels[x, y] = (255, 255, 255, 255) # White outline
35
+
36
+ # Overlay original image
37
+ outlined_img = Image.alpha_composite(outlined_img, processed_img)
38
+
39
+
40
+ # Save the result as bytes for Gradio
 
 
 
41
  output_buffer = io.BytesIO()
42
+ outlined_img.save(output_buffer, format="PNG")
43
  output_buffer.seek(0)
44
+ img_data = base64.b64encode(output_buffer.read()).decode("utf-8")
45
+ data_url = f"data:image/png;base64,{img_data}"
46
 
47
+ return data_url
48
 
49
  # Gradio Interface
50
  with gr.Blocks(title="Sticker Maker") as interface:
 
60
  create_btn = gr.Button("Create Sticker", variant="primary")
61
 
62
  with gr.Column():
63
+ # Use HTML to create an image and a download link
64
+ image_output_html = gr.HTML(
65
+ """
66
+ <div style="position: relative;">
67
+ <img id="sticker-preview" src="" style="max-width: 100%; max-height: 400px;">
68
+ <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>
69
+ </div>
70
+ """
71
+ )
72
 
73
+ def update_image(image, outline_size):
74
+ data_url = process_image(image, outline_size)
75
+ if data_url:
76
+ return f"""
77
+ <div style="position: relative;">
78
+ <img id="sticker-preview" src="{data_url}" style="max-width: 100%; max-height: 400px;">
79
+ <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>
80
+ </div>
81
+ """
82
+ else:
83
+ return """
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
  create_btn.click(
92
+ fn=update_image,
93
  inputs=[image_upload, outline_size],
94
+ outputs=image_output_html,
95
  )
96
 
97
  if __name__ == "__main__":