如何在 Bokeh 中显示 TIFF 图像?

How do I display a TIFF image in Bokeh?

我可以将 TIFF 图像作为 NumPy 数组加载到内存中,其中每个像素由 3 元素 RGB 向量表示:

from PIL import Image
import numpy as np
arr=np.array(Image.open(imgfn))

例如,上面的 arr 可能具有形状 (2469,2858,3)。

遵循 Bokeh docs,在 Bokeh 中,像素是根据颜色图解释的一维数字。

如何将我的 3D RGB TIFF 数组映射到 1D Bokeh 颜色图索引数组,我应该使用什么颜色图?

This post 建议我应该写一个叫做 RGBAColorMapper 的东西。我该怎么做?

还有一个叫做 image_rgba 的 4D 像素,我如何将 3D 像素转换为 4D 以使用它?

基本上我正在寻找与 MatPlotLib imshow 相同的功能。

您可以使用 PIL 包将 tiff 图像转换为 rgba。然后可以直接用 image_rgba 绘制它。 tiff 文件是从 http://www-eng-x.llnl.gov/documents/tests/tiff.html, following the SO answer posted here.

下载的
import numpy

from PIL import Image
from bokeh.plotting import figure, show

im = Image.open('a_image.tif')
im = im.convert("RGBA")
# uncomment to compare
#im.show()
imarray = numpy.array(im)

p = figure(x_range=(0,10), y_range=(0,1), width=1000, height=200)

p.image_rgba(image=[imarray], x=0, y=0, dw=10, dh=1)

show(p)