Post by nathanmyersc on Mar 20, 2024 2:55:02 GMT
It requires Python Image Library
Uses same concept as the new Text to Wav writer i just shared. It skips the header and takes the pixel data and converts it into wav data if even its 32767 - pixel value if its odd its -32767 + pixel value
Just cool stuff to try.
Python image library should standardly handle
JPEG
PNG
BMP
GIF
TIFF
WebP
PPM/PGM
OpenEXR
ICO
PSD (read-only)
and more...
It says.
Uses same concept as the new Text to Wav writer i just shared. It skips the header and takes the pixel data and converts it into wav data if even its 32767 - pixel value if its odd its -32767 + pixel value
Just cool stuff to try.
Python image library should standardly handle
JPEG
PNG
BMP
GIF
TIFF
WebP
PPM/PGM
OpenEXR
ICO
PSD (read-only)
and more...
It says.
from PIL import Image
import struct
import wave
import os
def jpg_to_wav(input_file, output_file, channels=1, sampling_rate=767500):
# Open the JPEG file
with Image.open(input_file) as img:
# Check if the image has an alpha channel
if img.mode == 'RGBA':
has_alpha = True
else:
has_alpha = False
# Get pixel data
pixel_data = list(img.getdata())
# Adjust pixel values
adjusted_data = []
for pixel in pixel_data:
# Extract RGB(A) values
if has_alpha:
r, g, b, a = pixel
adjusted_data.extend([
32767 - r if r % 2 == 0 else -32767 + r,
32767 - g if g % 2 == 0 else -32767 + g,
32767 - b if b % 2 == 0 else -32767 + b,
32767 - a if a % 2 == 0 else -32767 + a
])
else:
r, g, b = pixel
adjusted_data.extend([
32767 - r if r % 2 == 0 else -32767 + r,
32767 - g if g % 2 == 0 else -32767 + g,
32767 - b if b % 2 == 0 else -32767 + b
])
# Reshape data to match the number of channels
num_samples = len(adjusted_data)
num_samples_per_channel = num_samples // channels
adjusted_data = adjusted_data[:num_samples_per_channel * channels]
data_channels = [adjusted_data[i::channels] for i in range(channels)]
# Convert data to bytes
frames = b''.join([struct.pack('<h', sample) for sample in adjusted_data])
# Write data to WAV file
with wave.open(output_file, 'w') as wav_file:
wav_file.setnchannels(channels)
wav_file.setsampwidth(2) # 16-bit audio
wav_file.setframerate(sampling_rate)
wav_file.writeframes(frames)
def get_valid_filename(prompt):
while True:
filename = input(prompt)
if os.path.exists(filename):
return filename
print("File not found. Please enter a valid filename.")
# Ask for input file name
input_file = get_valid_filename("Enter input image file name: ")
# Ask for output file name
output_file = input("Enter output file name (without extension): ") + " ImageWavMaker.wav"
jpg_to_wav(input_file, output_file)
I tested it with this image of marijuanna with a intentioned bitmap overlaying it and am starting to get a pretty stoned feeling. its really cool.