Trying to get script running over the whole image array but stops after one image

edited April 2018 in Questions about Code

I am trying to get this code to run over all images in the array, but it just stops after the first image, i added a j = j+1 after the noLoop(); and the print(f) shows that it gets incremented. Any help would be appreciated my head about to get hot.

/* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/31404*@* */
/* !do not delete the line above, required for linking your tweak if you re-upload */
/*
    pixel knitting 
    (c) Pierre Commenge / emoc*codelab.fr
    27 juillet 2011

    feed with fresh pictures only!
    (uncomment at the end of draw() to save frames)
*/

// Free software: you can redistribute this program and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

//PImage img;
boolean first = true;
int i;
int j;
float im, imax, imin;
float lasty, x, y;
String ts;

// parameters (play here)
float ANGLE_SEED = 13;     // 13 / 257 / 77
float amod = ANGLE_SEED;
float amod_inc = 0.5; // angle change for a new line
boolean MODIFY_ANGLE = true;
float smod = 0.7; // size modifier


String[]imageNames = {"sp-00.jpg", "sp-01.jpg", "sp-02.jpg", "sp-03.jpg"};
PImage[]images = new PImage[imageNames.length];

void setup() {
  frameRate(250);

    for (int j=0; j < imageNames.length; j++) {
    String imageName = imageNames[j];
    images[j] = loadImage(imageName);
 }

  //size(img.width, img.height);
  size(1280, 720); 
  imax = images[j].width * images[j].height;
  imin = 0;
  smooth();
}

void draw() {


  if (first) {
  image(images[j], 0, 0);
  first = false;
  }

  images[j].loadPixels();


  im += 800;
  if (im > imax) im = imax;

  for (i = int(imin); i < im; i++) {
    x = i%images[j].width;
    y = floor(i/images[j].width);
    if (MODIFY_ANGLE && (lasty != y)) amod += amod_inc;

    color cc = images[j].pixels[i]; 
    stroke(cc);
    fill(cc);
    float bri = brightness(images[j].pixels[i]);
    float sat = saturation(images[j].pixels[i]);
    if (bri > 245) draw_lines(bri, x, y, amod - x/3, smod);
    else if (bri < 10) draw_lines_dark(bri, x, y, amod - x/3, smod);
    else {
      strokeWeight(sat / 15); 
      float a = (360.0 / 255.0) * sat + amod; 
      float x2 = x + bri/4 * random(0.7,1) * smod * cos(radians(a)); 
      float y2 = y + bri/4 * random(0.7,1) * smod * sin(radians(a));
      line(x, y, x2, y2);
    }
    if ((sat > 200) && (bri > 150)) { //  
      draw_circle(sat, x, y, amod, smod);
    }
    lasty = y;
  }
  imin = im;

  if (im == imax) {
    // saveFrame("image_" + hour() + minute() + second() + ".png"); // UNCOMMENT !
    noLoop();
    print(j);

    j = j+1;
    //print(j);

  }

}

void drawImage() {

}

void draw_lines(float s, float x, float y, float amod, float smod) {
  int im = int((s - 245) / 5);
  for (int i=0; i < im; i++) {
    float b = random(amod, amod+90);
    float l = im * random(10 * smod, 40 * smod);
    float x2 = x + l * cos(radians(b));
    float y2 = y + l * sin(radians(b));
    strokeWeight(( 1 - (0.5 * im)) * smod);
    line(x, y, x2, y2);
  }
}

void draw_lines_dark(float s, float x, float y, float amod, float smod) {
  int im = int(s / 2);
  for (int i=0; i < im; i++) {
    float b = random(amod, amod+90);
    float l = im * random(10 * smod, 40 * smod);
    float x2 = x + l * cos(radians(b));
    float y2 = y + l * sin(radians(b));
    strokeWeight(( 1 - (0.2 * im)) * smod);
    line(x, y, x2, y2);
  }
}

void draw_circle(float s, float x, float y, float amod, float smod) {
  float dia = random(s / 50) * smod;
  float ll = random(s / 3.0) * smod;
  float ang = random(360); 
  float x2 = x + ll * cos(radians(ang));
  float y2 = y + ll * sin(radians(ang));
  strokeWeight(1);
  ellipse(x2, y2, dia, dia);
}

Answers

  • size should be the first line in setup

    Get rid of noLoop

    line 46 is refering to the 0th image always, not to the last one

    Try line 96 after line 99

  • Changed code as suggested. Now i get a ArrayIndexOutOfBoundsException: 4

    See code below

    /* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/31404*@* */
    /* !do not delete the line above, required for linking your tweak if you re-upload */
    /*
        pixel knitting 
        (c) Pierre Commenge / emoc*codelab.fr
        27 juillet 2011
    
        feed with fresh pictures only!
        (uncomment at the end of draw() to save frames)
    */
    
    // Free software: you can redistribute this program and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    
    //PImage img;
    boolean first = true;
    int i;
    int j;
    float im, imax, imin;
    float lasty, x, y;
    String ts;
    
    // parameters (play here)
    float ANGLE_SEED = 13;     // 13 / 257 / 77
    float amod = ANGLE_SEED;
    float amod_inc = 0.5; // angle change for a new line
    boolean MODIFY_ANGLE = true;
    float smod = 0.7; // size modifier
    
    
    String[]imageNames = {"sp-00.jpg", "sp-01.jpg", "sp-02.jpg", "sp-03.jpg"};
    PImage[]images = new PImage[imageNames.length];
    
    void setup() {
      size(1280, 720); 
      frameRate(250);
    
        for (int j=0; j < imageNames.length; j++) {
        String imageName = imageNames[j];
        images[j] = loadImage(imageName);
     }
    
      //size(img.width, img.height);
      imax = images[j].width * images[j].height;
      imin = 0;
      smooth();
    }
    
    void draw() {
    
    
      if (first) {
      image(images[j], 0, 0);
      first = false;
      }
    
      images[j].loadPixels();
    
    
      im += 800;
      if (im > imax) im = imax;
    
      for (i = int(imin); i < im; i++) {
        x = i%images[j].width;
        y = floor(i/images[j].width);
        if (MODIFY_ANGLE && (lasty != y)) amod += amod_inc;
    
        color cc = images[j].pixels[i]; 
        stroke(cc);
        fill(cc);
        float bri = brightness(images[j].pixels[i]);
        float sat = saturation(images[j].pixels[i]);
        if (bri > 245) draw_lines(bri, x, y, amod - x/3, smod);
        else if (bri < 10) draw_lines_dark(bri, x, y, amod - x/3, smod);
        else {
          strokeWeight(sat / 15); 
          float a = (360.0 / 255.0) * sat + amod; 
          float x2 = x + bri/4 * random(0.7,1) * smod * cos(radians(a)); 
          float y2 = y + bri/4 * random(0.7,1) * smod * sin(radians(a));
          line(x, y, x2, y2);
        }
        if ((sat > 200) && (bri > 150)) { //  
          draw_circle(sat, x, y, amod, smod);
        }
        lasty = y;
      }
      imin = im;
    
      if (im == imax) {
        // saveFrame("image_" + hour() + minute() + second() + ".png"); // UNCOMMENT !
        //noLoop();
        print(j);
    
      }
    
        j = j+1;
        print(j);
    
    }
    
    void drawImage() {
    
    }
    
    void draw_lines(float s, float x, float y, float amod, float smod) {
      int im = int((s - 245) / 5);
      for (int i=0; i < im; i++) {
        float b = random(amod, amod+90);
        float l = im * random(10 * smod, 40 * smod);
        float x2 = x + l * cos(radians(b));
        float y2 = y + l * sin(radians(b));
        strokeWeight(( 1 - (0.5 * im)) * smod);
        line(x, y, x2, y2);
      }
    }
    
    void draw_lines_dark(float s, float x, float y, float amod, float smod) {
      int im = int(s / 2);
      for (int i=0; i < im; i++) {
        float b = random(amod, amod+90);
        float l = im * random(10 * smod, 40 * smod);
        float x2 = x + l * cos(radians(b));
        float y2 = y + l * sin(radians(b));
        strokeWeight(( 1 - (0.2 * im)) * smod);
        line(x, y, x2, y2);
      }
    }
    
    void draw_circle(float s, float x, float y, float amod, float smod) {
      float dia = random(s / 50) * smod;
      float ll = random(s / 3.0) * smod;
      float ang = random(360); 
      float x2 = x + ll * cos(radians(ang));
      float y2 = y + ll * sin(radians(ang));
      strokeWeight(1);
      ellipse(x2, y2, dia, dia);
    }
    
  • if (j < imageNames.length) j++;

  • where would i put this line?

  • Instead of j++;

  • Honestly I am not at home and can’t run your code

    So i am not fully sure what you want to achieve

  • I would like it to run over the whole images in the array one by one. It does not do that, either it does one image and stops or it just dont do anything

  • Answer ✓

    I worked it out, the loop stopped because i didn't set imax to 0 after the processing loop finished.

    Below the final code

    /* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/31404*@* */
    /* !do not delete the line above, required for linking your tweak if you re-upload */
    /*
        pixel knitting 
        (c) Pierre Commenge / emoc*codelab.fr
        27 juillet 2011
    
        feed with fresh pictures only!
        (uncomment at the end of draw() to save frames)
    */
    
    // Free software: you can redistribute this program and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    
    //PImage img;
    boolean first = true;
    int i;
    int j;
    float im, imax, imin;
    float lasty, x, y;
    String ts;
    
    // parameters (play here)
    float ANGLE_SEED = 13;     // 13 / 257 / 77
    float amod = ANGLE_SEED;
    float amod_inc = 0.5; // angle change for a new line
    boolean MODIFY_ANGLE = true;
    float smod = 0.7; // size modifier
    
    
    String[]imageNames = {"sp-00.jpg", "sp-01.jpg", "sp-02.jpg", "sp-03.jpg"};
    PImage[]images = new PImage[imageNames.length];
    
    void setup() {
        size(1280, 720); 
        frameRate(250);
    
        for (int j=0; j < imageNames.length; j++) {
        String imageName = imageNames[j];
        images[j] = loadImage(imageName);
     }
    
      //size(img.width, img.height);
      imax = images[j].width * images[j].height;
      imin = 0;
      smooth();
    }
    
    void draw() {
    
    
      if (first) {
      image(images[j], 0, 0);
      first = false;
      }
    
      images[j].loadPixels();
    
    
      im += 800;
      if (im > imax) im = imax;
    
      for (i = int(imin); i < im; i++) {
        x = i%images[j].width;
        y = floor(i/images[j].width);
        if (MODIFY_ANGLE && (lasty != y)) amod += amod_inc;
    
        color cc = images[j].pixels[i]; 
        stroke(cc);
        fill(cc);
        float bri = brightness(images[j].pixels[i]);
        float sat = saturation(images[j].pixels[i]);
        if (bri > 245) draw_lines(bri, x, y, amod - x/3, smod);
        else if (bri < 10) draw_lines_dark(bri, x, y, amod - x/3, smod);
        else {
          strokeWeight(sat / 15); 
          float a = (360.0 / 255.0) * sat + amod; 
          float x2 = x + bri/4 * random(0.7,1) * smod * cos(radians(a)); 
          float y2 = y + bri/4 * random(0.7,1) * smod * sin(radians(a));
          line(x, y, x2, y2);
        }
        if ((sat > 200) && (bri > 150)) { //  
          draw_circle(sat, x, y, amod, smod);
        }
        lasty = y;
      }
      imin = im;
    
      if (im == imax) {
        //saveFrame("image_" + hour() + minute() + second() + ".png"); // UNCOMMENT !
        saveFrame("processed_" + imageNames[j] + ".png"); // UNCOMMENT !
        // noLoop();
        print(j);
    
        if (j < imageNames.length) j++;
        im = 0;
        print(j);
    
        if (j == imageNames.length) {
        noLoop();
        }
      }
    
    }
    
    void drawImage() {
    
    }
    
    void draw_lines(float s, float x, float y, float amod, float smod) {
      int im = int((s - 245) / 5);
      for (int i=0; i < im; i++) {
        float b = random(amod, amod+90);
        float l = im * random(10 * smod, 40 * smod);
        float x2 = x + l * cos(radians(b));
        float y2 = y + l * sin(radians(b));
        strokeWeight(( 1 - (0.5 * im)) * smod);
        line(x, y, x2, y2);
      }
    }
    
    void draw_lines_dark(float s, float x, float y, float amod, float smod) {
      int im = int(s / 2);
      for (int i=0; i < im; i++) {
        float b = random(amod, amod+90);
        float l = im * random(10 * smod, 40 * smod);
        float x2 = x + l * cos(radians(b));
        float y2 = y + l * sin(radians(b));
        strokeWeight(( 1 - (0.2 * im)) * smod);
        line(x, y, x2, y2);
      }
    }
    
    void draw_circle(float s, float x, float y, float amod, float smod) {
      float dia = random(s / 50) * smod;
      float ll = random(s / 3.0) * smod;
      float ang = random(360); 
      float x2 = x + ll * cos(radians(ang));
      float y2 = y + ll * sin(radians(ang));
      strokeWeight(1);
      ellipse(x2, y2, dia, dia);
    }
    
  • Well done

Sign In or Register to comment.