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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -53
app.py CHANGED
@@ -8,43 +8,48 @@ 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()
13
- image.save(img_byte_arr, format="PNG")
14
- img_byte_arr.seek(0)
15
-
16
- # Remove background
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:
@@ -71,22 +76,25 @@ with gr.Blocks(title="Sticker Maker") as interface:
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,
 
8
  if image is None:
9
  return None
10
 
11
+ try:
12
+ # Convert image to bytes for rembg
13
+ img_byte_arr = io.BytesIO()
14
+ image.save(img_byte_arr, format="PNG")
15
+ img_byte_arr.seek(0)
16
+
17
+ # Remove background
18
+ processed_bytes = rembg.remove(img_byte_arr.getvalue())
19
+ processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
20
+
21
+ # Create outline mask
22
+ alpha = processed_img.split()[-1]
23
+ alpha = Image.merge('L', [alpha])
24
+ blurred = alpha.filter(ImageFilter.GaussianBlur(radius=outline_size))
25
+ new_alpha = blurred.point(lambda x: 0 if x == 0 else 255) # Binary mask
26
+
27
+ # Apply outline to the transparent image
28
+ outlined_img = Image.new("RGBA", processed_img.size, (0, 0, 0, 0)) # Fully transparent
29
+ pixels = outlined_img.load()
30
+
31
+ alpha_pixels = new_alpha.load()
32
+ for x in range(outlined_img.width):
33
+ for y in range(outlined_img.height):
34
+ if alpha_pixels[x, y] > 0:
35
+ pixels[x, y] = (255, 255, 255, 255) # White outline
36
+
37
+ # Overlay original image
38
+ outlined_img = Image.alpha_composite(outlined_img, processed_img)
39
+
40
+
41
+ # Save the result as bytes for Gradio
42
+ output_buffer = io.BytesIO()
43
+ outlined_img.save(output_buffer, format="PNG")
44
+ output_buffer.seek(0)
45
+ img_data = base64.b64encode(output_buffer.read()).decode("utf-8")
46
+ data_url = f"data:image/png;base64,{img_data}"
47
+
48
+ return data_url
49
+
50
+ except Exception as e:
51
+ print(f"Error processing image: {e}") # Log the error
52
+ return None
53
 
54
  # Gradio Interface
55
  with gr.Blocks(title="Sticker Maker") as interface:
 
76
  )
77
 
78
  def update_image(image, outline_size):
79
+ try:
80
+ data_url = process_image(image, outline_size)
81
+ if data_url:
82
+ return f"""
83
+ <div style="position: relative;">
84
+ <img id="sticker-preview" src="{data_url}" style="max-width: 100%; max-height: 400px;">
85
+ <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>
86
+ </div>
87
+ """
88
+ else:
89
+ return """
90
+ <div style="position: relative;">
91
+ <img id="sticker-preview" src="" style="max-width: 100%; max-height: 400px;">
92
+ <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>
93
+ </div>
94
+ """
95
+ except Exception as e:
96
+ print(f"Error in update_image: {e}") # Log the error
97
+ return f"Error: {e}" # Display error message in preview pane
98
 
99
  create_btn.click(
100
  fn=update_image,