10
10
image = None
11
11
fixed_image = None
12
12
fixed = False
13
- advanced_properties = {"alpha_matting_foreground_threshold" :240 ,
14
- "alpha_matting_background_threshold" :10 ,
15
- "alpha_matting_erode_size" :10 }
16
-
13
+ advanced_properties = {
14
+ "alpha_matting_foreground_threshold" : 240 ,
15
+ "alpha_matting_background_threshold" : 10 ,
16
+ "alpha_matting_erode_size" : 10 ,
17
+ }
17
18
18
19
19
20
def convert_image (img ):
@@ -22,59 +23,104 @@ def convert_image(img):
22
23
byte_im = buf .getvalue ()
23
24
return byte_im
24
25
26
+
25
27
def upload_image (state ):
26
28
state .image = Image .open (state .path_upload )
27
29
state .original_image = convert_image (state .image )
28
30
state .fixed = False
29
31
fix_image (state )
30
32
33
+
31
34
def fix_image (state , id = None , action = None ):
32
35
state .fixed = False
33
- notify (state , 'info' , 'Removing the background...' )
34
- fixed_image = remove (state .image ,
35
- alpha_matting = True if action is not None else False , # Apply options when the button is clicked
36
- alpha_matting_foreground_threshold = int (state .advanced_properties ['alpha_matting_foreground_threshold' ]),
37
- alpha_matting_background_threshold = int (state .advanced_properties ['alpha_matting_background_threshold' ]),
38
- alpha_matting_erode_size = int (state .advanced_properties ['alpha_matting_erode_size' ]))
36
+ notify (state , "info" , "Removing the background..." )
37
+ fixed_image = remove (
38
+ state .image ,
39
+ alpha_matting = (
40
+ True if action is not None else False
41
+ ), # Apply options when the button is clicked
42
+ alpha_matting_foreground_threshold = int (
43
+ state .advanced_properties ["alpha_matting_foreground_threshold" ]
44
+ ),
45
+ alpha_matting_background_threshold = int (
46
+ state .advanced_properties ["alpha_matting_background_threshold" ]
47
+ ),
48
+ alpha_matting_erode_size = int (
49
+ state .advanced_properties ["alpha_matting_erode_size" ]
50
+ ),
51
+ )
39
52
40
53
state .fixed_image = convert_image (fixed_image )
41
54
state .fixed = True
42
- notify (state , ' success' , ' Background removed successfully!' )
55
+ notify (state , " success" , " Background removed successfully!" )
43
56
44
57
45
58
def download_image (state ):
46
59
download (state , content = state .fixed_image , name = "fixed_img.png" )
47
60
48
61
49
-
50
62
with tgb .Page () as page :
51
63
tgb .toggle (theme = True )
52
64
53
- with tgb .layout ("265px 1fr " , columns__mobile = "30 70 " ):
65
+ with tgb .layout ("20 80 " , columns__mobile = "1 " ):
54
66
with tgb .part ("sidebar" ):
55
- tgb .text ("### Removing Background from image" , mode = "md" )
56
- tgb .file_selector ("{path_upload}" , extensions = ".png,.jpg" , label = "Upload your image" , on_action = upload_image , class_name = "fullwidth" )
67
+ tgb .text ("### Removing **Background** from image" , mode = "md" )
68
+ tgb .file_selector (
69
+ "{path_upload}" ,
70
+ extensions = ".png,.jpg" ,
71
+ label = "Upload image" ,
72
+ on_action = upload_image ,
73
+ class_name = "fullwidth" ,
74
+ )
57
75
58
76
with tgb .expandable (title = "More options" , expanded = False ):
59
77
tgb .text ("**Foreground threshold**" , mode = "md" )
60
- tgb .slider ("{advanced_properties.alpha_matting_foreground_threshold}" , max = 500 , label = "Foreground threshold" )
78
+ tgb .slider (
79
+ "{advanced_properties.alpha_matting_foreground_threshold}" ,
80
+ max = 500 ,
81
+ label = "Foreground threshold" ,
82
+ width = "100%" ,
83
+ )
61
84
tgb .text ("**Background threshold**" , mode = "md" )
62
- tgb .slider ("{advanced_properties.alpha_matting_background_threshold}" , max = 50 , label = "Background threshold" )
85
+ tgb .slider (
86
+ "{advanced_properties.alpha_matting_background_threshold}" ,
87
+ max = 50 ,
88
+ label = "Background threshold" ,
89
+ width = "100%" ,
90
+ )
63
91
tgb .text ("**Erosion size**" , mode = "md" )
64
- tgb .slider ("{advanced_properties.alpha_matting_erode_size}" , max = 50 , label = "Erosion size" )
65
-
66
- tgb .button ("Run with options" , on_action = fix_image , class_name = "plain fullwidth" , active = "{original_image}" )
67
-
68
- tgb .file_download ("{None}" , label = "Download result" , on_action = download_image , active = "{fixed}" )
92
+ tgb .slider (
93
+ "{advanced_properties.alpha_matting_erode_size}" ,
94
+ max = 50 ,
95
+ label = "Erosion size" ,
96
+ width = "100%" ,
97
+ )
98
+
99
+ tgb .button (
100
+ "Run with options" ,
101
+ on_action = fix_image ,
102
+ class_name = "plain fullwidth" ,
103
+ active = "{original_image}" ,
104
+ )
105
+
106
+ tgb .file_download (
107
+ "{None}" ,
108
+ label = "Download result" ,
109
+ on_action = download_image ,
110
+ active = "{fixed}" ,
111
+ class_name = "fullwidth" ,
112
+ )
69
113
70
114
with tgb .part ("container" ):
71
- tgb .text ("# Background Remover" , mode = "md" )
115
+ tgb .text ("# Background ** Remover** " , mode = "md" )
72
116
73
- tgb .text ("""
117
+ tgb .text (
118
+ """
74
119
Give it a try by uploading an image to witness the seamless removal of the background. You can download images in full quality from the sidebar.
75
120
This code is open source and accessible on [GitHub](https://github.com/Avaiga/demo-remove-background).
76
- """ , mode = "md" )
77
-
121
+ """ ,
122
+ mode = "md" ,
123
+ )
78
124
79
125
with tgb .layout ("1 1" ):
80
126
with tgb .part ("card text-center" , render = "{original_image}" ):
@@ -86,4 +132,4 @@ def download_image(state):
86
132
87
133
88
134
if __name__ == "__main__" :
89
- Gui (page = page ).run (margin = "0px" , title = ' Background Remover' )
135
+ Gui (page = page ).run (margin = "0px" , title = " Background Remover" )
0 commit comments