Wednesday 22 October 2008

Animated gifs

Just spent a while developing a very simple first app that processes a photograph, and generates an animated gif, where the photo is revealed as a set of scanlines.

Imagine my annoyance when I discover that this blogsite doesn't allow animated gifs in a profile! At least for me, it only displays the first frame, so I've removed it.

One useful thing I discovered - when downloading a new library (in this case gifAnimation), you need to exit & restart the IDE after you've copied the library folder to the right place.

For those who care, here's my code:


import gifAnimation.*; // needs gifAnimation library from http://www.extrapixel.ch/processing/gifAnimation/

PImage a;
int blobwidth;
int blobheight;
int y;
int x;
GifMaker gifExport;

void setup()
{
a = loadImage("pict5124a.jpg"); // name of image to scan
size(a.width,a.height);
noStroke();
background(0); // black background to prevent flashing between loops
smooth();
blobwidth = width;
blobheight = height / 15;
y=0;
x=0;

gifExport = new GifMaker(this, "export.gif");
gifExport.setRepeat(0); // make it an "endless" animation
}

void draw()
{
fill(0, 0, 0, 64); // fade existing lines by overwriting with black. Last parameter = 0 for no fade; 255 for maximum fade)
rect(0,0,width, height); // this is the actual overwrite
PImage blob = a.get(x, y, blobwidth, blobheight); // get blob
image(blob, x, y); // and write to screen
x = x + blobwidth; //scan horizontally
if (x >= width)
{
x = 0;
y = y + blobheight; // scan vertically
if (y >= height)
{
y = 0;
gifExport.finish(); // finalise the gif after one run through
}
}
gifExport.addFrame();
}

And here's me:

animated me

(edited to correct obiwan error)

1 comment:

Anonymous said...

Thanks, James, got the library and the program works perfectly!