We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Help with PImage
Page Index Toggle Pages: 1
Help with PImage (Read 415 times)
Help with PImage
Apr 7th, 2008, 9:18pm
 
So -

Thanks in advance to everyone who takes a look!

I've written a program that draws some rectangles on the screen, using constrained randomness to make an attractive image.   Here's the code:



void setup() {
 size (screen.width, screen.height);
 smooth();
 background(255, 255, 255);
}



float mult = 0;
float multY = 0;
float posX = -900;
float posY = 0;
float bgcount = 0;

void draw() {
 frameRate (24);
 //background (155, 157, 255);
 if (mousePressed == true) {
   fill (155, 256, 155) ;
 }
 else {
   fill (240, 255, 155);
 }
 for (float i = 0; i < 10; i++) {
   noCursor();
   float drawX = mouseX;
   float drawY = mouseY;
   float x = random (1, 900);
   float color1 = random (120, 200);
   float color2 = random (60, 120);
   float color3 = random (180, 200);
   float coltransp = (random (20, 90));
   //stroke(color1, color2 ,255 );
   stroke(255);
   float rectwidth = (sin (random (1, 360))) * 150;
   fill (color1, color2, 255, coltransp);
   rect (posX + x, posY + x,  rectwidth, rectwidth*.7);
   //fill (100 + x + 30 * 1.02, 155, 255);
   //rect (i + x + 20, i, i+x, i);
   //stroke (100 + x, 120 + x, 255);
   //line (0, 0, mouseX, mouseY);
   mult = (mult + 15);
   multY = (multY + 1);
   bgcount = bgcount + 1;
   if (multY >= 400) {
     posX = posX + (random (1, 60));
     posY = random (-100, 0);
     mult = 0;
     multY = 0;

   }
   if (posX >= 1280) {
     posX = 0;
     posY = random (-100, 0);
     bgcount = random (1, 40);
   }
//if (bgcount >= 10) {
 //background (155, 157, 255);
 //bgcount = 0;
 //redraw ();
//}
 }
}

if (keyPressed = true) {
 noLoop();
 redraw ();
}

So now I want it to take color data from an array loaded with pixel data from the image.  Here's what I've got so far:


void setup() {
 size (screen.width, screen.height);
 smooth();
 background(255, 255, 255);
}

PImage img = loadImage("OliviaInMadrid.jpg");
int dimension;

for (int i=0; dimension; i+=2) {
 img.pixels[i] = color(0, 0, 0);
}
updatePixels();
image(img, 0, 0);`

float mult = 0;
float multY = 0;
float posX = -900;
float posY = 0;
float bgcount = 0;

void draw() {
 frameRate (24);
 //background (155, 157, 255);
 if (mousePressed == true) {
   fill (155, 256, 155) ;
 }
 else {
   fill (240, 255, 155);
 }
 for (float i = 0; i < 10; i++) {
   noCursor();
   float drawX = mouseX;
   float drawY = mouseY;
   float x = random (1, 900);
   float color1 = random (120, 200);
   float color2 = random (60, 120);
   float color3 = random (180, 200);
   float coltransp = (random (20, 90));
   //stroke(color1, color2 ,255 );
   stroke(255);
   float rectwidth = (sin (random (1, 360))) * 150;
   fill (color1, color2, 255, coltransp);
   rect (posX + x, posY + x,  rectwidth, rectwidth*.7);
   //fill (100 + x + 30 * 1.02, 155, 255);
   //rect (i + x + 20, i, i+x, i);
   //stroke (100 + x, 120 + x, 255);
   //line (0, 0, mouseX, mouseY);
   mult = (mult + 15);
   multY = (multY + 1);
   bgcount = bgcount + 1;
   if (multY >= 400) {
     posX = posX + (random (1, 60));
     posY = random (-100, 0);
     mult = 0;
     multY = 0;

   }
   if (posX >= 1280) {
     posX = 0;
     posY = random (-100, 0);
     bgcount = random (1, 40);
   }
//if (bgcount >= 10) {
 //background (155, 157, 255);
 //bgcount = 0;
 //redraw ();
//}
 }
}

if (keyPressed = true) {
 noLoop();
 redraw ();
}

It doesn't appear to do anything.  Does anyone have some insight?  This (that is, learning processing) is my first interaction with Java/programming since I was taught Pascal 8 years ago, in my freshman year of high school, and I'm having a hell of a time figuring out what's going on.  The image code, by the way, is straight off the reference.

Thanks again for all your help.
Re: Help with PImage
Reply #1 - Apr 7th, 2008, 10:04pm
 
Code:
 for (int i=0; dimension; i+=2) {  
img.pixels[i] = color(0, 0, 0);
}


That loop will never end, the middle part of the three is the "end condition", i.e. what must be satisfied for the loop to end.
So you need to do a test in there.. e.g. i<img.pixels.length or i<dimension or something.
Re: Help with PImage
Reply #2 - Apr 8th, 2008, 9:36am
 
Thanks for the help, John.

Nw it's telling me an image is not in the folder, while it most certainly <i>is</i>.  I'm looking at it.  What gives?  PImage is giving me huge headaches - I'm copying sketches directly from Fry/Reas' book, and they don't work either.  What am I missing?

Quote:


void setup() {
 size (screen.width, screen.height);
 smooth();

}

PImage img1 = loadImage("olivia.jpg");

for (int i=0; i >=image.width; i+=2) {
 img1.pixels[i] = color(0, 0, 0);
}
updatePixels();
float mult = 0;
float multY = 0;
float posX = -900;
float posY = 0;
float bgcount = 0;
println(6);
background(255);
void draw() {
 frameRate (24);
 //background (155, 157, 255);

 for (float i = 0; i < 10; i++) {
   noCursor();
   float drawX = mouseX;
   float drawY = mouseY;
   float x = random (1, 900);
   float color1 = random (120, 200);
   float color2 = random (60, 120);
   float color3 = random (180, 200);
   float coltransp = (random (20, 90));
   //stroke(color1, color2 ,255 );
   stroke(255);
   float rectwidth = (sin (random (1, 360))) * 150;
   fill (color1, color2, 255, coltransp);
   if (mousePressed == true) {
     fill (color1, color2, color3, coltransp) ;
   }
   else {
     fill (color2, color1, color3, coltransp);
   }
   rect (posX + x, posY + x,  rectwidth, rectwidth*.7);
   //fill (100 + x + 30 * 1.02, 155, 255);
   //rect (i + x + 20, i, i+x, i);
   //stroke (100 + x, 120 + x, 255);
   //line (0, 0, mouseX, mouseY);
   mult = (mult + 15);
   multY = (multY + 1);
   bgcount = bgcount + 1;
   if (multY >= 400) {
     posX = posX + (random (1, 60));
     posY = random (-100, 0);
     mult = 0;
     multY = 0;

   }
   if (posX >= 1280) {
     posX = 0;
     posY = random (-100, 0);
     bgcount = random (1, 40);
   }
   //if (bgcount >= 10) {
   //background (155, 157, 255);
   //bgcount = 0;
   //redraw ();
   //}
 }
}


Re: Help with PImage
Reply #3 - Apr 8th, 2008, 10:49am
 
The image has to be in a subdirectory of your sketch folder, called "data".

Also you really shouldn't have anything other than variable definitions outside of functions, so the start of your code should really change from this
Code:
void setup() {
size (screen.width, screen.height);
smooth();

}

PImage img1 = loadImage("olivia.jpg");

for (int i=0; i >=image.width; i+=2) {
img1.pixels[i] = color(0, 0, 0);
}
updatePixels();
float mult = 0;
float multY = 0;
float posX = -900;
float posY = 0;
float bgcount = 0;
println(6);
background(255);
void draw() {
frameRate (24);

to this
Code:

PImage img1;
float mult = 0;
float multY = 0;
float posX = -900;
float posY = 0;
float bgcount = 0;

void setup() {
size (screen.width, screen.height);
smooth();
img1 = loadImage("olivia.jpg");

//this need to be Less than, not greater than or equal, since
// it keeps running whilst the condition is true, I wasn't
// really clear earlier unfortunately.
for (int i=0; i <image.width; i+=2) {
img1.pixels[i] = color(0, 0, 0);
}
//need to do updatepixels on img1
img1.updatePixels();
println(6);
background(255);
frameRate(24);
}




void draw() {
//etc...

Re: Help with PImage
Reply #4 - Apr 8th, 2008, 10:57am
 
Its not a good idea to load the image outside a method. any time you use draw and setup only global variables should be declared outside a function, all other stuff has to be inside:

Code:


float mult, multY, posX, posY, bgcount;
PImage img1;

void setup() {
size (screen.width, screen.height);
smooth();
PImage img1 = loadImage("olivia.jpg");

for (int i=0; i >=image.width; i+=2) {
img1.pixels[i] = color(0, 0, 0);
}
img1.updatePixels();
float mult = 0;
float multY = 0;
float posX = -900;
float posY = 0;
float bgcount = 0;
background(255);
}


One note: its not necessarie to call frameRate() in the drawing loop cause its never change so its enough to call it once in the setup().

Mhmm, same as John but he was faster.
Re: Help with PImage
Reply #5 - Apr 8th, 2008, 4:14pm
 
Again, thank you all very much.

If any of you are in NYC you should come to the Fordham University at Lincoln Center theatre design and production design show.  There's some pretty baller work on the walls (and in the ears if I do say so myself - I'm a sound designer, usually...)  We open Thursday at 5 (I thin it's 5, at least...)  If anyone would like to come hit me with a message and I'll send you more deets.
Page Index Toggle Pages: 1