将生成的频谱图图像数据作为 jpeg 文件保存到 google 驱动器中的目录中

Saving generated spectrogram image data into a directory as jpeg files in google drive

我有一个语音数据集,想在 google 驱动器中将 spectrogram/chromogram 图像提取为 jpeg。我在此处保存的代码片段仅保存最后一张图像。我已经看到 librosa 库只提供 bgr 图像。有人可以帮我解决这个问题吗?

import os, glob
from PIL import Image, ImageOps 
import matplotlib.pyplot as plt 
def load_data():
    X,y=[],[]
    count = 0
    for file in glob.glob("/content/drive/My Drive/rand/data/Actor_*//*.wav"):
        file_name=os.path.basename(file)
        #src_fname, ext = file_name.splitext(x)
        emotion=emotions[file_name.split("-")[2]]
        chromagram = feature_chromagram(waveform, sample_rate)
        librosa.display.specshow(chromagram, y_axis='chroma', x_axis='time')
        fig = plt.Figure()
        save_fname = os.path.join(PATH, os.path.basename(emotion)+'.tif')
        #im.save(save_fname)
        fig.savefig(save_fname)
        features = get_features(file)
        X.append(features)
        #y.append(emotion)
        count += 1
        # '\r' + end='' results in printing over same line
        print('\r' + f' Processed {count}/{1440} audio samples',end=' ')
    # Return arrays to plug into sklearn's cross-validation algorithms
    return np.array(X), np.array(y) ```

我已更正代码..

for folder in os.listdir(data_dir):
  i=0
  for fil in os.listdir(os.path.join(data_dir,folder)):
    dir=os.path.join(data_dir,folder,fil)
    with soundfile.SoundFile(dir) as audio:
      waveform=audio.read(dtype="float32")
      sample_rate=audio.samplerate
      librosa.display.waveplot(waveform,sr=sample_rate)
      stft_spectrum_matrix = librosa.stft(waveform)
      librosa.display.specshow(librosa.amplitude_to_db(np.abs(stft_spectrum_matrix), ref=np.max),y_axis='log', x_axis='time')
      #print(i)
      plt.savefig(os.path.join(out_dir,folder,'spec{:04}'.format(i)))
      mfc_coefficients = librosa.feature.mfcc(waveform, sr=sample_rate, n_mfcc=40)
      librosa.display.specshow(mfc_coefficients, x_axis='time',norm=Normalize(vmin=-30,vmax=30))
      plt.savefig(os.path.join(out_dir,folder,'mfcc{:04}'.format(i)))
      chromagram = librosa.feature.chroma_stft(waveform, sr=sample_rate)
      librosa.display.specshow(chromagram, y_axis='chroma', x_axis='time')
      plt.savefig(os.path.join(out_dir,folder,'chroma{:04}'.format(i)))
      i+=1

将这一行修改为

plt.savefig(os.path.join(out_dir,folder,'chroma{:04}.jpg'.format(i)))