Merge pull request #16 from jagilley/main

Add image_from_spectrogram to audio.py
This commit is contained in:
Hayk Martiros 2022-12-18 22:58:01 -08:00 committed by GitHub
commit 01e8a4774a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 0 deletions

View File

@ -85,6 +85,34 @@ def spectrogram_from_image(
return data
def image_from_spectrogram(
spectrogram: np.ndarray, max_volume: float = 50, power_for_image: float = 0.25
) -> Image.Image:
"""
Compute a spectrogram image from a spectrogram magnitude array.
"""
# Apply the power curve
data = np.power(spectrogram, power_for_image)
# Rescale to 0-1
data = data / np.max(data)
# Rescale to 0-255
data = data * 255
# Invert
data = 255 - data
# Convert to a PIL image
image = Image.fromarray(data.astype(np.uint8))
# Flip Y
image = image.transpose(Image.FLIP_TOP_BOTTOM)
# Convert to RGB
image = image.convert("RGB")
return image
def spectrogram_from_waveform(
waveform: np.ndarray,