Raw image sequence: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:


<source lang="python">
<source lang="python">
# rawimagesequence.py
import struct, sys
import struct, sys


Line 28: Line 29:
     out.close()
     out.close()
</source>
</source>
python rawimagesequence.py
The outer loop of this script produces each of the 24 individual frames. Each frame has a different brightness calculated from full on (white) to full off (black). The frame variable is used to number the files using the special [http://docs.python.org/release/2.5.2/lib/typesseq-strings.html python string formatting operator] (%):
  print "hello%04d" % 23
  ==>
  hello0023


[[File:RawImageSequenceOutput.png]]
[[File:RawImageSequenceOutput.png]]


Using [[Imagemagick]] to convert this to an animated gif:
You can use [[Imagemagick]] (for instance) to convert this to an animated gif:
  convert frame* fadetoblack.gif
  convert frame* fadetoblack.gif


[[File:fadetoblack.gif]]
[[File:fadetoblack.gif]]

Latest revision as of 14:39, 5 November 2013

Following on from the Raw image example...

# 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
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 = r
            b = r
            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()
python rawimagesequence.py

The outer loop of this script produces each of the 24 individual frames. Each frame has a different brightness calculated from full on (white) to full off (black). The frame variable is used to number the files using the special python string formatting operator (%):

 print "hello%04d" % 23
 ==>
 hello0023

RawImageSequenceOutput.png

You can use Imagemagick (for instance) to convert this to an animated gif:

convert frame* fadetoblack.gif

Fadetoblack.gif