我試圖用Bokeh顯示一系列圖像,並希望以交互方式滑動或播放序列。當我運行我的腳本時,它顯示第一個圖像,但是當我拖動滑塊或單擊播放按鈕時圖像未更新。我的代碼如下:在散景中用滑塊瀏覽圖像序列
import numpy as np
import bokeh.io
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import ColumnDataSource, HoverTool, SingleIntervalTicker, Slider, Button, Label
from bokeh.palettes import Spectral1
from skimage.external import tifffile as T
img=T.imread('C:/Users/UserXX/Desktop/Image_Sequence.tif')
sources={}
frames=list(range(0,img.shape[0]))
for frame in frames:
sources[frame]=ColumnDataSource(data=dict(image=[img[frame,:,:]]))
source1 = sources[0]
p_img = figure(plot_width=694, plot_height=520, x_range=[0,1388], y_range=[0, 1040])
label = Label(x=1.1, y=18, text=str(frames[0]), text_font_size='70pt', text_color='#eeeeee')
p_img.add_layout(label)
p_img.image(image='image', x=[0], y=[0], dw=[1388], dh=[1040],source=source1, palette="Spectral11")
slider = Slider(start=frames[0], end=frames[-1], value=frames[0],step=1, title="Frame")
def animate_update():
frame = slider.value + 1
if frame > frames[-1]:
frame = frames[0]
slider.value = frame
def slider_update(attr, old, new):
global source
global sources
frame = slider.value
label.text = str(frame)
source= sources[frame]
slider.on_change('value', slider_update)
def animate():
if button.label == '► Play':
button.label = '❚❚ Pause'
curdoc().add_periodic_callback(animate_update, 200)
else:
button.label = '► Play'
curdoc().remove_periodic_callback(animate_update)
button = Button(label='► Play', width=60)
button.on_click(animate)
l = layout([[p_img],[slider, button],], sizing_mode='scale_width')
curdoc().add_root(l)
curdoc().title = "Image_Sequence"
我用這作爲一個例子: https://github.com/bokeh/bokeh/blob/master/examples/app/gapminder/main.py 我不知道如果我是新的圖像數據傳遞到源的方式是否正確。 有什麼建議嗎?
嘗試用source1.data = sources [frame] .data替換source = sources [frame]。由於該圖的來源爲source1,因此您需要更改source1的關聯數據。否則,移動滑塊只是定義源而不是source1,它正在繪製。如果這不起作用嘗試複製你的例子。 – Anthonydouc