相关疑难解决方法(0)

如何使用matplotlib色彩映射将Numpy数组转换为PIL图像

我有一个简单的问题,但无法找到一个好的解决方案.

我想拍摄一个代表灰度图像的numpy 2D数组,并在应用一些matplotlib色彩图时将其转换为RGB PIL图像.

我可以使用以下pyplot.figure.figimage命令获得合理的PNG输出:

dpi = 100.0
w, h = myarray.shape[1]/dpi, myarray.shape[0]/dpi
fig = plt.figure(figsize=(w,h), dpi=dpi)
fig.figimage(sub, cmap=cm.gist_earth)
plt.savefig('out.png')
Run Code Online (Sandbox Code Playgroud)

虽然我可以调整它以获得我想要的东西(可能使用StringIO来获取PIL图像),我想知道是否有更简单的方法来做到这一点,因为它似乎是一个非常自然的图像可视化问题.让我们说,像这样:

colored_PIL_image = magic_function(array, cmap)
Run Code Online (Sandbox Code Playgroud)

谢谢阅读!

python numpy matplotlib color-mapping python-imaging-library

108
推荐指数
3
解决办法
16万
查看次数

如何使用PIL读取原始图像?

我有一个原始图像,其中每个像素对应一个16位无符号整数.我试图使用PIL Image.fromstring()函数读取,如下面的代码:

if __name__ == "__main__":
    if (len(sys.argv) != 4):
        print 'Error: missing input argument'
        sys.exit()

    file = open(sys.argv[1], 'rb')
    rawData = file.read()
    file.close()

    imgSize = (int(sys.argv[2]), int(sys.argv[3]))

    # Use the PIL raw decoder to read the data.
    #   - the 'F;16' informs the raw decoder that we are reading a little endian, unsigned integer 16 bit data.
    img = Image.fromstring('L', imgSize, rawData, 'raw', 'F;16')

    im.save('out.png')
Run Code Online (Sandbox Code Playgroud)

PIL文档通知fromstring()函数的第一个参数是'mode'.然而,看文档和谷歌搜索,我无法找到有关该论点真正意义的细节(我相信它与色彩空间或类似的东西有关).有谁知道我在哪里可以找到关于fromstring()函数的更详细的参考以及mode参数的含义?

python image image-processing python-imaging-library

30
推荐指数
4
解决办法
6万
查看次数