Store the Spectrogram as Image in Python

Becky picture Becky · Sep 20, 2018 · Viewed 10.4k times · Source

I want to store the STFT spectrogram of the audio as image. The code below shows a spectrogram to me as output, but when saved as image I get a different image.

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')

enter image description here

from PIL import Image
img = Image.fromarray(out)    
if img.mode != 'RGBA':
    img = img.convert('RGBA')
img.save('output.png')

But when I save it the output file is a black image.enter image description here

I want to save the exact image of the spectogrm.

Answer

Jon Nordby picture Jon Nordby · Oct 6, 2018

If you want exactly what librosa.display.spectrogram() will show, then use matplotlib to save the plot to a file:

import matplotlib.pyplot as plt
import librosa.display

import numpy as np
import pandas as pd
import librosa


filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
y = y[:100000] # shorten audio a bit for speed

window_size = 1024
window = np.hanning(window_size)
stft  = librosa.core.spectrum.stft(y, n_fft=window_size, hop_length=512, window=window)
out = 2 * np.abs(stft) / np.sum(window)

# For plotting headlessly
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

fig = plt.Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
p = librosa.display.specshow(librosa.amplitude_to_db(out, ref=np.max), ax=ax, y_axis='log', x_axis='time')
fig.savefig('spec.png')

spec.png:

spec.png