Last active
September 6, 2024 15:14
-
-
Save hirocarma/e16560a2dfe0e22237ed17e418daf26d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import cv2 | |
import numpy as np | |
from scipy.io import wavfile | |
import matplotlib.pyplot as plt | |
def image_to_audio(image_path, output_wav, output_spectrogram): | |
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) | |
height, width = img.shape | |
spectrogram = [] | |
for col in range(width): | |
column_data = img[:, col] | |
f_col = np.fft.fft(column_data) | |
magnitude_col = 20 * np.log(np.abs(f_col)) | |
spectrogram.append(magnitude_col) | |
spectrogram = np.array(spectrogram).T | |
plt.figure(figsize=(10, 6)) | |
plt.imshow(spectrogram, cmap="jet", aspect="auto", origin="lower") | |
plt.title("Spectrogram (Time on X-axis)") | |
plt.xlabel("Time (Columns of Image)") | |
plt.ylabel("Frequency (FFT Magnitude)") | |
plt.colorbar() | |
plt.savefig(output_spectrogram) | |
plt.close() | |
f = np.fft.fft2(img) | |
fshift = np.fft.fftshift(f) | |
magnitude_spectrum = 20 * np.log(np.abs(fshift)) | |
plt.figure(figsize=(6, 6)) | |
plt.imshow(magnitude_spectrum, cmap='gray') | |
plt.title('Magnitude Spectrum') | |
plt.colorbar() | |
plt.savefig(output_spectrogram) | |
plt.close() | |
inverse_f = np.fft.ifftshift(fshift) | |
img_reconstructed = np.fft.ifft2(inverse_f) | |
audio_signal = np.real(img_reconstructed).flatten() | |
audio_signal_normalized = np.int16( | |
(audio_signal / np.max(np.abs(audio_signal))) * 32767 | |
) | |
sample_rate = 44100 | |
wavfile.write(output_wav, sample_rate, audio_signal_normalized) | |
print(f"Audio saved as {output_wav}") | |
print(f"Time-based Spectrogram saved as {output_spectrogram}") | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print("Usage: python script_name.py image_file") | |
sys.exit(1) | |
img_file = sys.argv[1] | |
basename = os.path.basename(img_file) | |
wav_file = basename + ".wav" | |
spectro_file = "output_time_spectrogram.png" | |
image_to_audio(img_file, wav_file, spectro_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment