User:Elleke Hageman/Python: Sound and Image
< User:Elleke Hageman
Revision as of 11:23, 14 October 2013 by Elleke Hageman (talk | contribs)
Image #2 (above)
import struct, array
width = 320
height = 240
filename="original.tga"
datafile = open(filename, "wb")
header = struct.pack("<BBBHHBHHHHBB", 0, 0, 2, 0, 0, 8, 0, 0, width, height, 24, 1 << 5)
datafile.write(header)
data = ''
for y in xrange(50):
for x in xrange(50):
r, g, b = 5, 0, 205
#achtergrond
if x > 10 and x < 10:
#middenstuk eerste cf breedte ervoor 2de breedte ding zelf
g = 255
if y < 20:
#hoogte bovenste kolom
r = 255
if y > 120:
b = 128
for x in xrange(50):
for y in xrange(50):
if y > 10 and x < 10:
#middenstuk eerste cf breedte ervoor 2de breedte ding zelf
g = 255
if y < 20:
#hoogte bovenste kolom
r = 255
if y > 120:
b = 128
data += struct.pack('B', b)
data += struct.pack('B', g)
data += struct.pack('B', r)
datafile.write(data)
datafile.close()
Image #1 (failed attempt)
import struct, array
width = 320
height = 240
filename="image1.tga"
datafile = open(filename, "wb")
# TGA format: http://gpwiki.org/index.php/TGA
# Offset, ColorType, ImageType, PaletteStart, PaletteLen, PalBits, XOrigin, YOrigin, Width, Height, BPP, Orientation
header = struct.pack("<BBBHHBHHHHBB", 0, 0, 2, 0, 0, 8, 0, 0, width, height, 24, 1 << 5)
datafile.write(header)
data = ''
for y in xrange(320):
for x in xrange(240):
r, g, b = 150, 0, 205
if y < 32:
r = 217
if x > 64 and x < 256:
g = 0
if y > 120:
b = 10
data += struct.pack('B', b)
data += struct.pack('B', g)
data += struct.pack('B', r)
for y in xrange(170):
for x in xrange(90):
r, g, b = 100, 100, 100
if y < 150:
r = 192
if x > 80 and x < 256:
g = 4
if y > 200:
b = 159
data += struct.pack('B', b)
data += struct.pack('B', g)
data += struct.pack('B', r)
for y in xrange(height):
for x in xrange(width):
r, g, b = 0, 0, 0
if y < 32:
r = 36
if x > 64 and x < 256:
g = 0
if y > 120:
b = 217
data += struct.pack('B', b)
data += struct.pack('B', g)
data += struct.pack('B', r)
datafile.write(data)
datafile.close()
'''Sound #1'''
<source lang="python">
import wave, struct
filename = "sound.wav"
nframes=0
nchannels=1
sampwidth=1 # in bytes so 2=16bit, 1=8bit
framerate=44100
bufsize=2048
w = wave.open(filename, 'w')
w.setparams((nchannels, sampwidth, framerate, nframes, 'NONE', 'not compressed'))
max_amplitude = float(int((2 ** (sampwidth * 8)) / 2) - 1)
# split the samples into chunks (to reduce memory consumption and improve performance)
#for chunk in grouper(bufsize, samples):
# frames = ''.join(''.join(struct.pack('h', int(max_amplitude * sample)) for sample in channels) for channels in chunk if channels is not None)
# w.writeframesraw(frames)
freq = 15000
# this means that FREQ times a second, we need to complete a cycle
# there are FRAMERATE samples per second
# so FRAMERATE / FREQ = CYCLE LENGTH
cycle = framerate / freq
data = ''
for i in range(10):
for x in range(20000):
data += struct.pack('h', int(0.4 * max_amplitude))
for x in range(1000):
data += struct.pack('h', int(-0.8 * max_amplitude))
w.writeframesraw(data)
w.close()
Sound #2
import wave, struct
filename = "sound2.wav"
nframes=0
nchannels=1
sampwidth=2
framerate=44100
bufsize=2048
w = wave.open(filename, 'w')
w.setparams((nchannels, sampwidth, framerate, nframes, 'NONE', 'not compressed'))
max_amplitude = float(int((2 ** (sampwidth * 8)) / 2) - 1)
freq = 1200
cycle = framerate / freq
data = ''
for i in range(500):
for _ in range(cycle/1):
data += struct.pack('h', int(0.5 * max_amplitude))
for _ in range(cycle/1):
data += struct.pack('h', int(-0.5 * max_amplitude))
freq = 200
cycle = framerate / freq
for _ in range(100):
data = ''
cycle = framerate / freq
for _ in range(190):
for _ in range(cycle/2):
data += struct.pack('h', int(0.5 * max_amplitude))
data += struct.pack('h', 0)
for _ in range(cycle/2):
data += struct.pack('h', int(-0.5 * max_amplitude))
data += struct.pack('h', 0)
w.writeframesraw(data)
w.close()