Wednesday, 8 April 2009

Mix Up

We learned in Chunk45 how we can use the mask method to create an image from two other images. Chunk46 images can be used to create a mask. Instead of using one mask we shall explore how we can use a different mask on each image in a folder of generated images.

We need to decide on the file type and the size of the images we would like to make. The size of our window, masks and images must be the same size, although they can be of different file types. We can use any sketch that loops through the draw method. If we examine the code for Chunk16 we can see, that a new image is generated and displayed, at the end of every iteration of draw. We will therefore use Chunk16 as an example. As a new image is displayed at the end of draw it would make sense to place the save statement there. We initialize an integer as a global variable so its value can be referenced outside of draw. We increment the integer by one at the start of draw and at the end of draw we just add a save statement e.g.:
save("name"+n+".gif");
Where name is the name of the file each image will have followed by a sequential number n. We can increase the frame rate and use the exit method to close Chunk16 when it has generated the required amount of images. We would add an if statement to control how many images were generated like so:
if(n==20)exit();
Next we perform the same process on the Chunk 46 code to produce the same amount of images, changing the name so the images wont be overwritten when moved.
Next we write a simple sketch to use the Chunk46 images as a mask on the Chunk 16 images. (or vica versa)

Here is the code for the masking program:
int n=0; void setup(){ size(700, 700); frameRate(.2); } void draw(){ n++; if(n>50)n=0; PImage img = loadImage("c"+n+".jpg"); PImage i= loadImage("line"+n+".gif"); //PImage img= loadImage("line20.gif"); image(i, 0, 0, 700, 700); filter(GRAY); loadPixels(); img.mask(pixels); image(img, 0, 0, 700, 700); save("mix"+n+".gif"); }

As you can see it uses the code we have just discussed to save a copy of each masked image. If you add an image to the sketch then save it, a data folder will be created, where we can copy the images in Chunk16 and Chunk 46 to.

The new images will be saved in the new sketch's folder.


1 comment:

  1. Hi Rosie

    Really like some of the images you are getting with your lines. Noted a comment you posted on MartinP's blog re. frame rate. Writing as someone who knows very little about Processing and not a whole lot about Java: if Processing works like it could do in native code Windows with one thread there will be a timer in there that calls draw() and so executes everything in it so many times per second. If it takes longer to do the stuff in draw() than the frame rate then this maybe has the capability of messing everything up - although it wouldn't do in properly written native code because the draw() caller would disable the timer before calling draw() and enable it again after draw() returned. A frame rate of 0.2 means allow 5 seconds to execute draw(), sounds about right for slow stuff like reading and writing largish files.

    Hope this makes sense. Any comments, particularly critical, on my blog would be gratefully received.

    Martin

    ReplyDelete