2017-05-08 61 views
0

我有以下代碼創建的音頻文件的波形:提取RGBA陣列matplotlib

times = np.arange(len(data))/float(samplerate) 
fig = plot.figure(figsize = (8,4), frameon = False) 
axes = fig.gca() 
if(channels == 1): #mono file 
    axes.fill_between(times, data, color = 'k') 
else: #stereo file 
    axes.fill_between(times, data[:,0], data[:,1], color = 'k') 

plot.savefig(savefile, dpi = 'figure', transparent = True) 

我曾嘗試多種方法提取從該圖然而沒有RGBA陣列獲得了成功。我當前的代碼是下面:

canvas.draw()  # draw the canvas, cache the renderer 

width, height = fig.get_size_inches() * fig.get_dpi() 
waveformRGBA= np.fromstring(canvas.tostring_rgb(),dtype='uint8').reshape(height, width, 3) 
print waveformRGBA 

這種方法產生的[255,255,255]

任何想法空白RGBA陣列???

+0

您的「當前代碼」不顯示如何創建「畫布」。這可能有助於瞭解。 –

+0

可能重複的[matplotlib:渲染到緩衝區/訪問像素數據](http://stackoverflow.com/questions/21939658/matplotlib-render-into-buffer-access-pixel-data) – kazemakase

+0

我看到你使用差不多(但不完全)與在上面鏈接的問題的答案中相同的命令。這個答案中的例子是否也適用於你?否則,嘗試解決這些差異。 – kazemakase

回答

1

若要從PNG文件中讀取RGB和α:

from scipy import misc 
im = misc.imread("figure.png") 

在這種情況下im是3D陣列RGB和α信息用於與圖中的每個像素(行×列×4)尺寸行x列。