python - 如何在 python 中从 numpy 数组制作图像?

标签 python image numpy matplotlib plotly

我想将每个 numpy 数组映射到一种颜色以创建图像。 例如:如果我有 numpy 数组:

[ [0 0 1 3] 
[0 2 4 5]
[1 2 3 6] ]

我想通过将所有低于 3 的值映射为蓝色来制作图像

[ [blue blue sky-blue green]
[blue sky-blue green green] 
[blue sky-blue green green]

最佳答案

您可以制作双色彩色图。然后根据您的条件创建一个包含 1 或 0 的数组,并将两者传递给 pyplot.imshow():

import numpy as np

from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap

# white and blue
color = ListedColormap([(1,1,1), (0,0,1)])

a = np.array([
    [0, 0, 1, 3], 
    [0, 2, 4, 5],
    [1, 2, 3, 6] 
])

plt.axis('off')
plt.imshow(a < 3, cmap=color)

enter image description here

关于python - 如何在 python 中从 numpy 数组制作图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72021614/

相关文章:

python - 检查点是否在直线上的函数

python - 无法导入 Python 统计库

html - 仅在悬停图像时显示菜单

python - scikit 混淆矩阵中标量变量的索引无效

python - 是否有一个Python函数(最好是seaborn)可以帮助我用散点图上的一条线连接两组点?

python - 导入tensorflow_federated时出现类型错误

python - 我如何 json_normalize() df 中的特定字段并保留其他列?

c# - 在 C# 中创建图像

image - OpenCV中的Concat图像

python - 加速特定矩阵和向量大小的 einsum