TSC4 commited on
Commit
1ba86f9
·
verified ·
1 Parent(s): 74c568d

Upload appv2.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. appv2.py +93 -0
appv2.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app code for remove.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
+ import gradio as gr
11
+ from PIL import Image
12
+ import numpy as np
13
+
14
+ def sanitize_hex_color(color):
15
+ """
16
+ Sanitize and validate hex color input.
17
+ """
18
+ if color.startswith('rgb'):
19
+ rgb_vals = color.strip('rgb()').split(',')
20
+ if len(rgb_vals) == 3:
21
+ try:
22
+ r, g, b = map(int, rgb_vals)
23
+ return f'#{r:02x}{g:02x}{b:02x}'.upper()
24
+ except ValueError:
25
+ raise ValueError("Invalid RGB color format")
26
+ color = color.strip().upper()
27
+ color = color.lstrip('#')
28
+ if len(color) != 6:
29
+ raise ValueError("Invalid hex color. Use '#RRGGBB' format")
30
+ try:
31
+ int(color, 16)
32
+ except ValueError:
33
+ raise ValueError("Invalid hexadecimal color")
34
+ return f'#{color}'
35
+
36
+ def remove_background_color(image, hex_color, tolerance):
37
+ """
38
+ Remove pixels of a specified hex color from an image.
39
+ """
40
+ if isinstance(image, np.ndarray):
41
+ image = Image.fromarray(image)
42
+ image = image.convert("RGBA")
43
+ try:
44
+ hex_color = sanitize_hex_color(hex_color)
45
+ target_color = tuple(int(hex_color.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
46
+ except ValueError as e:
47
+ raise ValueError(str(e))
48
+ data = np.array(image)
49
+ mask = np.all(np.abs(data[:,:,:3] - target_color) <= tolerance, axis=2)
50
+ alpha_channel = np.where(mask, 0, 255)
51
+ data[:, :, 3] = alpha_channel
52
+ return data
53
+
54
+
55
+ def color_remover_interface(image, hex_color, tolerance, _=None):
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 processed_image # Return only the processed image
65
+ except Exception as e:
66
+ gr.Warning(str(e))
67
+ return image # Return original image in case of error
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=40, step=1, label="Color Tolerance"),
76
+ gr.ColorPicker(label="Pick a Color") # Color picker remains
77
+ ],
78
+ outputs=[
79
+ gr.Image(type="numpy", label="Processed Image") # Removed output textbox
80
+ ],
81
+ title="Image Colour Remover",
82
+ description="""
83
+ Remove a specific colour from your image with precision!
84
+
85
+ ### How to Use:
86
+ 1. Upload an image
87
+ 2. Choose the colour to remove
88
+ 3. Adjust the colour tolerance (higher numbers will remove similair colours to hex code entered)
89
+ 4. See the magic happen!
90
+ """,
91
+ )
92
+
93
+ iface.launch(share=True) # Launch the interface with a shareable link