我正在尝试使用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 我不确定我传递新图像数据到源的方式是否正确。 有什么建议吗?