title: Store the Spectrogram as Image in Python
body: <p>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.</p>
  
  <pre><code>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')
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/aPB7i.png" rel="noreferrer"><img src="https://i.stack.imgur.com/aPB7i.png" alt="enter image description here"></a></p>
  
  <pre><code>from PIL import Image
  img = Image.fromarray(out)    
  if img.mode != 'RGBA':
      img = img.convert('RGBA')
  img.save('output.png')
  </code></pre>
  
  <p>But when I save it the output file is a black image.<a href="https://i.stack.imgur.com/cVebs.png" rel="noreferrer"><img src="https://i.stack.imgur.com/cVebs.png" alt="enter image description here"></a></p>
  
  <p>I want to save the exact image of the spectogrm.</p>