|
| 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') |
0 commit comments