我正在运行一个将wav文件转换为png的脚本,我遇到的问题是我无法加载超过1秒的wav文件。
我需要更改imageSize = math.sqrt(44100)
以增加允许的图片大小,但我不确定如何去做。我想加载一个60秒的wav文件。
#####################
#
# Convert WAV into PNG
# Author: @bobvanluijt
#
#####################
from PIL import Image
import wave, struct, sys, math
##
# Collect input
##
if sys.argv[1][-4:] != '.wav':
sys.exit("First argument should be a .wav file")
if sys.argv[2][-4:] != '.png':
sys.exit("Second argument should be a .png file")
##
# Conversion:
##
# Wave file needs to be 16 bit mono
waveFile = wave.open(sys.argv[1], 'r')
if waveFile.getnchannels() != 1:
sys.exit("ERROR: The wave file should be single channel (mono)")
if waveFile.getframerate() != 44100:
sys.exit("ERROR: The samplerate should be 44,100")
imageRgbArray = list()
waveLength = waveFile.getnframes()
# Create the image size (based on the length)
imageSize = math.sqrt(44100)
# Loop through the wave file
for i in range(0, 44100):
# Try to read frame, if not possible fill with 0x0
try:
waveData = waveFile.readframes(1)
data = struct.unpack("<h", waveData) # This loads the wave bit
convertedData = int(data[0]) + 32768 # This adds 16bit/2 (=32768) to the data
except:
convertedData = 0
pass
# This converts the number into a hex value.
convertedDataHex = hex(convertedData)
# convert the value to a string and strips first two characters
hexString = str(convertedDataHex)[2:]
# Check how much the string should be prefixed to get the color hex length (= 6 char)
count = 6 - len(hexString)
# Prefix with a zero
while (count > 0):
hexString = "0" + hexString
count -= 1
# Convert into RGB value
rgbData = tuple(int(hexString[i:i + 6 // 3], 16) for i in range(0, 6, 6 // 3)) # Convert to RGB data
# Add the RGB value to the image array
imageRgbArray.append(rgbData)
# Create new image
im = Image.new('RGB', (int(imageSize), int(imageSize)))
# Add image data
im.putdata(imageRgbArray)
# Save image
im.save(sys.argv[2])
答案 0 :(得分:0)
如果采样率为44100,那么图像大小应该是音频长度的44100倍的平方根。
尝试替换
imageSize = math.sqrt(44100)
与
imageSize = math.sqrt(44100*60)
假设你的wav文件恰好是60秒。