【问题标题】:How do I work with images in Bokeh (Python)如何在 Bokeh (Python) 中处理图像
【发布时间】:2016-04-11 07:39:45
【问题描述】:

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

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('image.png')
plt.imshow(img)

Bokeh(0.10) 可以实现这样的效果吗?

【问题讨论】:

  • 不是直接这样。 Bokeh 有Image(标量数据,颜色映射)、ImageRGBA(原始 RGBA 数据)和ImageURL(通过网络加载的图像)。这将在 GitHub 问题跟踪器上提出很好的功能请求。

标签: python image matplotlib bokeh


【解决方案1】:

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

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, w=0.8, h=0.6)
## could also leave out keywords
# p.image_url(['tree.png'], 0, 1, 0.8, h=0.6)  
show(p)

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

这是文档:

http://docs.bokeh.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")
  • 该示例在使用散景服务器时不起作用。找不到图片 (404)
【解决方案2】:

较早的答案很有帮助。但是,我想要一个没有任何附加对象的仅图像选项。因此,添加 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)

【讨论】:

  • figure 是一个方便的函数,它返回一个 bokeh.models.Plot 实例,其中设置了所有轴/网格/工具。或者,您可以实例化一个裸 Plot 对象并将图像添加到该对象(避免所有减法)
  • @Luke 感谢您的评论,您是否可以分享一个工作代码。
【解决方案3】:

另一种选择是在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)

ImageURL can't get updated dynamically with a callback。但是,使用 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])

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

(在 Python 3.7.3 和散景 1.2.0 中测试)

【讨论】:

  • 我正在尝试使用本地图像来遵循此答案,但 Bokeh 始终显示替代文本。我哪里错了? """&lt;img src="/Users/me/Documents/Projects/Another Project/ABCD/ImageViewer/date_time_images/20201102/024744.png" alt="i_broken"&gt;"""
【解决方案4】:

使用 bokeh serve 运行这个示例有点棘手。我建议正确设置工作目录:

server_folder/
     +main.py
     +static/
         +logo.png

.. 并从目录 ABOVE server_folder

运行 bokeh serve 命令
bokeh serve server_folder --show

那么这段代码对我有用

#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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-16
    • 2011-08-30
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2014-06-24
    相关资源
    最近更新 更多