Skip to content

Commit a5fa8b2

Browse files
committed
Update to 3.1
1 parent 5d963da commit a5fa8b2

6 files changed

+191
-80
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
## How to run
2727

28-
This demo works with a Python version superior to 3.8. Install the dependencies of the *requirements.txt* and run the *main.py*.
28+
This demo works with a Python version superior to 3.8. Install the dependencies of the *requirements.txt* and run the *main_markdown.py* or *main_tgb.py*.
2929

3030

3131
## Directory Structure
@@ -52,12 +52,12 @@ specific language governing permissions and limitations under the License.
5252

5353
## Installation
5454

55-
Want to install _demo sales dashboard_? Check out our [`INSTALLATION.md`](INSTALLATION.md) file.
55+
Want to install _demo remove background_? Check out our [`INSTALLATION.md`](INSTALLATION.md) file.
5656

5757
## Contributing
5858

59-
Want to help build _demo sales dashboard_? Check out our [`CONTRIBUTING.md`](CONTRIBUTING.md) file.
59+
Want to help build _demo remove background_? Check out our [`CONTRIBUTING.md`](CONTRIBUTING.md) file.
6060

6161
## Code of conduct
6262

63-
Want to be part of the _demo sales dashboard_ community? Check out our [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md) file.
63+
Want to be part of the _demo remove background_ community? Check out our [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md) file.

src/fixed_img.png

4.43 MB
Loading

src/main.py

-75
This file was deleted.

src/main_markdown.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from taipy.gui import Gui, notify, download
2+
from rembg import remove
3+
from PIL import Image
4+
from io import BytesIO
5+
6+
7+
path_upload = ""
8+
original_image = None
9+
image = None
10+
fixed_image = None
11+
fixed = False
12+
advanced_properties = {"alpha_matting_foreground_threshold":240,
13+
"alpha_matting_background_threshold":10,
14+
"alpha_matting_erode_size":10}
15+
16+
17+
page = """<|toggle|theme|>
18+
<page|layout|columns=265px 1fr|
19+
<|sidebar|
20+
### Removing **Background**{: .color-primary} from image
21+
22+
<|{path_upload}|file_selector|extensions=.png,.jpg|label=Upload your image|on_action=upload_image|class_name=fullwidth|>
23+
24+
<|More options|expandable|not expanded|
25+
**Foreground threshold**
26+
27+
<|{advanced_properties.alpha_matting_foreground_threshold}|slider|max=500|>
28+
29+
**Background threshold**
30+
31+
<|{advanced_properties.alpha_matting_background_threshold}|slider|max=50|>
32+
33+
**Erosion size**
34+
<|{advanced_properties.alpha_matting_erode_size}|slider|max=50|>
35+
36+
<|Run with options|button|on_action=fix_image|class_name=plain fullwidth|active={original_image}|>
37+
|>
38+
39+
---
40+
41+
<|{None}|file_download|label=Download result|on_action=download_image|active={fixed}|>
42+
|>
43+
44+
<|container|
45+
# Background **Remover**{: .color-primary}
46+
47+
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.
48+
This code is open source and accessible on [GitHub](https://github.com/Avaiga/demo-remove-background).
49+
50+
<images|layout|columns=1 1|
51+
<col1|card text-center|part|render={original_image}|
52+
### Original Image 📷
53+
<|{original_image}|image|>
54+
|col1>
55+
56+
<col2|card text-center|part|render={fixed}|
57+
### Fixed Image 🔧
58+
<|{fixed_image}|image|>
59+
|col2>
60+
|images>
61+
|>
62+
|page>
63+
"""
64+
65+
66+
def convert_image(img):
67+
buf = BytesIO()
68+
img.save(buf, format="PNG")
69+
byte_im = buf.getvalue()
70+
return byte_im
71+
72+
def upload_image(state):
73+
state.image = Image.open(state.path_upload)
74+
state.original_image = convert_image(state.image)
75+
state.fixed = False
76+
fix_image(state)
77+
78+
def fix_image(state, id=None, action=None):
79+
state.fixed = False
80+
notify(state, 'info', 'Removing the background...')
81+
fixed_image = remove(state.image,
82+
alpha_matting=True if action is not None else False, # Apply options when the button is clicked
83+
alpha_matting_foreground_threshold=int(state.advanced_properties['alpha_matting_foreground_threshold']),
84+
alpha_matting_background_threshold=int(state.advanced_properties['alpha_matting_background_threshold']),
85+
alpha_matting_erode_size=int(state.advanced_properties['alpha_matting_erode_size']))
86+
87+
state.fixed_image = convert_image(fixed_image)
88+
state.fixed = True
89+
notify(state, 'success', 'Background removed successfully!')
90+
91+
92+
def download_image(state):
93+
download(state, content=state.fixed_image, name="fixed_img.png")
94+
95+
96+
if __name__ == "__main__":
97+
Gui(page=page).run(margin="0px", title='Background Remover')

src/main_tgb.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from taipy.gui import Gui, notify, download
2+
from rembg import remove
3+
from PIL import Image
4+
from io import BytesIO
5+
import taipy.gui.builder as tgb
6+
7+
8+
path_upload = ""
9+
original_image = None
10+
image = None
11+
fixed_image = None
12+
fixed = False
13+
advanced_properties = {"alpha_matting_foreground_threshold":240,
14+
"alpha_matting_background_threshold":10,
15+
"alpha_matting_erode_size":10}
16+
17+
18+
19+
def convert_image(img):
20+
buf = BytesIO()
21+
img.save(buf, format="PNG")
22+
byte_im = buf.getvalue()
23+
return byte_im
24+
25+
def upload_image(state):
26+
state.image = Image.open(state.path_upload)
27+
state.original_image = convert_image(state.image)
28+
state.fixed = False
29+
fix_image(state)
30+
31+
def fix_image(state, id=None, action=None):
32+
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']))
39+
40+
state.fixed_image = convert_image(fixed_image)
41+
state.fixed = True
42+
notify(state, 'success', 'Background removed successfully!')
43+
44+
45+
def download_image(state):
46+
download(state, content=state.fixed_image, name="fixed_img.png")
47+
48+
49+
50+
with tgb.Page() as page:
51+
tgb.toggle(theme=True)
52+
53+
with tgb.layout("265px 1fr", columns__mobile="30 70"):
54+
with tgb.part("sidebar"):
55+
tgb.text("Removing Background from image")
56+
tgb.file_selector("{path_upload}", extensions=".png,.jpg", label="Upload your image", on_action=upload_image, class_name="fullwidth")
57+
58+
with tgb.expandable(title="More options", expanded=False):
59+
tgb.text("Foreground threshold")
60+
tgb.slider("{advanced_properties.alpha_matting_foreground_threshold}", max=500, label="Foreground threshold")
61+
tgb.text("Background threshold")
62+
tgb.slider("{advanced_properties.alpha_matting_background_threshold}", max=50, label="Background threshold")
63+
tgb.text("Erosion size")
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}")
69+
70+
with tgb.part("container"):
71+
tgb.text("Background Remover", class_name="h1")
72+
73+
tgb.text("""
74+
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+
This code is open source and accessible on [GitHub](https://github.com/Avaiga/demo-remove-background).
76+
""", mode="md")
77+
78+
79+
with tgb.layout("1 1"):
80+
with tgb.part("card text-center", render="{original_image}"):
81+
tgb.text("Original Image 📷")
82+
tgb.image("{original_image}")
83+
with tgb.part("card text-center", render="{fixed_image}"):
84+
tgb.text("Fixed Image 🔧")
85+
tgb.image("{fixed_image}")
86+
87+
88+
if __name__ == "__main__":
89+
Gui(page=page).run(margin="0px", title='Background Remover')

src/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
taipy==3.0.0.dev3
1+
taipy==3.1
22
rembg
33
pillow

0 commit comments

Comments
 (0)