繁体   English   中英

我如何在散景 (Python) 中处理图像

[英]How do I work with images in Bokeh (Python)

例如,您可以使用以下代码 plot matplotlib 中的图像:

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

Bokeh(0.10) 可以实现这样的功能吗?

您可以使用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, 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

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

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

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)

在此处输入图片说明

另一种选择是在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 无法通过回调动态更新 但是,使用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 和 bokeh 1.2.0 中测试)

这是一个有效的简单示例,几乎与您请求的简单格式相同:

from PIL import Image
import numpy as np 
from bokeh.plotting import figure, output_notebook, show
output_notebook()

#load image
im = Image.open('Segment_image.png') # just replace any image that you want here


p = figure()
imarray = np.array(im.convert("RGBA"))

plotted_image = p.image_rgba(image=[imarray.view("uint32").reshape(imarray.shape[:2])], x=0, y=0, dw=imarray.shape[0], dh=imarray.shape[1])
show(p)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM