TSC4 commited on
Commit
65501ab
·
verified ·
1 Parent(s): f70fbd1

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Untitled5.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1GoN403JoCKCLavITeCYoMzFXKctQ83en
8
+ """
9
+
10
+ !pip install --upgrade gradio_client
11
+ import gradio as gr
12
+ from PIL import Image
13
+ import numpy as np
14
+
15
+ def sanitize_hex_color(color):
16
+ """
17
+ Sanitize and validate hex color input.
18
+ """
19
+ if color.startswith('rgb'):
20
+ rgb_vals = color.strip('rgb()').split(',')
21
+ if len(rgb_vals) == 3:
22
+ try:
23
+ r, g, b = map(int, rgb_vals)
24
+ return f'#{r:02x}{g:02x}{b:02x}'.upper()
25
+ except ValueError:
26
+ raise ValueError("Invalid RGB color format")
27
+ color = color.strip().upper()
28
+ color = color.lstrip('#')
29
+ if len(color) != 6:
30
+ raise ValueError("Invalid hex color. Use '#RRGGBB' format")
31
+ try:
32
+ int(color, 16)
33
+ except ValueError:
34
+ raise ValueError("Invalid hexadecimal color")
35
+ return f'#{color}'
36
+
37
+ def remove_background_color(image, hex_color, tolerance):
38
+ """
39
+ Remove pixels of a specified hex color from an image.
40
+ """
41
+ if isinstance(image, np.ndarray):
42
+ image = Image.fromarray(image)
43
+ image = image.convert("RGBA")
44
+ try:
45
+ hex_color = sanitize_hex_color(hex_color)
46
+ target_color = tuple(int(hex_color.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
47
+ except ValueError as e:
48
+ raise ValueError(str(e))
49
+ data = np.array(image)
50
+ mask = np.all(np.abs(data[:,:,:3] - target_color) <= tolerance, axis=2)
51
+ alpha_channel = np.where(mask, 0, 255)
52
+ data[:, :, 3] = alpha_channel
53
+ return data
54
+
55
+ def color_remover_interface(image, hex_color, tolerance):
56
+ """
57
+ Gradio interface function for color removal.
58
+ """
59
+ try:
60
+ if image is None:
61
+ raise ValueError("Please upload an image")
62
+ hex_color = sanitize_hex_color(hex_color)
63
+ processed_image = remove_background_color(image, hex_color, tolerance)
64
+ return image, processed_image
65
+ except Exception as e:
66
+ gr.Warning(str(e))
67
+ return image, image
68
+
69
+ # Deploy using Gradio Client
70
+ iface = gr.Interface(
71
+ fn=color_remover_interface,
72
+ inputs=[
73
+ gr.Image(type="numpy", label="Upload Image"),
74
+ gr.Textbox(label="Hex Color to Remove", placeholder="#FFFFFF"),
75
+ gr.Slider(minimum=0, maximum=255, value=20, step=1, label="Color Tolerance")
76
+ ],
77
+ outputs=[
78
+ gr.Image(type="numpy", label="Original Image"),
79
+ gr.Image(type="numpy", label="Processed Image")
80
+ ],
81
+ title="Image Color Remover",
82
+ description="""
83
+ Remove a specific color from your image with precision!
84
+
85
+ ### How to Use:
86
+ 1. Upload an image
87
+ 2. Choose the color to remove
88
+ 3. Adjust the color tolerance
89
+ 4. See the magic happen!
90
+ """,
91
+ # api_name="your-color-remover-app" # You can optionally set a custom API name
92
+ )
93
+
94
+ iface.launch(share=True) # Launch the interface with a shareable link