Spaces:
Sleeping
Sleeping
added white background
Browse files- copaint/gradio_ui.py +16 -7
copaint/gradio_ui.py
CHANGED
@@ -26,15 +26,23 @@ fromTensortoPIL = torchvision.transforms.ToPILImage()
|
|
26 |
|
27 |
from PIL import Image
|
28 |
|
29 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
# Convert transparency to white background
|
31 |
if image.mode in ("RGBA", "LA"):
|
32 |
background = Image.new("RGB", image.size, (255, 255, 255)) # white background
|
33 |
background.paste(image, mask=image.split()[-1]) # paste using alpha channel as mask
|
34 |
image = background
|
|
|
35 |
else:
|
|
|
36 |
image = image.convert("RGB") # just to be safe
|
37 |
-
|
38 |
return image # or continue processing
|
39 |
|
40 |
def add_grid_to_image(image, h_cells, w_cells):
|
@@ -189,11 +197,12 @@ def build_gradio_ui():
|
|
189 |
|
190 |
# Upload Design Template
|
191 |
with gr.Column(scale=2):
|
192 |
-
input_image = gr.Image(type="pil", label="Upload Your Design"
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
197 |
)
|
198 |
with gr.Column(scale=1):
|
199 |
# Grid
|
|
|
26 |
|
27 |
from PIL import Image
|
28 |
|
29 |
+
def load_with_transparency(image_file):
|
30 |
+
# Open manually to preserve RGBA
|
31 |
+
image = image_file #Image.open(image_file.name).convert("RGBA")
|
32 |
+
|
33 |
+
# Example: return image size and check mode
|
34 |
+
print( f"Image mode: {image.mode}, size: {image.size}")
|
35 |
+
|
36 |
+
logger.info(f"TRANSPARENCY MODE: {image.mode}")
|
37 |
# Convert transparency to white background
|
38 |
if image.mode in ("RGBA", "LA"):
|
39 |
background = Image.new("RGB", image.size, (255, 255, 255)) # white background
|
40 |
background.paste(image, mask=image.split()[-1]) # paste using alpha channel as mask
|
41 |
image = background
|
42 |
+
logger.info(f"TRANSPARENCY REMOVED WITH WHITE BACKGROUND")
|
43 |
else:
|
44 |
+
logger.info(f"TRANSPARENCY REMOVED")
|
45 |
image = image.convert("RGB") # just to be safe
|
|
|
46 |
return image # or continue processing
|
47 |
|
48 |
def add_grid_to_image(image, h_cells, w_cells):
|
|
|
197 |
|
198 |
# Upload Design Template
|
199 |
with gr.Column(scale=2):
|
200 |
+
input_image = gr.Image(type="pil", image_mode="RGBA", label="Upload Your Design")
|
201 |
+
|
202 |
+
input_image.upload(
|
203 |
+
fn=load_with_transparency,
|
204 |
+
inputs=input_image,
|
205 |
+
outputs=input_image
|
206 |
)
|
207 |
with gr.Column(scale=1):
|
208 |
# Grid
|