【问题标题】:How to extract rgb values of this colorbar image in python?如何在 python 中提取此颜色条图像的 rgb 值?
【发布时间】:2022-11-05 01:38:58
【问题描述】:

Image

我想制作一个在附加图像颜色栏中使用的颜色图。到目前为止,我尝试了下面给出的代码,但没有得到我想要的结果。

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np

img = plt.imread('Climat.png')
colors_from_img = img[:, 0, :]
my_cmap = LinearSegmentedColormap.from_list('my_cmap', colors_from_img, N=651)
y = random_sample((100, 100))
imshow(y, cmap=my_cmap);plt.colorbar()

寻找您的建议。先感谢您。

【问题讨论】:

  • 我的猜测是左边框是完全白色的,所以当你做colors_from_img = img[:, 0, :] 时,你得到的所有颜色都是白色的。尝试在 x 方向上移动到图像的中间,例如colors_from_img = img[:, 30, :]

标签: python matplotlib rgb colormap


【解决方案1】:

bicarlsen 给了你正确的方向。将提取颜色的点限制为彩色矩形:

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np

img = plt.imread('Climat.png')
colors_from_img = img[80::88, 30, :]
my_cmap = LinearSegmentedColormap.from_list('my_cmap', colors_from_img[::-1], N=len(colors_from_img))
y = np.random.random_sample((100, 100))
plt.imshow(y, cmap=my_cmap)
plt.colorbar()
plt.show()

样本输出:

P.S.:最初,我想到了一种更通用的方法

colors_from_img = np.unique(img[:, 30, :], axis=0)

是可能的,但是随着输入图像被光栅化,在黑线分隔彩色矩形的地方会出现各种混合颜色。

【讨论】:

    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 2011-02-19
    • 1970-01-01
    相关资源
    最近更新 更多