每張圖片自帶BGM?牛津小哥開源神器,實現任意影像轉換聲譜圖
- 2020 年 2 月 12 日
- 筆記
十三 發自 凹非寺 量子位 報道 | 公眾號 QbitAI
給你一張圖片,你能想像它的聲音嗎?
一個叫SpectroGraphic的神器就能做到這點。

例如,給定一個「怪物史萊克」的照片,通過這個工具,就能生成其對應的聲譜圖。

影像嵌入聲譜圖
大多數聲音是許多聲波的複雜組合,而每一種聲波都有不同的頻率和強度。
聲譜圖(spectrogram)是一種表示聲音的方法,它的橫軸是時間,縱軸是頻譜。

△聲譜圖示例
而SpectroGraphic所做的工作就是獲取一張影像,簡單地把它解釋成一張聲譜圖。
這樣,就可以通過產生的聲音,便將影像嵌入到了聲譜圖中。
是不是非常酷炫?
現在,項目已開源,每個人都可以體驗了!
項目安裝
首先,運行如下程式碼來獲取命令行工具spectrographic:
pip install spectrographic
也可以直接把stand-alone文件夾里的spectrographic.py文件,作為命令行工具使用。

此外,還需要確保滿足 requirements.txt 文件中涉及的所有依賴項。
可以通過如下命令來進行安裝:
pip install requirements.txt
在使用 pip 進行安裝之後,只需要在控制台中運行 spectrographic […]。
在使用stand-alone腳本時,必須使用 python spectrographic.py […]。
還可以簡單地從 SpectroGraphic.base 中導入 SpectroGraphic 類。
命令行工具的使用
usage: spectrographic [-h] [--version] -i PATH_TO_IMAGE [-d DURATION] [-m MIN_FREQ] [-M MAX_FREQ] [-r RESOLUTION] [-c CONTRAST] [-p] [-s SAVE_FILE] Turn any image into sound. optional arguments: -h, --help show this help message and exit --version show program's version number and exit -i PATH_TO_IMAGE, --image PATH_TO_IMAGE Path of image that we want to embed in a spectrogram. -d DURATION, --duration DURATION Duration of generated sound. -m MIN_FREQ, --min_freq MIN_FREQ Smallest frequency used for drawing the image. -M MAX_FREQ, --max_freq MAX_FREQ Largest frequency used for drawing the image. -r RESOLUTION, --resolution RESOLUTION Vertical resolution of the image in the spectrogram. -c CONTRAST, --contrast CONTRAST Contrast of the image in the spectrogram. -p, --play Directly play the resulting sound. -s SAVE_FILE, --save SAVE_FILE Path to .wav file in which to save the resulting sound.
如果你的源影像在./source.png,想要生成10s的音頻,頻率範圍為10kHz到20kHz,還希望保存為sound.wav,最終還要播放音頻。
那麼就運行如下程式碼:
spectrographic --image ./source.png --min_freq 10000 --max_freq 20000 --duration 10 --save sound.wav --play
如果你正在使用stand-alone腳本:
python spectrographic.py --image ./source.png --min_freq 10000 --max_freq 20000 --duration 10 --save sound.wav --play
作者簡介

△右:Levi Borodenko
Levi Borodenko,目前在牛津大學攻讀數學和統計學碩士學位。學術研究重點是隨機分析和機器學習。
本科也就讀於牛津大學,2017-2018擔任PROMYS Oxford的輔導員,2018-2019年擔任PROMYS Boston的首席輔導員。
更多開源項目可訪問如下鏈接: https://github.com/LeviBorodenko
傳送門
項目地址: https://github.com/LeviBorodenko/spectrographic
個人簡介: https://cv.levib.dev/