File size: 1,533 Bytes
0042606
 
 
 
 
 
 
5569dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0042606
 
 
 
 
 
 
 
 
 
 
 
 
5074feb
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()