从bokeh==2.1.1升级到bokeh==2.2.0会导致figure.image_rgba()出现问题

3
这段代码在使用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)

bokeh==2.2.0(以及后续版本)中运行完全相同的代码不会输出任何内容,也不会引发任何错误。
Bokeh的发布说明没有提到对image_rgba()的任何更改。
1个回答

6
错误在浏览器中的JS控制台(因为错误实际上发生在浏览器中,而不是“python侧”):
未处理的Promise拒绝:错误:期望一个2D数组
如果您查看np_img,您会发现它不是2D数组:
In [3]: np_img.shape
Out[3]: (297, 275, 4)

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

Bokeh期望一个二维的uint32数组,而不是一个三维的uint8数组,这一点一直都是如此,并在文档中有所体现。过去可能会无意中接受一个三维数组(这也就是为什么在发布说明中没有任何更改记录的原因),或者在NumPy或PIL转换为NumPy数组方面发生了一些变化。无论如何,你需要创建一个二维视图:

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接