📜 ⬆️ ⬇️

Create moving pictures with Processing

image

On Habré there is an article how to get sinegraphs with the help of a free program from Microsoft. I was interested in this topic and I decided to write a short sketch for Processing language. What kind of programming language can be read here . Such moving pictures are a set of several dozen frames, in which most of the pixels are transparent. Only areas with a moving object remain opaque in all images. The first frame is displayed completely, it is the background.

To get the desired animation, you need to edit the alpha channel of each frame. As the original images for the script, I used a series of snapshots of candles. After running the script, only one fixed background frame will appear on the screen. Click on the image in the place where you want to see the animation. The algorithm works in real time. To save the animation, press the "s" key. The folder “out” will appear in the folder with the sketch, it contains the frames of the output animation. Next, load the Gimp, open the resulting images as layers and save them in gif format, as an animation. Here is the source code of the script.
')
// Zurbaganin // 2012 int nFrames =15; //   PImage[] imgs = new PImage[nFrames]; //   PImage brush=new PImage (63, 63, RGB);//      - void setup() { background(255); createBrush();//     - size(1024, 768, P3D);//   frameRate(25); //   for (int k = 0; k < imgs.length; k++) { String imgName = nf(k, 2) + ".png";//      imgs[k] = loadImage(imgName);//    imgs[k].format=ARGB;//   32-  } nullAlpha();////      } void draw() { int frame = frameCount % (nFrames);//           image(imgs[frame], 0, 0);//     } void createBrush() //      { for (int i = 0; i < brush.height; i++) { for (int j = 0; j < brush.width; j++) { float gr; float a=dist(brush.width/2, brush.height/2, j, i); gr=92*(1-a/dist(brush.width/4, brush.height/4, 0, 0)); brush.pixels[i*brush.width+j]=color(gr); } } } void mousePressed() //    . //         { if (mouseButton == LEFT) { for (int k = 1; k < imgs.length; k++) { for (int i = 0; i < brush.height; i++) { for (int j = 0; j < brush.width; j++) { int i1=mouseY-brush.height/2+i; int j1=mouseX-brush.height/2+j; float r=(int)red(imgs[k].pixels[i1*imgs[k].width+j1]); float g=(int)green(imgs[k].pixels[i1*imgs[k].width+j1]); float b=(int)blue(imgs[k].pixels[i1*imgs[k].width+j1]); float a=(int)alpha(imgs[k].pixels[i1*imgs[k].width+j1]); a=a+(int)red(brush.pixels[i*brush.width+j]); if (a>255) { a=255; } // float a=255; imgs[k].pixels[i1*imgs[k].width+j1]=color(r, g, b, a); } } } } } void nullAlpha()//      { for (int k = 1; k < imgs.length; k++) { for (int i = 0; i < imgs[k].height; i++) { for (int j = 0; j < imgs[k].width; j++) { float r=(int)red(imgs[k].pixels[i*imgs[k].width+j]); float g=(int)green(imgs[k].pixels[i*imgs[k].width+j]); float b=(int)blue(imgs[k].pixels[i*imgs[k].width+j]); float a=(int)alpha(imgs[k].pixels[i*imgs[k].width+j]); a=0;//     imgs[k].pixels[i*imgs[k].width+j]=color(r, g, b, a); } } } } void keyPressed() { if (key == 's' || key == 'S') {//      for (int k = 1; k < imgs.length; k++) { for (int i = 0; i < imgs[k].height; i++) { for (int j = 0; j < imgs[k].width; j++) { float r, g, b; float a=(int)alpha(imgs[k].pixels[i*imgs[k].width+j]); if (a==0) { r=(int)red(imgs[0].pixels[i*imgs[0].width+j]); g=(int)green(imgs[0].pixels[i*imgs[0].width+j]); b=(int)blue(imgs[0].pixels[i*imgs[0].width+j]); } else { r=(int)red(imgs[k].pixels[i*imgs[k].width+j]); g=(int)green(imgs[k].pixels[i*imgs[k].width+j]); b=(int)blue(imgs[k].pixels[i*imgs[k].width+j]); } a=255; // float a=255; imgs[k].pixels[i*imgs[k].width+j]=color(r, g, b, a); } } } for (int k = 0; k < imgs.length; k++) { imgs[k].save("/out/"+nf(k, 6)+".png"); } } } 


For normal operation of the script, the original images must be saved in the format of png 1024x768 pixels and moved to the sketch directory. File names should be: “01.png, 02.png ... **. Png”. Specify your number of frames in the code using the nFrames parameter.

This video demonstrates the work program.
Processing is free software, does not require installation, works fine under Linux and Windows.

Source: https://habr.com/ru/post/140411/


All Articles