Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/doc#986 - Document best practice on main script #1

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions src/main_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,49 @@
from io import BytesIO


path_upload = ""
original_image = None
image = None
fixed_image = None
fixed = False
advanced_properties = {"alpha_matting_foreground_threshold":240,
"alpha_matting_background_threshold":10,
"alpha_matting_erode_size":10}
def convert_image(img):
buf = BytesIO()
img.save(buf, format="PNG")
byte_im = buf.getvalue()
return byte_im


def upload_image(state):
state.image = Image.open(state.path_upload)
state.original_image = convert_image(state.image)
state.fixed = False
fix_image(state)


def fix_image(state, id=None, action=None):
state.fixed = False
notify(state, 'info', 'Removing the background...')
fixed_image = remove(state.image,
alpha_matting=True if action is not None else False, # Apply options when the button is clicked
alpha_matting_foreground_threshold=int(state.advanced_properties['alpha_matting_foreground_threshold']),
alpha_matting_background_threshold=int(state.advanced_properties['alpha_matting_background_threshold']),
alpha_matting_erode_size=int(state.advanced_properties['alpha_matting_erode_size']))

state.fixed_image = convert_image(fixed_image)
state.fixed = True
notify(state, 'success', 'Background removed successfully!')


def download_image(state):
download(state, content=state.fixed_image, name="fixed_img.png")


page = """<|toggle|theme|>
if __name__ == "__main__":
path_upload = ""
original_image = None
image = None
fixed_image = None
fixed = False
advanced_properties = {"alpha_matting_foreground_threshold":240,
"alpha_matting_background_threshold":10,
"alpha_matting_erode_size":10}

page = """<|toggle|theme|>
<page|layout|columns=265px 1fr|
<|sidebar|
### Removing **Background**{: .color-primary} from image
Expand Down Expand Up @@ -60,38 +92,6 @@
|images>
|>
|page>
"""

"""

def convert_image(img):
buf = BytesIO()
img.save(buf, format="PNG")
byte_im = buf.getvalue()
return byte_im

def upload_image(state):
state.image = Image.open(state.path_upload)
state.original_image = convert_image(state.image)
state.fixed = False
fix_image(state)

def fix_image(state, id=None, action=None):
state.fixed = False
notify(state, 'info', 'Removing the background...')
fixed_image = remove(state.image,
alpha_matting=True if action is not None else False, # Apply options when the button is clicked
alpha_matting_foreground_threshold=int(state.advanced_properties['alpha_matting_foreground_threshold']),
alpha_matting_background_threshold=int(state.advanced_properties['alpha_matting_background_threshold']),
alpha_matting_erode_size=int(state.advanced_properties['alpha_matting_erode_size']))

state.fixed_image = convert_image(fixed_image)
state.fixed = True
notify(state, 'success', 'Background removed successfully!')


def download_image(state):
download(state, content=state.fixed_image, name="fixed_img.png")


if __name__ == "__main__":
Gui(page=page).run(margin="0px", title='Background Remover')
91 changes: 44 additions & 47 deletions src/main_tgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,20 @@
import taipy.gui.builder as tgb


path_upload = ""
original_image = None
image = None
fixed_image = None
fixed = False
advanced_properties = {"alpha_matting_foreground_threshold":240,
"alpha_matting_background_threshold":10,
"alpha_matting_erode_size":10}



def convert_image(img):
buf = BytesIO()
img.save(buf, format="PNG")
byte_im = buf.getvalue()
return byte_im


def upload_image(state):
state.image = Image.open(state.path_upload)
state.original_image = convert_image(state.image)
state.fixed = False
fix_image(state)


def fix_image(state, id=None, action=None):
state.fixed = False
notify(state, 'info', 'Removing the background...')
Expand All @@ -46,44 +37,50 @@ def download_image(state):
download(state, content=state.fixed_image, name="fixed_img.png")



with tgb.Page() as page:
tgb.toggle(theme=True)

with tgb.layout("265px 1fr", columns__mobile="30 70"):
with tgb.part("sidebar"):
tgb.text("### Removing Background from image", mode="md")
tgb.file_selector("{path_upload}", extensions=".png,.jpg", label="Upload your image", on_action=upload_image, class_name="fullwidth")

with tgb.expandable(title="More options", expanded=False):
tgb.text("**Foreground threshold**", mode="md")
tgb.slider("{advanced_properties.alpha_matting_foreground_threshold}", max=500, label="Foreground threshold")
tgb.text("**Background threshold**", mode="md")
tgb.slider("{advanced_properties.alpha_matting_background_threshold}", max=50, label="Background threshold")
tgb.text("**Erosion size**", mode="md")
tgb.slider("{advanced_properties.alpha_matting_erode_size}", max=50, label="Erosion size")

tgb.button("Run with options", on_action=fix_image, class_name="plain fullwidth", active="{original_image}")

tgb.file_download("{None}", label="Download result", on_action=download_image, active="{fixed}")

with tgb.part("container"):
tgb.text("# Background Remover", mode="md")

tgb.text("""
if __name__ == "__main__":
path_upload = ""
original_image = None
image = None
fixed_image = None
fixed = False
advanced_properties = {"alpha_matting_foreground_threshold":240,
"alpha_matting_background_threshold":10,
"alpha_matting_erode_size":10}

with tgb.Page() as page:
tgb.toggle(theme=True)

with tgb.layout("265px 1fr", columns__mobile="30 70"):
with tgb.part("sidebar"):
tgb.text("### Removing Background from image", mode="md")
tgb.file_selector("{path_upload}", extensions=".png,.jpg", label="Upload your image", on_action=upload_image, class_name="fullwidth")

with tgb.expandable(title="More options", expanded=False):
tgb.text("**Foreground threshold**", mode="md")
tgb.slider("{advanced_properties.alpha_matting_foreground_threshold}", max=500, label="Foreground threshold")
tgb.text("**Background threshold**", mode="md")
tgb.slider("{advanced_properties.alpha_matting_background_threshold}", max=50, label="Background threshold")
tgb.text("**Erosion size**", mode="md")
tgb.slider("{advanced_properties.alpha_matting_erode_size}", max=50, label="Erosion size")

tgb.button("Run with options", on_action=fix_image, class_name="plain fullwidth", active="{original_image}")

tgb.file_download("{None}", label="Download result", on_action=download_image, active="{fixed}")

with tgb.part("container"):
tgb.text("# Background Remover", mode="md")

tgb.text("""
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.
This code is open source and accessible on [GitHub](https://github.com/Avaiga/demo-remove-background).
""", mode="md")
""", mode="md")

with tgb.layout("1 1"):
with tgb.part("card text-center", render="{original_image}"):
tgb.text("### Original Image 📷", mode="md")
tgb.image("{original_image}")
with tgb.part("card text-center", render="{fixed_image}"):
tgb.text("### Fixed Image 🔧", mode="md")
tgb.image("{fixed_image}")

with tgb.layout("1 1"):
with tgb.part("card text-center", render="{original_image}"):
tgb.text("### Original Image 📷", mode="md")
tgb.image("{original_image}")
with tgb.part("card text-center", render="{fixed_image}"):
tgb.text("### Fixed Image 🔧", mode="md")
tgb.image("{fixed_image}")


if __name__ == "__main__":
Gui(page=page).run(margin="0px", title='Background Remover')