Generative collage error

Hello, when I run this code I get this message. There are only 16 images in the sketch and they are in the data/images folder.

loading…
32images loaded!
The file data/images/._04.jpg contains bad image data, or may not be an image.
IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
int amount = int(random(1, 10));

PGraphics[] layers = new PGraphics[amount];
PGraphics[] masks = new PGraphics[amount];

import java.io.File;
String [] imageFileNames;
StringList images;

void setup() {
  size(1200, 900);

  java.io.File folder = new java.io.File(dataPath("images"));
  imageFileNames = folder.list();
  images = new StringList();

  println("loading…");
  // Load the image-files and push thems to imagess-arrayList
  for (int i = 0; i < imageFileNames.length; i++) {
    String filename = imageFileNames[I];
    if (filename.indexOf("Store") == -1) {
      images.push("data/images/"+filename);
    }
  }

  println(images.size() + "images loaded!");

  for (int i = 0; i < layers.length; i++) {
    layers[i] = createGraphics(width, height);
    masks[i] = createGraphics(width, height);
  }
}

PGraphics l, m;
PImage img;
void draw() {

  amount = int(random(3, 30));

  background(0);
  //blendMode(SCREEN);
  for (int i = 0; i < layers.length; i++) {
    l = layers[I];
    img = loadImage(images.get(int(random(images.size()))));
    img.resize(width, 0);
    l.beginDraw();
    l.clear();
    l.push();
    l.image(img, 0, 0);
    l.pop();
    l.endDraw();

    m = masks[I];
    m.beginDraw();
    m.background(#FFFFFF);
    m.noStroke();
    m.fill(0);
    m.beginShape();
    for (int j = 0; j < 12; j++) {
      float x = random(-width, width*2);
      float y = random(-height, height*2);
      m.curveVertex(x, y);
    }
    m.endShape();

    if (random(1) < 0.1) {
      m.circle(random(width), random(height), random(width/2));
    }
    m.endDraw();

    l.mask(m);
    image(l, 0, 0);
  }
  // saveFrame("out/###.png");
}

It looks like one of your filenames is an empty string ("") try changing this to

  for (int i = 0; i < imageFileNames.length; i++) {
    String filename = imageFileNames[i];   // You had I instead of i
    if (filename.length() > 0 && filename.indexOf("Store") == -1) {
      images.push("data/images/"+filename);
    }
  }

Obviously I can’t guarentee this works because I can’t execute the code but I hope it works :smile:

1 Like

There’s a much simpler way to get all file paths within a folder using listPaths():

1 Like

Hello, I am not sure why it said [I] because in my sketch it says small i. However, the code you provided did not work. Oh well….

This is the new error:

The method length() in the type String is not applicable for the arguments (int)

See reference:

Example:

:)

1 Like

I don’t know how to fix this.

So it looks like the code runs if the sketch is on the desktop of my iMac, but not when it’s on an external drive.

Sorry about that it was a typo :smile:: and I have corrected my post.

1 Like
  1. Do you get an error message?
  2. Have you checked the images file path is correct relative to your sketch code?
1 Like