Python 将png转换回wav

Python 将png转换回wav,python,Python,我修改了一些代码,将.wav文件转换成.png文件 .wav到.png转换的原始源来自: 我对其进行了编辑,以使颜色以渐变方式进行排序,使其看起来像: 在这里: 从PIL导入图像 导入波浪、结构、系统、数学 ## #收集输入 ## 如果sys.argv[1][-4:!='。wav': sys.exit(“第一个参数应该是.wav文件”) 如果sys.argv[2][4:]!='。png': sys.exit(“第二个参数应该是.png文件”) ## #转换: ## #Wave文件需要是16位m

我修改了一些代码,将.wav文件转换成.png文件

.wav到.png转换的原始源来自: 我对其进行了编辑,以使颜色以渐变方式进行排序,使其看起来像:

在这里:

从PIL导入图像
导入波浪、结构、系统、数学
##
#收集输入
##
如果sys.argv[1][-4:!='。wav':
sys.exit(“第一个参数应该是.wav文件”)
如果sys.argv[2][4:]!='。png':
sys.exit(“第二个参数应该是.png文件”)
##
#转换:
##
#Wave文件需要是16位mono
波形文件=波形打开(系统argv[1],'r')
if waveFile.getnchannels()!=1:
sys.exit(“错误:波形文件应为单通道(mono)”)
imageRgbArray=list()
波长=波形文件。getnframes()
#创建图像大小(基于长度)
imageSize=math.ceil(math.sqrt(波长))
#循环浏览wave文件
对于范围内的i(波长):
#尝试读取帧,如果不可能,则填充0x0
尝试:
waveData=waveFile.readframes(1)

数据=struct.unpack("由于您将wav数据转换为图像数据的方式导致信息丢失,因此无法将其转换回原始形式。感谢您的回复。是否有一种方法可以在不丢失数据的情况下正确转换它,但仍能对颜色进行排序?可能,但坦率地说,我不确定您想要的图像是什么创建方法或表示。在这方面,您正在对数据进行的排序确实会把事情搞砸。我在尝试解决您的其他问题时试图弄清楚目标是什么,但过了一段时间后放弃了,因为这似乎没有多大意义。我更新了上面的代码以接受更长的音频,这确实使图像变大了r、 这是在我开始乱搞色彩之前:(适用于任何音频大小)我希望能够将颜色组织在一起,这样它们就可以被分类在一起,这样照相机就更容易分辨,比如QR码。我不知道如何纠正信息丢失,或者在颜色重新排列后如何读回信息。这听起来是个很酷的主意,为什么不使用递归神经网络而不是卷积神经网络呢据我所知(我当然不是这方面的专家)。RNN非常适合时间序列数据,如音频文件。这样,您就不会丢失从16位采样到8位像素的任何信息。
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)")


imageRgbArray = list()

waveLength = waveFile.getnframes()

# Create the image size (based on the length)
imageSize = math.ceil(math.sqrt(waveLength))

# Loop through the wave file
for i in range(waveLength):

    # 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
    except:
        convertedData = 0
        pass

    bits = 5
    rgbData = tuple([(convertedData>>bits*i)&(2**bits-1) for i in range(3)])
    rgbData = tuple(map(lambda x: x<<3, rgbData))

    # 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(list(sorted(imageRgbArray)))

# Save image
im.save(sys.argv[2])
from PIL import Image
import wave, struct, sys, soundfile as Sndfile, numpy as np, math

##
# Collect input
##
if sys.argv[1][-4:] != '.png':
    sys.exit("First argument should be a .png file")

if sys.argv[2][-4:] != '.wav':
    sys.exit("Second argument should be a .wav file")

##
# Conversion:
##

# Open image
with Image.open(sys.argv[1]) as pngFile:

    # Load image
    pngAllPixels = pngFile.load()

    # Set the counters that create the image
    countX = 0
    countY = 0
    count = pngFile.size[0] * pngFile.size[1]

    # Create the array which will contain all the bits
    bitArray = list()

    # Loop through the individual pixels
    while count > 0:

        # Set the location of the pixel that should be loaded
        singlePixel = pngAllPixels[countX, countY]

        # Get RGB vals and convert them to hex
        singlePixelToHexString = '%02x%02x%02x' % (singlePixel[0], singlePixel[1], singlePixel[2])

        # Break if end of file (0x0)
        if singlePixelToHexString == "000000":
            break # break because audio is < 44100 bit

        # Convert hex string into actual hex
        singlePixelToHex = hex(int("0x" + singlePixelToHexString.lstrip("0"), 16) + int("0x0", 16))

        # This adds 16bit/2 (=32768) to the data and converts hex into a bit
        singleBit = int(singlePixelToHex, 16) - 32768

        # Append the single bit to the array
        bitArray.append(singleBit)

        # Run through the image and set x and y vals (goes to next row when ready)
        if countX == (pngFile.size[0] - 1):
            countX = 0
            countY += 1
        else:
            countX += 1
        count -= 1

    # Convert the array into a Numpy array
    bitArrayNp = np.array(bitArray, dtype=np.int16)

    # Output the file
Sndfile.write(sys.argv[2], bitArrayNp, 44100, 'PCM_16')