我希望将音频的STFT谱图存储为图像。以下代码向我显示了一个谱图作为输出,但保存为图像时,我得到了不同的图像。
但是当我保存它时,输出文件是一张黑色的图片。 我想要保存确切的频谱图像。
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
audio_name = '---.au'
hop_length = 512
window_size = 1024
import librosa
y, sr = librosa.load(audio_name)
window = np.hanning(window_size)
out = librosa.core.spectrum.stft(y, n_fft = window_size, hop_length = hop_length,
window=window)
out = 2 * np.abs(out) / np.sum(window)
import librosa.display
librosa.display.specshow(librosa.amplitude_to_db(out,ref=np.max),
y_axis='log', x_axis='time')
from PIL import Image
img = Image.fromarray(out)
if img.mode != 'RGBA':
img = img.convert('RGBA')
img.save('output.png')
但是当我保存它时,输出文件是一张黑色的图片。 我想要保存确切的频谱图像。