Prototyping raw image sequence assignment (2013): Difference between revisions
Lucia Dossin (talk | contribs) (→Lucia) |
|||
Line 166: | Line 166: | ||
output.close() | output.close() | ||
</source> | </source> | ||
|} | |||
==== Mihail ==== | |||
{| | |||
|[[File:Mishopyanim.gif]] | |||
import struct, sys | |||
width = 320 | |||
height = 240 | |||
header = struct.pack("<BBBHHBHHHHBB",0,0,2,0,0,8,0,0,width,height,32,1<<5) | |||
totalframes = 25 | |||
for frame in xrange(totalframes): | |||
out = open("frame_%02d.tga" % frame, "wb") | |||
out.write(header) | |||
for y in xrange(height): | |||
for x in xrange(width): | |||
if y >= 200: | |||
r = 5*(float(frame)) | |||
g = y/4+2*(float(frame)) | |||
b = 3*(float(frame)) | |||
a = 255 | |||
elif y <= 75: | |||
r = (x+y)/4+(float(frame)/totalframes) | |||
g = y+2*(float(frame)) | |||
b = y+2*(float(frame)) | |||
a = 255 | |||
else: | |||
r = 5+5*(float(frame)/totalframes) | |||
g = y-2*(float(frame)) | |||
b = 4*(float(frame)) | |||
a = 255 | |||
out.write(struct.pack('B', b)) | |||
out.write(struct.pack('B', g)) | |||
out.write(struct.pack('B', r)) | |||
out.write(struct.pack('B', a)) | |||
out.close() | |||
|} | |} |
Revision as of 02:06, 9 November 2013
Assignment: In Python, following the techniques shown in the Raw image and Raw image sequence examples, create a 1 second long animation by generating 25 frames of an image sequence and converting them to a GIF. Use just the basics: loops, variables, and if statements.
Rules:
- The code should be "pure" python (just using the standard libraries of python such as struct).
- The code should be no more than 50 lines.
- The images should be 320 x 240 pixels in size.
Results
To be posted / linked to here. Please post both your code (again, PURE python -- no libraries and less than 50 lines) + the resulting image (converted to GIF and uploaded to the wiki).
Luisa
import struct, sys
width = 320
height = 240
header = struct.pack("<BBBHHBHHHHBB",0,0,2,0,0,8,0,0,width,height,32,1<<5)
totalframes = 25
for frame in xrange(totalframes):
out = open("frame%04d.tga" % frame, "wb")
out.write(header)
for y in xrange(height):
for x in xrange(width):
r = (1.0-(float(frame)/totalframes))*255
g = 0
b = 0
a = 255
if y < 32 and y > 100:
a = 35
b = 125
if x > 80 and x < 160:
g = r
out.write(struct.pack('B', b))
out.write(struct.pack('B', g))
out.write(struct.pack('B', r))
out.write(struct.pack('B', a))
print r
out.close()
Lucia
import struct, sys
width = 320
height = 240
xWhite = 0
header = struct.pack("<BBBHHBHHHHBB",0,0,2,0,0,8,0,0,width,height,32,1<<5)
totalframes = 25
for frame in xrange(totalframes):
frame = frame+1
out = open("frames/frame%02d.tga" % frame, "wb")
out.write(header)
for y in xrange(height):
for x in xrange(width):
xWhite = xWhite +1
if (frame != 1 and xWhite%frame == 0):
r = 255
g = 255
b = 255
a = 255
else:
r = 0
g = 0
b = 0
a = 255
out.write(struct.pack('B', b))
out.write(struct.pack('B', g))
out.write(struct.pack('B', r))
out.write(struct.pack('B', a))
out.close()
A variation (that's actually the original idea - the previous code was 'accidental'):
import struct, sys
width = 320
height = 240
header = struct.pack("<BBBHHBHHHHBB",0,0,2,0,0,8,0,0,width,height,32,1<<5)
totalframes = 25
for frame in xrange(totalframes):
out = open("last/frame_%02d.tga" % frame, "wb")
out.write(header)
for y in xrange(height):
for x in xrange(width):
#if (x%10==0 and x == frame*10):
#line above is redundant either one condition or the other would be enough, so
if ( x == frame*10):
r = 255
g = 255
b = 255
a = 255
else:
r = 0
g = 0
b = 0
a = 255
out.write(struct.pack('B', b))
out.write(struct.pack('B', g))
out.write(struct.pack('B', r))
out.write(struct.pack('B', a))
out.close()