Skip to content

Instantly share code, notes, and snippets.

@hirocarma
Created September 6, 2024 14:17
Show Gist options
  • Save hirocarma/52dee55904044083dda989a228d122b8 to your computer and use it in GitHub Desktop.
Save hirocarma/52dee55904044083dda989a228d122b8 to your computer and use it in GitHub Desktop.
import os
import sys
import cv2
import numpy as np
from scipy.io import wavfile
def image_to_audio(image_path, output_wav):
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
height, width = img.shape
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
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}")
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"
image_to_audio(img_file, wav_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment