img2music / app.py
Kvikontent's picture
Update app.py
5569dfb
import streamlit as st
import numpy as np
import librosa
import soundfile as sf
from PIL import Image
from io import BytesIO
def process_color_to_audio(array, sample_rate=44100, duration=5):
r_mean = np.mean(array[:, :, 0]) / 255.0
g_mean = np.mean(array[:, :, 1]) / 255.0
b_mean = np.mean(array[:, :, 2]) / 255.0
min_freq = 100
max_freq = 1000
r_freq = min_freq + (1 - r_mean) * (max_freq - min_freq)
g_freq = min_freq + (1 - g_mean) * (max_freq - min_freq)
b_freq = min_freq + (1 - b_mean) * (max_freq - min_freq)
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
audio_data = np.sin(2 * np.pi * r_freq * t) + np.sin(2 * np.pi * g_freq * t) + np.sin(2 * np.pi * b_freq * t)
audio_data /= np.max(np.abs(audio_data))
return audio_data, sample_rate
def main():
st.title('PNG to Audio Streamlit App')
st.write("Upload a PNG image and get the corresponding audio!")
uploaded_file = st.file_uploader("Choose a PNG image", type="png")
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded PNG image', use_column_width=True)
if st.button("Generate Audio"):
array = np.array(image)
audio_data, sample_rate = process_color_to_audio(array)
with BytesIO() as output:
sf.write(output, audio_data, sample_rate, format='wav')
st.audio(output.getvalue(), format='audio/wav')
if __name__ == '__main__':
main()