Can't load images before setup()

Hi, I am not sure if this is a bug or a feature. Basically, you cannot load files before setup().
Example code

PImage img = loadImage("clrPic.jpg");
int p[][];
int v[][];
void setup() {
  size(600, 800);
  p = new int[width][height];
  v = new int[width][height];
}
void draw() {
  background(0);
  //image(img, 0, 0);
}


and so on.
Is the issue that I didn’t save the sketch yet? Dunno

1 Like

Hi @CodeMasterX,

From the documentation:

In most cases, load all images in setup() to preload them at the start of the program. Loading images inside draw() will reduce the speed of a program. Images cannot be loaded outside setup() unless they’re inside a function that’s called after setup() has already run.

So this is definitely not a bug. But I don’t know if Processing enforces that or it cannot load an image because the setup function does something in particular (setting the sketch path? As stated in the error…)

2 Likes

Hello,

You must define the path.

I suspected this as well…

It works with a path set and an image to load:

:)

1 Like

Use settings() instead:

It’s enforced and used to work on old versions of Processing.

The reason they’ve arbitrarily decided to throw a RuntimeException for it is b/c sketchPath isn’t initialized until settings() callback.

However full paths don’t depend on sketchPath and this “rule” is artificially being applied for it as well.

2 Likes

Just to elaborate – load images and set sketch size in setup() or settings(), as described above. However, this is a designed limitation for full sketches with setup() etc. – it isn’t a limitation related to the sketch path itself. Unsaved sketches are just sketches in a temporary directory that have not yet been renamed and moved into the main file path, and immediate mode sketches without setup() can always load images just fine, whether or they are saved or “unsaved” (which is really just located in a temporary directory).

Try opening a new, unsaved sketch window, enter this snippet of code and run without saving:

PImage img = loadImage("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/Processing_3_logo.png/600px-Processing_3_logo.png");
img.resize(100,100);
image(img, 0, 0);

The remote image will load and display, even in an “unsaved” sketch.

Now download that image and put the absolute local path in the sketch (not relative). For example:

PImage img = loadImage("/Users/jeremydouglass/Downloads/600px-Processing_3_logo.png");
img.resize(100,100);
image(img, 0, 0);

Again, the image displays in an “unsaved” sketch.

Finally, create a new unsaved sketch and run a single line in it:

println(sketchPath());

You will see an output such as:

/var/folders/zq/fkhfd8b97hj4w137b5l9mkrh0000gn/T/untitled591735022134215726sketches/sketch_210901b

That is the sketch location and name before it is saved. Open that folder and copy the image inside. Then try loading the local image from the “unsaved” sketch with a relative path.

println(sketchPath());
PImage img = loadImage("600px-Processing_3_logo.png");
img.resize(100,100);
image(img, 0, 0);

The image loads fine, even from an unsaved sketch in a tmp folder.

So: this is a Processing requirement for how a sketch with setup() etc. loads resources – it doesn’t have anything to do with sketch paths or a sketch being “unsaved.”

1 Like