Introduction to dash-canvas

Note: dash-canvas is a legacy package. The recommended way to annotate images is to use the drawing tools of plotly figures.

dash-canvas is a module for image annotation and image processing using Dash. It provides both the DashCanvas object for drawing and annotations on images, and a set of utility functions to process images using the annotations.

dash-canvas can be used in various fields in which user interaction with images is required, such as quality control in industry, identification and segmentation of cells or organs in life and medical sciences, quantification of phases in materials and geosciences, construction of training sets for machine learning, etc.

Install dash-canvas with

The source is on GitHub at plotly/dash-canvas.

DashCanvas: a canvas object for annotations

Let’s get started with a simple canvas object.

This example has not been ported to Julia yet - showing the Python version instead.

Visit the old docs site for Julia at: https://community.plotly.com/c/dash/julia/20

from dash import Dash, html
from dash_canvas import DashCanvas

app = Dash()
app.config.suppress_callback_exceptions = True

app.layout = html.Div([
    html.H5('Press down the left mouse button and draw inside the canvas'),
    DashCanvas(id='canvas_101')
    ])


if __name__ == '__main__':
    app.run(debug=True)
Press down the left mouse button and draw inside the canvas

You can draw inside the object with the freehand tool, and use the tool buttons to draw lines, zoom in and out, pan, select objects and move them inside the canvas.

DashCanvas comes with a set of properties which can be adjusted to control the geometry of the canvas, the default tool and its properties. You can pass a background image either as a filename (filename property) or as a data string (image_content property); more examples below.

This example has not been ported to Julia yet - showing the Python version instead.

Visit the old docs site for Julia at: https://community.plotly.com/c/dash/julia/20

from dash import Dash, html
from dash_canvas import DashCanvas

app = Dash()

filename = 'https://raw.githubusercontent.com/plotly/datasets/master/mitochondria.jpg'
canvas_width = 500


app.layout = html.Div([
    DashCanvas(id='canvaas_image',
               tool='line',
               lineWidth=5,
               lineColor='red',
               filename=filename,
               width=canvas_width)
    ])


if __name__ == '__main__':
    app.run(debug=True)

The height of the canvas is adjusted automatically by keeping the aspect ratio of the background image.

Basic callbacks to modify DashCanvas properties

Like any Dash component, the properties of a DashCanvas can be modified by other components, via callbacks. Please be sure to have read about Basic Callbacks in the Dash Fundamentals.

This example has not been ported to Julia yet - showing the Python version instead.

Visit the old docs site for Julia at: https://community.plotly.com/c/dash/julia/20


from dash_canvas import DashCanvas
from dash import Dash, html, dcc, Input, Output, callback
import dash_daq as daq

filename = 'https://www.publicdomainpictures.net/pictures/60000/nahled/flower-outline-coloring-page.jpg'
canvas_width = 300

app = Dash()

app.layout = html.Div([
        html.Div([
            DashCanvas(
                id='canvaas-color',
                width=canvas_width,
                filename=filename,
                hide_buttons=['line', 'zoom', 'pan'],
                )
                ], className="six columns"),
        html.Div([
            html.H6(children=['Brush width']),
            dcc.Slider(
                id='bg-width-slider',
                min=2,
                max=40,
                step=1,
                value=5
            ),
            daq.ColorPicker(
                id='color-picker',
                label='Brush color',
                value=dict(hex='#119DFF')
            ),
        ], className="three columns"),
        ])


@callback(Output('canvaas-color', 'lineColor'), Input('color-picker', 'value'))
def update_canvas_linecolor(value):
    if isinstance(value, dict):
        return value['hex']
    else:
        return value


@callback(Output('canvaas-color', 'lineWidth'), Input('bg-width-slider', 'value'))
def update_canvas_linewidth(value):
    return value


if __name__ == '__main__':
    app.run(debug=True)
Brush width

In the example above, a slider dcc_slider and a color picker daq_colorpicker are used to adjust the width and color of the drawing brush. We just created an image coloring tool in a few lines of code! You can learn more about available components in the
component libraries
section of the Dash documentation. Also note that the set of available buttons has been restricted through thehide_buttons properties, in order to keep the app design simple.

Retrieving the geometry of annotations and using utility functions

The geometry of annotations can be retrieved by pressing the bottom-right button of the DashCanvas. This button is called “Save” by default; the name can be customized through the goButtonTitle property. This button updates the json_data property of DashCanvas, which is a JSON string with information about the background image and the geometry of annotations.

This example has not been ported to Julia yet - showing the Python version instead.

Visit the old docs site for Julia at: https://community.plotly.com/c/dash/julia/20

from dash import Dash, html, dash_table, Input, Output, callback
from dash.exceptions import PreventUpdate
from dash_canvas import DashCanvas
import json

app = Dash()

filename = 'https://raw.githubusercontent.com/plotly/datasets/master/mitochondria.jpg'
canvas_width = 500

columns = ['type', 'width', 'height', 'scaleX', 'strokeWidth', 'path']

app.layout = html.Div([
    html.H6('Draw on image and press Save to show annotations geometry'),
    DashCanvas(id='annot-canvas',
               lineWidth=5,
               filename=filename,
               width=canvas_width,
               ),
    dash_table.DataTable(id='canvaas-table',
              style_cell={'textAlign': 'left'},
              columns=[{"name": i, "id": i} for i in columns]),
    ])


@callback(Output('canvaas-table', 'data'),
              Input('annot-canvas', 'json_data'))
def update_data(string):
    if string:
        data = json.loads(string)
    else:
        raise PreventUpdate
    return data['objects'][1:]


if __name__ == '__main__':
    app.run(debug=True)
Draw on image and press Save to show annotations geometry

Updating the background image

The background image can be updated thanks to the image_content property (a ), for example using the contents property of dcc_upload (an “open file” dialog). Updating image_content triggers the update of the json_data property containing the annotations.

More examples

A gallery of examples using DashCanvas is available at plotly/canvas-portal.