import matplotlib.pyplot as plt import numpy as np from scipy.io.wavfile import write

Load the image

image_path = 'hugging_face.jpg' image = plt.imread(image_path)

Convert the image to grayscale

grayscale_image = np.mean(image, axis=2)

Define the musical parameters

sample_rate = 44100 # Audio sample rate (Hz) duration = 0.1 # Duration of each note (seconds)

Define the mapping from pixel values to musical notes

min_note = 40 # MIDI note number for the lowest pixel value max_note = 80 # MIDI note number for the highest pixel value

Rescale the pixel values to the range [min_note, max_note]

scaled_image = (grayscale_image - np.min(grayscale_image)) scaled_image *= (max_note - min_note) / np.max(scaled_image) scaled_image += min_note

Generate the audio signal

total_duration = int(duration * sample_rate * grayscale_image.shape[1]) t = np.linspace(0, total_duration / sample_rate, total_duration, endpoint=False) audio_signal = np.zeros(total_duration) for i, column in enumerate(scaled_image.T): start = int(i * duration * sample_rate) end = int((i + 1) * duration * sample_rate) audio_signal[start:end] = np.sin(2 * np.pi * column * t[start:end])

Normalize the audio signal

audio_signal /= np.max(np.abs(audio_signal)) audio_signal *= 32767 # Scale the signal to the range of a 16-bit integer

Convert the audio signal to 16-bit integer format

audio_signal = audio_signal.astype(np.int16)

Save the audio signal to a WAV file

output_file = 'hugging_face.wav' write(output_file, sample_rate, audio_signal)

print(f"Audio file '{output_file}' generated successfully!")