Skip to content

0.18.0

Compare
Choose a tag to compare
@ianhi ianhi released this 22 Jul 19:37
· 78 commits to master since this release
a4829c9

User facing improvements:
#207 - Allow image segmenter to embedded in a GUI + allow specifying the buttons for the panning and for the lasso.

Embed image-segmenter in PySimpleGUI example
import numpy as np
import PySimpleGUI as sg

"""
    Embedding the Matplotlib toolbar into your application
"""

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure

from mpl_interactions import image_segmenter

# ------------------------------- PySimpleGUI CODE

layout = [
    [sg.T("Graph: y=sin(x)")],
    [sg.B("Toggle Erase Mode"), sg.B("Exit")],
    [sg.T("Controls:")],
    [sg.Canvas(key="controls_cv")],
    [sg.T("Figure:")],
    [
        sg.Column(
            layout=[
                [
                    sg.Canvas(
                        key="fig_cv",
                        # it's important that you set this size
                        size=(300, 800),
                    )
                ]
            ],
            background_color="#DAE0E6",
            pad=(0, 0),
        )
    ],
    [sg.B("Alive?")],
]

window = sg.Window("Graph with controls", layout)

# apply the layout so that the canvas exists for matplotlib to use.
window.finalize()

# Set up the Matplotlib Figure
DPI = 100
fig = Figure((200 * 2 / float(DPI), 800 / float(DPI)), dpi=DPI)
canvas = FigureCanvasTkAgg(fig, master=window["fig_cv"].TKCanvas)  # A tk.DrawingArea.

toolbar = NavigationToolbar2Tk(canvas, window["controls_cv"].TKCanvas)
toolbar.update()
fig.set_size_inches(404 * 2 / float(DPI), 404 / float(DPI))
canvas.draw()
canvas.get_tk_widget().pack(side="right", fill="both", expand=1)

# Set up matplotlib styling
# This won't change each loop
ax = fig.gca()
from imageio import imread

image = imread(
    "https://github.com/matplotlib/matplotlib/raw/v3.3.0/lib/matplotlib/mpl-data/sample_data/ada.png"
)
_ = ax.imshow(image)
segmenter = image_segmenter(image, ax=ax)


while True:
    event, values = window.read()
    print(event, values)
    if event in (sg.WIN_CLOSED, "Exit"):  # always,  always give a way out!
        break
    elif event == "Toggle Erase Mode":
        segmenter.erasing = not segmenter.erasing

window.close()
Thanks to @redeboer for several behind the scenes improvements:#203 #204