Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34567891011121316 unmodified lines303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141154 unmodified lines1201211221231241251261275 unmodified lines1331341351361371381392 unmodified linesimport rembgimport ioimport osimport shutildef process_image(image, outline_size, outline_color, add_outline):if image is None:return None, None# Convert hex color to RGBr = int(outline_color[1:3], 16)16 unmodified linesbackground = 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 maskoutline_img = Image.new('RGBA', background.size, rgb_color + (255,))# Paste the processed image onto the backgroundbackground.paste(outline_img, (0, 0), create_outline_mask(processed_img, outline_size))background.paste(processed_img, (outline_size, outline_size), processed_img)final_img = backgroundelse:final_img = processed_img# Create output directory if it doesn't existos.makedirs("outputs", exist_ok=True)# Save as PNG with transparency preservedoutput_path = "outputs/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_pathdef create_outline_mask(image, thickness):"""Create a mask for the outline based on the image alpha channel"""# Get mask from alpha channelmask = image.split()[3]w, h = image.sizeexpanded = Image.new('L', (w + 2*thickness, h + 2*thickness), 0)expanded.paste(mask, (thickness, thickness))# Create outline maskresult = Image.new('L', expanded.size, 0)for i in range(thickness):# Expand the mask in all 8 directions for each thickness leveltemp = Image.new('L', expanded.size, 0)# Expand righttemp.paste(expanded, (-1, 0))result = Image.composite(temp, result, temp)# Expand lefttemp = Image.new('L', expanded.size, 0)temp.paste(expanded, (1, 0))result = Image.composite(temp, result, temp)# Expand uptemp = Image.new('L', expanded.size, 0)temp.paste(expanded, (0, 1))result = Image.composite(temp, result, temp)# Expand downtemp = Image.new('L', expanded.size, 0)temp.paste(expanded, (0, -1))result = Image.composite(temp, result, temp)# Expand diagonal directionstemp = 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 outlinefor y in range(expanded.height):for x in range(expanded.width):if expanded.getpixel((x, y)) > 0:result.putpixel((x, y), 0)return result# 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")with gr.Row():with gr.Column():4 unmodified linescreate_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")# Make outline options visible only when add_outline is checkedadd_outline.change(5 unmodified linescreate_btn.click(fn=process_image,inputs=[image_upload, outline_size, outline_color, add_outline],outputs=[output_preview, output_file])if __name__ == "__main__":2 unmodified lines34567891011121316 unmodified lines303132333435363738394041424344454647484950515253544 unmodified lines59606162636465665 unmodified lines727374757677782 unmodified linesimport rembgimport ioimport osimport tempfiledef process_image(image, outline_size, outline_color, add_outline):if image is None:return None# Convert hex color to RGBr = int(outline_color[1:3], 16)16 unmodified linesbackground = Image.new('RGBA',(processed_img.width + 2*outline_size,processed_img.height + 2*outline_size),rgb_color + (255,))# Paste the processed image onto the backgroundbackground.paste(processed_img, (outline_size, outline_size), processed_img)final_img = backgroundelse:final_img = processed_img# Use a temporary file with .png extension to ensure correct formattemp_dir = tempfile.gettempdir()output_path = os.path.join(temp_dir, "sticker.png")final_img.save(output_path, format="PNG")return output_path# Gradio Interfacewith gr.Blocks(title="Sticker Maker") as interface:gr.Markdown("# Sticker Maker")gr.Markdown("Upload an image to remove background and add optional outline")with gr.Row():with gr.Column():4 unmodified linescreate_btn = gr.Button("Create Sticker", variant="primary")with gr.Column():# Single output component for preview and downloadoutput_image = gr.Image(label="Transparent Background", type="filepath", container=False)# Make outline options visible only when add_outline is checkedadd_outline.change(5 unmodified linescreate_btn.click(fn=process_image,inputs=[image_upload, outline_size, outline_color, add_outline],outputs=output_image)if __name__ == "__main__":
app.py+11-72