简体   繁体   English

从 bokeh==2.1.1 升级到 bokeh==2.2.0 打破了 figure.image_rgba()

[英]Upgrading from bokeh==2.1.1 to bokeh==2.2.0 breaks figure.image_rgba()

This snippet of code renders an image (albeit upside-down) when using bokeh==2.1.1 :这段代码在使用bokeh==2.1.1时呈现图像(尽管颠倒):

from PIL import Image
import numpy as np
import requests

from bokeh.plotting import figure
from bokeh.io import output_notebook
from bokeh.plotting import show
output_notebook()

response = requests.get('https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg', stream=True)
rgba_img = Image.open(response.raw).convert("RGBA")
numpy_img = np.array(rgba_img)

fig = figure()
plotted_image = fig.image_rgba(image=[np_img], x=50, y=50, dw=50, dh=50)
show(fig)

Running the exact same code in bokeh==2.2.0 (as well as later versions) outputs nothing, and doesn't raise any errors.bokeh==2.2.0 (以及更高版本)中运行完全相同的代码不会输出任何内容,也不会引发任何错误。

bokeh's release notes does not mention any changes to image_rgba() bokeh 的发行说明没有提到image_rgba()任何更改

The error is in the JS console in the browser (because the error actually occurs in the browser, not on the "python side"):错误是在浏览器的 JS 控制台中(因为错误实际上发生在浏览器中,而不是在“python 端”):

Unhandled Promise Rejection: Error: expected a 2D array未处理的承诺拒绝:错误:需要二维数组

And if you look at np_img , you can see it is not a 2D array:如果您查看np_img ,您会发现它不是一个二维数组:

In [3]: np_img.shape
Out[3]: (297, 275, 4)

In [4]: np_img.dtype
Out[4]: dtype('uint8')

Bokeh expects a 2D array of uint32, not a 3D array of uint8, and this has always been the case, and what has been demonstrated in the docs. Bokeh 期望 uint32 的 2D 数组,而不是 uint8 的 3D 数组,情况一直如此,并且文档中已经证明了这一点。 It's possible this a 3D array was accepted accidentally or unintentionally in the past (which is why no change would be noted in the release notes), or it's possible that something has changed on either the NumPy or PIL side with conversions to NumPy arrays.有可能在过去意外或无意地接受了 3D 数组(这就是为什么在发行说明中不会注意到任何变化的原因),或者可能在 NumPy 或 PIL 方面发生了一些变化,转换为 NumPy 数组。 Regardless, you will need to create a 2D view:无论如何,您将需要创建一个 2D 视图:

np_img = np.array(rgba_img)

np_img2d = np_img.view("uint32").reshape(np_img.shape[:2])

fig = figure()
plotted_image = fig.image_rgba(image=[np_img2d], x=50, y=50, dw=50, dh=50)

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

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