Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34566789101011121316 unmodified lines30313233343536333435393637383940454647484941424344455253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061074647484950112515253544 unmodified lines59606112312462636465665 unmodified lines727374136757677782 unmodified linesimport rembgimport ioimport osimport shutilimport tempfile
def process_image(image, outline_size, outline_color, add_outline): if image is None: return None, None return None # Convert hex color to RGB r = int(outline_color[1:3], 16)16 unmodified lines background = Image.new('RGBA', (processed_img.width + 2*outline_size, processed_img.height + 2*outline_size), (0, 0, 0, 0)) # Start with transparent background # Create outline mask outline_img = Image.new('RGBA', background.size, rgb_color + (255,)) rgb_color + (255,)) # Paste the processed image onto the background background.paste(outline_img, (0, 0), create_outline_mask(processed_img, outline_size)) background.paste(processed_img, (outline_size, outline_size), processed_img) final_img = background else: final_img = processed_img # Create output directory if it doesn't exist os.makedirs("outputs", exist_ok=True) # Save as PNG with transparency preserved output_path = "outputs/sticker.png" # Use a temporary file with .png extension to ensure correct format temp_dir = tempfile.gettempdir() output_path = os.path.join(temp_dir, "sticker.png") final_img.save(output_path, format="PNG") # Create a copy for gradio to return (workaround for download issues) download_path = "outputs/download_sticker.png" shutil.copy2(output_path, download_path) return final_img, download_path
def create_outline_mask(image, thickness): """Create a mask for the outline based on the image alpha channel""" # Get mask from alpha channel mask = image.split()[3] w, h = image.size expanded = Image.new('L', (w + 2*thickness, h + 2*thickness), 0) expanded.paste(mask, (thickness, thickness)) # Create outline mask result = Image.new('L', expanded.size, 0) for i in range(thickness): # Expand the mask in all 8 directions for each thickness level temp = Image.new('L', expanded.size, 0) # Expand right temp.paste(expanded, (-1, 0)) result = Image.composite(temp, result, temp) # Expand left temp = Image.new('L', expanded.size, 0) temp.paste(expanded, (1, 0)) result = Image.composite(temp, result, temp) # Expand up temp = Image.new('L', expanded.size, 0) temp.paste(expanded, (0, 1)) result = Image.composite(temp, result, temp) # Expand down temp = Image.new('L', expanded.size, 0) temp.paste(expanded, (0, -1)) result = Image.composite(temp, result, temp) # Expand diagonal directions temp = Image.new('L', expanded.size, 0) temp.paste(expanded, (-1, -1)) result = Image.composite(temp, result, temp) temp = Image.new('L', expanded.size, 0) temp.paste(expanded, (-1, 1)) result = Image.composite(temp, result, temp) temp = Image.new('L', expanded.size, 0) temp.paste(expanded, (1, -1)) result = Image.composite(temp, result, temp) temp = Image.new('L', expanded.size, 0) temp.paste(expanded, (1, 1)) result = Image.composite(temp, result, temp) # Subtract the original mask to get just the outline for y in range(expanded.height): for x in range(expanded.width): if expanded.getpixel((x, y)) > 0: result.putpixel((x, y), 0) return result return output_path
# Gradio Interfacewith gr.Blocks(title="Sticker Maker") as interface: gr.Markdown("# Sticker Maker") gr.Markdown("Remove background, add optional outline, and download as transparent PNG") gr.Markdown("Upload an image to remove background and add optional outline") with gr.Row(): with gr.Column():4 unmodified lines create_btn = gr.Button("Create Sticker", variant="primary") with gr.Column(): output_preview = gr.Image(label="Preview (Transparent Background)", type="pil") output_file = gr.File(label="Download PNG") # Single output component for preview and download output_image = gr.Image(label="Transparent Background", type="filepath", container=False) # Make outline options visible only when add_outline is checked add_outline.change(5 unmodified lines create_btn.click( fn=process_image, inputs=[image_upload, outline_size, outline_color, add_outline], outputs=[output_preview, output_file] outputs=output_image )
if __name__ == "__main__":app.py+11-72