I use pyplot to get the image. It seems like the default is color image, but I want to get the gray scale. I use the code below, however, I need to firstly save the color image before I get the gray scale image. Is there any way to show and save the gray scale image directly?
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
#downsampling
a = sound
R = 2
a=a.reshape(-1,R).mean(axis=1)
Pxx, freqs, bins, im = plt.specgram(a, NFFT=1024, Fs=44100/2,
noverlap=900) #Fs be downsampling
plt.savefig('sp_xyz.png')
image = Image.open('sp_xyz.png').convert("L")
arr = np.asarray(image)
plt.imshow(arr, cmap='gray_r')