如何在Bokeh(Python)中处理图像

Tre*_*ick 16 python image matplotlib bokeh

例如,您可以使用以下代码在matplotlib中绘制图像:

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('image.png')
plt.imshow(img)
Run Code Online (Sandbox Code Playgroud)

Bokeh(0.10)有可能是这样的吗?

Luk*_*van 19

您可以使用ImageURL字形(image_url绘图方法)在本地或从Web加载图像.

from bokeh.plotting import figure, show, output_file

output_file('image.html')

p = figure(x_range=(0,1), y_range=(0,1))
p.image_url(url=['tree.png'], x=0, y=1)
show(p)
Run Code Online (Sandbox Code Playgroud)

图像示例

一个问题 - 如果您只绘制图像(而不是其他数据),则必须明确设置绘图范围.

这是文档:

http://bokeh.pydata.org/en/latest/docs/reference/models/glyphs.html#bokeh.models.glyphs.ImageURL

  • 这个示例代码不再适用于0.12.5,我试图编辑示例代码但它被拒绝了两次,你需要使用以下对image_url的调用:`p.image_url(url = ['tree.png'],x = 0,y = 0,w = 1,h = 1,anchor ="bottom_left")` (5认同)
  • 该示例在使用散景服务器时不起作用。无法找到图像 (404) (2认同)

小智 9

早期的答案很有帮助.但是,我想要一个没有任何附加对象的图像选项.因此,添加Bokeh版本0.12.0的答案并删除所有网格,轴和工具栏.

from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource, Range1d

bosch_logo = "static/tree.jpg"
logo_src = ColumnDataSource(dict(url = [bosch_logo]))

page_logo = figure(plot_width = 500, plot_height = 500, title="")
page_logo.toolbar.logo = None
page_logo.toolbar_location = None
page_logo.x_range=Range1d(start=0, end=1)
page_logo.y_range=Range1d(start=0, end=1)
page_logo.xaxis.visible = None
page_logo.yaxis.visible = None
page_logo.xgrid.grid_line_color = None
page_logo.ygrid.grid_line_color = None
page_logo.image_url(url='url', x=0.05, y = 0.85, h=0.7, w=0.9, source=logo_src)
page_logo.outline_line_alpha = 0 
curdoc().add_root(page_logo)
Run Code Online (Sandbox Code Playgroud)

  • ``figure''是一个便利函数,它返回一个``bokeh.models.Plot''实例,并在其上设置了所有轴/网格/工具。或者,您可以实例化一个裸露的“ Plot”对象并将图像添加到该对象(避免所有的减法操作) (2认同)

Art*_*uro 6

另一种选择是在div中显示图像:

from bokeh.io import output_notebook, show
from bokeh.models.widgets import Div

output_notebook()
div_image = Div(text="""<img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/7.png" alt="div_image">""", width=150, height=150)
show(div_image)
Run Code Online (Sandbox Code Playgroud)

杰尼龟精灵

ImageURL 无法通过回调动态更新。但是,使用 a 时div,您可以将 视为div_image.text常规 Python 字符串,例如:

from ipywidgets import interact

from bokeh.io import output_notebook, show, push_notebook
from bokeh.models.widgets import Div

output_notebook()
div_image = Div(text="""<img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png" alt="div_image">""", width=100, height=100)

def update(pokemon_number=1):
    div_image.text = """<img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png" alt="div_image">""".format(pokemon_number)
    push_notebook()

show(div_image, notebook_handle=True)

interact(update, pokemon_number=[1, 4, 7])
Run Code Online (Sandbox Code Playgroud)

口袋妖怪选择器

当然,图片源也可以指向本地文件。

(在 Python 3.7.3 和 bokeh 1.2.0 中测试)


Kar*_*rik 5

使用散景服务运行这个例子有点棘手。我建议正确设置工作目录:

server_folder/
     +main.py
     +static/
         +logo.png
Run Code Online (Sandbox Code Playgroud)

.. 并从目录ABOVE server_folder运行 bokeh serve 命令

bokeh serve server_folder --show
Run Code Online (Sandbox Code Playgroud)

然后这段代码对我有用

#main.py file
from bokeh.plotting import figure, curdoc
x_range = (-20,-10) # could be anything - e.g.(0,1)
y_range = (20,30)
p = figure(x_range=x_range, y_range=y_range)
#img_path = 'https://docs.bokeh.org/en/latest/_static/images/logo.png'
img_path = 'server_folder/static/logo.png'
p.image_url(url=[img_path],x=x_range[0],y=y_range[1],w=x_range[1]-x_range[0],h=y_range[1]-y_range[0])
doc = curdoc()
doc.add_root(p)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明