pythonmatplotliblibrosa

Save scalogram as image


i would like to read wave files from directory, create a scalogram image and save inside another directory,for scalogram creation, i have searched a lot of tutorial and find this link scalogram,i have applied to my audio file and scalogram is generated pretty fine, here is image : scalogram

only left part is to save it as jpg format under some resolution to specific directory, could you tell me please how to do it?i will post code as well

import cv2
from scipy import signal
import numpy as np
import pywt
import matplotlib.pyplot as plt
from ssqueezepy import cwt
from ssqueezepy.visuals import plot, imshow
import librosa

y,sr =librosa.load("genres_original/classical/classical.00000.wav")

# cwtmatr =signal.cwt(y,signal.gau,width)
Wx, scales = cwt(y, 'morlet')
imshow(Wx, yticks=scales, abs=1,
       title="abs(CWT) | Morlet wavelet",
       ylabel="scales", xlabel="samples")
plt.show()

Solution

  • Adding plt.savefig('filename.jpg') to the end will save a JPEG image of the entire plot - including axes markers, titles etc.

    In order to avoid showing the plot on screen, you need to remove the call to plt.show() and explicitly create the figure and axes.

    fig, ax = plt.subplots(1)
    imshow(fig=fig, ax=ax, show=0, ....)
    

    If what you want is to save the 2d array that is the data of the scaleogram (for further processing), then you should use something like numpy.savez('filename.npy', wx=Wx, scales=scales).