如何在Bokeh中显示TIFF图像?

问题描述 投票:0回答:1

我可以将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中,像素是相对于颜色图解释的1D数字。

如何将3D RGB TIFF阵列映射到1D Bokeh色彩映射索引数组,以及我应该使用什么色彩映射?

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

还有一些叫做image_rgba的东西是4D像素,我怎样才能将3D像素转换为4D才能使用它?

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

image rgb bokeh imshow
1个回答
5
投票

您可以使用PIL包将tiff图像转换为rgba。然后用image_rgba直接绘制它。 tiff文件是从http://www-eng-x.llnl.gov/documents/tests/tiff.html下载的,根据SO答案发布了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)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.