2017-11-06 2 views
0

을 사용하여 스펙트로 그램 (축이나 다른 내용이없는 내용 만)을 저장하십시오. wav 파일에서 스펙트로 그램을 얻은 다음 png에 저장하고 싶습니다. 이미지의 내용 만 필요합니다. (축 또는 다른 것이 아닙니다). 나는 또한 the Matplotlib documentation을 읽은
Matplotlib plots: removing axis, legends and white spaces
scipy: savefig without frames, axes, only content
이러한 질문을 통해 들어 왔지만 그것은 쓸모없는 것 같습니다 그래서 질문 중 답변 위의 오래된 또는 내가 뭔가 잘못하고있어 간단한Matloptlib

plt.savefig('out.png', bbox_inches='tight', pad_inches=0)

때문에 내가 성취하고자하는 것을하지 않습니다. 처음에는 this guide을 따르려고했지만 코드가 충돌합니다. 그럼 난 this approach을 시도하지만 오래된 이후 나는 그것을 조금 수정 : 어쩌면 그것은, 보이지 않는
enter image description here 을하지만, 가장 큰에서 콘텐츠 주위에 흰색 테두리가 (있다 :

import matplotlib.pyplot as plt 
from scipy.io import wavfile 
import numpy as np 

def graph_spectrogram(wav_file): 
    rate, data = wavfile.read(wav_file) 
    pxx, freqs, bins, im = plt.specgram(x=data, Fs=rate, noverlap=384, NFFT=512) 
    plt.axis('off') 
    plt.savefig('sp_xyz.png', bbox_inches='tight', dpi=300, frameon='false') 

if __name__ == '__main__': # Main function 
    graph_spectrogram('...') 

이것은 내가 가진 무엇 가장 작은 것) : left, bottom, top, right. 나는 같은 이미지를 원하지만 그저 다른 것없이 내용 만 원한다. 어떻게하면 될까요? 파이썬 3.6과 Matplotlib 2.0.2를 사용합니다.

+0

시도'pad_inches = 0.0' 아니, 불행하게도 plt.savefig'의 매개 변수 (...)' – bastelflp

+0

로 . 나는 이것을 링크 된 질문에 대한 논평에서 보았고 나는 그것을 일찍 시도했지만 아무 것도 바뀌지 않았다. – Colonder

+0

방금 ​​[this] (https://stackoverflow.com/questions/11837979/removing-white-space-around-a-save-image-in-matplotlib)의 'pad_inches'에 음수 값을 지정할 수있는 것을 보았습니다.) 대답 해 주셔서 감사합니다. –

답변

1

나는 당신이 subplots_adjust을 원한다고 생각 :이 경우

fig,ax = plt.subplots(1) 
fig.subplots_adjust(left=0,right=1,bottom=0,top=1) 
ax.axis('tight') 
ax.axis('off') 

:

import matplotlib.pyplot as plt 
from scipy.io import wavfile 
import numpy as np 

def graph_spectrogram(wav_file): 
    rate, data = wavfile.read(wav_file) 
    fig,ax = plt.subplots(1) 
    fig.subplots_adjust(left=0,right=1,bottom=0,top=1) 
    ax.axis('off') 
    pxx, freqs, bins, im = ax.specgram(x=data, Fs=rate, noverlap=384, NFFT=512) 
    ax.axis('off') 
    fig.savefig('sp_xyz.png', dpi=300, frameon='false') 

if __name__ == '__main__': # Main function 
    graph_spectrogram('...') 
+0

이미지가 1920 x 1440 px이긴하지만 내용이 전혀 없습니다. – Colonder

+0

'ax.specgram'대신'plt.specgram'을 사용했습니다. 감사 – Colonder