TL; DR:您只需阅读"更新"部分.
如何
numpy.random.random((256, 256))将色彩映射输出到qt场景?
这是摘要的摘要.
以下是我想要保存到文件的色彩映射.
scaled_image = Image.fromarray(np.uint8(self.image*255))
plt.savefig("/home/test.png")
Run Code Online (Sandbox Code Playgroud)
self.image 是256x256 numpy数组,它的所有值都在-1和1之间.

如何将此图像输出到Qt中的场景?您可以使用self.image = numpy.random.random((256, 256))与我类似的起点.你如何将你的2D numpy随机值数组作为colormap放到pyqt场景中?
很近.这似乎有效,但规模已经反转.蓝色现在很热,红色很冷.如何切换它使它看起来像上面的图像?
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
gcf().canvas.draw() # IMPORTANT!
stringBuffer = gcf().canvas.buffer_rgba() # IMPORTANT!
l, b, w, h = gcf().bbox.bounds
qImage = QtGui.QImage(stringBuffer,
w,
h,
QtGui.QImage.Format_ARGB32)
pixmap = QtGui.QPixmap.fromImage(qImage)
pixmapItem = QtGui.QGraphicsPixmapItem(pixmap)
scene.addItem(pixmapItem)
self.graphicsView.setScene(scene)
Run Code Online (Sandbox Code Playgroud)
以下给我一个空白的屏幕
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
pixMap = QPixmap(scaled_image)
scene.addPixmap(pixMap)
self.graphicsView.setScene(scene)
Run Code Online (Sandbox Code Playgroud)
下面给我一个输出到Qt场景的灰度.它为什么不是彩色的?
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255)) …Run Code Online (Sandbox Code Playgroud) Matplotlib有很多很好的色彩图,但性能不好.我正在编写一些代码来使灰度图像变得鲜艳,其中使用颜色图进行插值是一个好主意.我想知道是否有开源彩色地图或演示代码使用Pillow通过colormap将灰度图像转换为彩色图像?
澄清:
演示:
第一个图像是灰度,第二个图像是'jet'cmap,第三个是'热'.
问题是我对颜色知之甚少,我想在PIL中实现这样的效果以获得更好的性能.
快速总结:matplotlib savefig 对于 PNG 来说太慢了。...寻找关于如何加快速度的想法/想法,或替代库(chaco?开罗?)
更新:添加了一些(非常粗略且准备好的)代码以在底部进行说明。
我正在使用 matplotlib (python 3.x,四核 macbook 上最新的 anaconda)通过imshow(). 我的目标是简单地在磁盘上生成带注释的图像文件(不需要交互式显示)。
轴设置为完全填充图形(因此没有样条线/控制等),并且 dpi/尺寸组合设置为匹配数组的大小 - 因此没有缩放/插值等。
在该单轴之上,我显示 3 个文本区域和一些(~6)矩形块。
...所以没有什么花哨的东西,而且从绘图的角度来看非常简单。
然而,当我将图形(带有savefig)保存为 PNG 时,大约需要 1.8 秒(!!!)。...保存为 raw 或 jpg 都需要约 0.7 秒。
我尝试将后端切换到 Agg,但这将 savefig() 的时间增加到约 2.1 秒
我是否认为这太慢了?我更喜欢保存为 PNG,而不是 JPG - 但我不明白为什么 PNG 比 JPG 慢那么多。我的目标是在 AWS 上部署,所以关心这里的速度。
有没有更快的库?(我不需要交互式 UI 绘图,只需要基本的保存到文件绘图)
下面是一些粗略且现成的代码,大致说明了这一点。我的机器上的输出是:
current backend: MacOSX
default save: 0.4048
default save - float64: 0.3446
full size figure: 0.8105
full size figure - with text/rect: …Run Code Online (Sandbox Code Playgroud) 我想要实现 Photoshop 中提供的渐变映射效果。已经有一篇文章解释了期望的结果。另外,这个答案完全涵盖了我想做的事情,但是
im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255))
Run Code Online (Sandbox Code Playgroud)
不适合我,因为我不知道如何将数组标准化为 1.0 的值。
下面是我的代码,因为我想让它工作。
im = Image.open(filename).convert('L') # Opening an Image as Grayscale
im_arr = numpy.asarray(im) # Converting the image to an Array
# TODO - Grayscale Color Mapping Operation on im_arr
im = Image.fromarray(im_arr)
Run Code Online (Sandbox Code Playgroud)
任何人都可以指出将颜色图应用于该数组的可能选项和理想方法吗?我不想绘制它,因为似乎没有一种简单的方法将 pyplot 图转换为图像。
另外,您能否指出如何规范化数组,因为我无法这样做并且无法在任何地方找到帮助。
x是一个numpy.float32数组,使用来自值-200到0.这些是dB(分贝)值.
当我这样做时(按照此处的建议):
Image.fromarray(x, mode='F')
Run Code Online (Sandbox Code Playgroud)
我得到一个灰度或有时几乎是黑色的图像.
如何将[-200,0]中的float映射到24位RGB字节数组(使用色彩映射),可以使用Python模块PIL读取Image.fromarray(x, mode='RGB')?
这是一些要测试的代码:
import scipy, numpy as np
import scipy.io.wavfile as wavfile
import numpy as np
from PIL import Image
def stft(x, fftsize=1024, overlap=4):
hop = fftsize / overlap
w = scipy.hanning(fftsize+1)[:-1]
return np.array([np.fft.rfft(w*x[i:i+fftsize]) for i in range(0, len(x)-fftsize, hop)])
def dB(ratio):
return 20 * np.log10(ratio+1e-10)
def magnitudedB(frame, fftsize=1024):
w = scipy.hanning(fftsize+1)[:-1]
ref = np.sum(w) / 2
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试将图像从 numpy 数组格式转换为 PIL 格式。这是我的代码:
img = numpy.array(image)
row,col,ch= np.array(img).shape
mean = 0
# var = 0.1
# sigma = var**0.5
gauss = np.random.normal(mean,1,(row,col,ch))
gauss = gauss.reshape(row,col,ch)
noisy = img + gauss
im = Image.fromarray(noisy)
Run Code Online (Sandbox Code Playgroud)
此方法的输入是 PIL 图像。此方法应将高斯噪声添加到图像中,并再次将其作为 PIL 图像返回。
任何帮助是极大的赞赏!
python ×5
matplotlib ×3
numpy ×3
colormap ×1
colors ×1
image ×1
pyqt4 ×1
python-3.x ×1
qt ×1
spectrogram ×1