如何在Bokeh中显示TIFF图像?

3
我可以将TIFF图像加载到内存中作为NumPy数组,其中每个像素由一个3元素RGB向量表示:
from PIL import Image
import numpy as np
arr=np.array(Image.open(imgfn))

例如,上面的arr可能具有形状(2469,2858,3)。
按照Bokeh文档,在Bokeh中,像素是一维数字,其解释与颜色映射相关。
我如何将我的3D RGB TIFF数组映射到1D Bokeh颜色映射索引数组中?应该使用什么颜色映射?
该帖子建议我编写一个名为RGBAColorMapper的内容。我该如何实现?
还有一些称为image_rgba的东西,它是一个4D像素,我怎样才能将3D像素转换为4D以使用它?
基本上,我正在寻找与MatPlotLib imshow相同的功能。

1
我对bokeh一无所知,但RGBA(4d)图像只是一个3d RGB图像,其中另一个通道代表alpha(不透明度/透明度),因此您可以添加一个额外的通道,其中所有值都为255,表示“完全不透明”。 - Mark Setchell
答案应该是一个使用Bokeh显示加载到Y行X列3个RGB Numpy数组的TIFF图像的Python脚本。这就是我用我的50个奖励点支付的。 - Lars Ericson
2个回答

6
您可以使用PIL软件包将tiff图像转换为rgba格式。然后,使用image_rgba函数轻松绘制此图像。该tiff文件可从http://www-eng-x.llnl.gov/documents/tests/tiff.html下载,遵循此处发布的SO答案。
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)

enter image description here


1

我不确定之前的解决方案是否有效,因为tiff文件已经不存在了,所以我无法测试它。无论如何,对于任何其他图像,请执行以下操作:

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
imarray = np.array(im.convert("RGBA"))

p = figure()
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)

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