So I'm wanting to feed spectrogram images to a convolutional neural network as an attempt to classify various sounds. I want each image to be exactly 384x128 pixels. However, when I actually save the image it is only 297x98. Here's my code:
def save_spectrogram(num):
dpi = 128
x_pixels = 384
y_pixels = 128
samples, sr = load_wave(num)
stft = np.absolute(librosa.stft(samples))
db = librosa.amplitude_to_db(stft, ref=np.max)
fig = plt.figure(figsize=(x_pixels//dpi, y_pixels//dpi), dpi=dpi, frameon=False)
ax = fig.add_subplot(111)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.set_frame_on(False)
librosa.display.specshow(db, y_axis='linear')
plt.savefig(TRAIN_IMG+str(num)+'.jpg', bbox_inches='tight', pad_inches=0, dpi=dpi)
Does anyone have any pointers on how I can fix this? I've also tried doing it without the subplot, but when I do that it still saves as the wrong size AND has white space/background.
