Prototyping raw image sequence assignment (2013)

From XPUB & Lens-Based wiki
Revision as of 23:03, 28 November 2013 by Elleke Hageman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 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()

Color.gif

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()

Lucia gif.gif

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()

Frames lucia.gif



Tamas

Plasma.gif
import math, struct, sys
from math import *
tau = 2 * pi

#computes distance from (x1,y1) to (x2,y2)
def dist(x1, y1, x2, y2):
    return sqrt(float((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)))

width = 320
height = 240
nFrames = 25
header = struct.pack("<BBBHHBHHHHBB",0,0,2,0,0,8,0,0,width,height,32,1<<5)

for frame in xrange(nFrames):
    output = open("frames/frame%02d.tga" % frame, "wb")
    output.write(header)
 
    for y in xrange(height):
        for x in xrange(width):
            offset = frame * (1.0 / nFrames)
            primaryColor = abs(sin((0.2)*tau + (x) / (4.0+8.0*sin((0.2)*pi)))
                + cos((0.2)*tau + (x+y) / (6.0+16.0*cos((0.2)*tau)))
                + sin(dist(x, y, 32*sin(offset*tau)+96, 32*cos(offset*tau)+height / 2) / 24.0) # orbit 1 on left side 
                + sin(dist(x, y, 32*cos(offset*tau) + width-96, 32*sin(offset*tau)+height / 2) / 8.0) # orbit 2 on right side
                )
            r = abs(primaryColor - abs(cos(dist(x + sin(offset*tau), y + 25*sin(offset*tau), width / 2, -2000) / 4.0))) * 48
            g = primaryColor * 48 if primaryColor >= 2 else primaryColor * 36 if primaryColor > 0.2 else abs(cos(5.0*dist(x + sin(offset*tau), y + 25*sin(offset*tau), width / 2, -2000) / 4.0)) * 24
            b = abs(primaryColor + abs(cos(2000*offset*tau + dist(x + sin(offset*tau), y + sin(offset*tau), width / 2, -2000) / 8.0))) * 48
            a = 255

            output.write(struct.pack('BBBB', b, g, r, a))

    output.close()

________________________________________________________________________________________________________________________________________________

Mihail

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()




Lídia

import struct, sys
import random
 
width = 320
height = 240

header = struct.pack("<BBBHHBHHHHBB", 0, 0, 2, 0, 0, 8, 0, 0, width, height, 32, 1 << 5)
 
totalFrames = 25
largeColumns = (width/5) - 25
smallColumns = 25

 
for frame in xrange(totalFrames):
    out = open("frame%04d.tga" % frame, "wb")
    out.write(header)
    attempt = random.randint(height/7, height/1.2)
    attempt1 = random.randint(10,90)
    cor = random.randint(55,255)
    for y in xrange(height):
        for x in xrange(5):
            for n in xrange(largeColumns):
                r, g, b, a = 0, 0, 0, 255
                if y <= attempt:
                    r, g = 55, 255
                    b = (frame + 1) * 10
                else:
                    r = 155
                out.write(struct.pack('B', b))
                out.write(struct.pack('B', g))
                out.write(struct.pack('B', r))
                out.write(struct.pack('B', a))
 
            for x in xrange(smallColumns):                                                                                                                                                           
                r, g, b, a = 0, 0, 0, 255
                if y < attempt1:
                    b = 155
                if y > attempt1 and y < 200:
                    b = cor 
                else:
                    b = 90
                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()

Lidiaprototyping.gif Lidiaprototypinglitch.gif Lidiaprototypingglitch.gif

Details

max

Stripes.gif

 

import struct, sys
import os 
out = sys.stdout
width = 320
height = 240
ysquare = 0
header = struct.pack("<BBBHHBHHHHBB",0,0,2,0,0,8,0,0,width,height,32,1<<5)
out.write(header)


totalframes = 25
for frame in xrange(totalframes):
    frame = frame+1
    filename = ("frame%02d.tga" % frame )
    out = open(filename, "wb")
    out.write(header)
    for y in xrange(height):
        for x in xrange(width):
            ysquare = ysquare +1
            if (frame != 1 and ysquare%frame == 0):
                r = 0
                b = 0 
                g = 0 
                a = 0

                if y <= 240 and x %2:
                    r = 255
                    b = 255
                    g = 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()

Group - Lucia, Luisa, Lídia, Mihail

Idea: draw several white diagonals on black background. The diagonals should move across the background and be made not of continuous lines, but of dashed lines.

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 (y%10>=0 and y%10<5 ):
                r = 0
                g = 0
                b = 0
                a = 255
            else:
                if (xWhite%11 == 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()

Output-dashed-diagonals.gif

Nikos (w. Michael)

# rawimagesequence.py
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
 def map(x, startend, newstartnewend):
   start, end = startend
   newstart, newend = newstartnewend
   p = (float(x) - start) / (end - start)
   return newstart + (p * (newend-newstart))
   
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):
           r =  map(frame, (0, totalframes), (255, 0))
           g =  map(frame, (0, totalframes), (0, 255))
           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))
   print r

   out.close()
   
import os
os.system("convert frame* nik.gif")

hkUE09l.gif

Elleke

import struct, sys, random
 
width = 320
height = 240
header = struct.pack("<BBBHHBHHHHBB",0,0,2,0,0,8,0,0,width,height,32,1<<5)
 
totalframes = 25
r = 0
g = 0
b = 0
a = 255
 
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 = random.randint(0, 30)          
            g = random.randint(0, 30)          
            b = random.randint(0, 30)  

        

         
           
    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