☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

Get coordinates from image using Lasso Select Tool

Community Supportcustomjsserverbokeh
thisuser
thisuser

Hi,
I’m trying to get the coordinates of all the pixels inside an area of interest using the Lasso select tool from an numpy array image.
A sample code is below. How can I extract the coordinates of all the pixels that have been selected by the lasso tool? Is using a customJS the right way? If so, what data should I extract with the javascript? Thanks for any help!

from bokeh.server.server import Server
from bokeh.models import LassoSelectTool
from bokeh.plotting import figure
from bokeh.io import show
from bokeh.layouts import column

import numpy as np
from functools import partial


def make_document(doc, mask, img):
    def update(attr, old, new):
        print('something selected.')
        
    fig = figure(tools=[LassoSelectTool()])
    fig.select(LassoSelectTool).select_every_mousemove = False
    r = fig.image([img], 0, 0 ,1,1)
    r.data_source.selected.on_change('data', update)
    doc.add_root(column([fig]))

if __name__ == '__main__':
    image = np.random.rand(512, 512)
    server = Server({'/': partial(make_document, mask=image, img=image)})
    server.start()
    server.io_loop.add_callback(server.show, "/")
    server.io_loop.start()
⌁   Link to post
thisuser
thisuser

Thanks. Based on your suggestion, I tried to access the coordinates with Python, but there is no response upon selecting the area (no msg shown in the cmd termincal). Is there anything I missed? Please see the updated code below, thanks!

from bokeh.server.server import Server
from bokeh.models import LassoSelectTool
from bokeh.plotting import figure
from bokeh.layouts import column
from bokeh.events import SelectionGeometry

import numpy as np
from functools import partial

def make_document(doc, img):
    def test_event(event):
        print('geometry selected') # not shown in the cmd terminal
    
    fig = figure(tools=[LassoSelectTool()])
    fig.select(LassoSelectTool).select_every_mousemove = False
    p = fig.image([img], 0, 0, 1, 1)
    p.on_event(SelectionGeometry, test_event)
    doc.add_root(column([fig]))

if __name__ == '__main__':
    image = np.random.rand(128 , 128)
    server = Server({'/': partial(make_document, img=image)})
    server.start()
    server.io_loop.add_callback(server.show, "/")
    server.io_loop.start()
⌁   Link to post

End of archived discussion · Replies are disabled