Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store the Spectrogram as Image in Python

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.

like image 204
Becky Avatar asked Sep 20 '18 20:09

Becky


People also ask

How do you save a spectrogram as an image?

Convert the power spectrogram (amplitude squared) to decibel (dB) units, using power_to_db() method.. Display the spectrogram as img (we can save it here). Save the img using savefig(). Display the image using plt.


1 Answers

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

like image 95
Jon Nordby Avatar answered Oct 02 '22 16:10

Jon Nordby