python - draw rgb spectrum in python/numpy
问题描述
I'm new to Python and i need to draw a RGB spectrum as a numpy array. For me it's clear that i need to rise the RGB values across the dimensions to get the spectrum.
import numpy as np
import matplotlib.pyplot as plt
spectrum = np.zeros([255,255, 3], dtype=np.unit8) #init the array
#fill the array with rgb values to create the spectrum without the use of loops
plt.imshow(spectrum)
plt.axis('off') # don't show axis
plt.show()
Is there a possiblity (e.g. a python or numpy method) to create the spectrum without the use of loops?
解决方案
Not sure if this is the result you'd like, but you could define the arrays for the RGB values yourself (see HSV-RGB comparison). I've used Pillow to convert grayscale to colour.
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
spectrum = np.zeros([256,256*6, 3], dtype=np.uint8) # init the array
# fill the array with rgb values to create the spectrum without the use of loops
spectrum[:,:,0] = np.concatenate(([255]*256, np.linspace(255,0,256), [0]*256, [0]*256, np.linspace(0,255,256), [255]*256), axis=0)
spectrum[:,:,1] = np.concatenate((np.linspace(0,255,256), [255]*256, [255]*256, np.linspace(255,0,256), [0]*256,[0]*256), axis=0)
spectrum[:,:,2] = np.concatenate(([0]*256, [0]*256,np.linspace(0,255,256),[255]*256, [255]*256, np.linspace(255,0,256)), axis=0)
img = Image.fromarray(spectrum, 'RGB')
img.show()
推荐阅读
- elasticsearch - 如果带有 _primary_shard 首选项的 GET 请求落在只有副本分片的节点上怎么办?
- python - 访问不同 django 模型的类中的对象
- c++ - 第 924 行:字符 9:运行时错误:引用绑定到“int”类型的空指针 (stl_vector.h)
- c - 使用 Clang 编译 MPI,但使用不同编译器的包装器
- django - Django(查找并替换所有) - 选择是:“配置”。django.core ....配置不当:“adolim.apps”不包含“AdolimConfig”类
- python - 如何使用滑块将交互式文本添加到 Plotly 条形图?
- ios - TableView 显示 Documents 文件夹中的文件
- python-3.x - 如何为单独的 python 脚本中保存的函数导入模块?
- java - How Can I Add TableView1 items to TableView2?
- ios - 为什么没有调用我的 UIButton 目标操作?