# Can't load images before setup()

**URL:** <https://discourse.processing.org/t/cant-load-images-before-setup/31998>\
**Category:** Processing\
**Created:** [August 29, 2021, 8:30pm UTC](https://discourse.processing.org/t/cant-load-images-before-setup/31998 "2021-08-29T20:30:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [August 29, 2021, 8:30pm UTC](https://discourse.processing.org/t/cant-load-images-before-setup/31998/1 "2021-08-29T20:30:42Z")

</div>

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

```auto
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);
}

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e42109cf1d5c309e9dd4e32140e46fb2dc5b74f5.png)  
and so on.  
Is the issue that I didn’t save the sketch yet? Dunno

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [August 29, 2021, 8:59pm UTC](https://discourse.processing.org/t/cant-load-images-before-setup/31998/2 "2021-08-29T20:59:40Z")

</div>

Hi @CodeMasterX,

> [@CodeMasterX](#):
>
> Hi, I am not sure if this is a bug or a feature.

From the [documentation](https://processing.org/reference/loadImage_.html):

> 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…)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 29, 2021, 9:09pm UTC](https://discourse.processing.org/t/cant-load-images-before-setup/31998/3 "2021-08-29T21:09:58Z")

</div>

Hello,

> [@CodeMasterX](#):
>
> Basically, you cannot load files before setup().

You must define the path.

> [@josephh](#):
>
> setting the sketch path?

I suspected this as well…

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b743b534c30858fe9093ea56fa66d9b9952a0b80.png)

`:)`

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 30, 2021, 10:35pm UTC](https://discourse.processing.org/t/cant-load-images-before-setup/31998/4 "2021-08-30T22:35:07Z")

</div>

> [@CodeMasterX](#):
>
> Basically, you cannot load files before **setup()**.

Use **settings()** instead:

> **[settings() / Reference](https://processing.org/reference/settings_.html)**
>
> The settings() function is new with Processing 3.0. It's not needed in most sketches. It's only useful when it's absolutely necessary to define the parameters to size() with a variable. …

> [@josephh](#):
>
> But I don’t know if Processing enforces that or…

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.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 1, 2021, 8:16pm UTC](https://discourse.processing.org/t/cant-load-images-before-setup/31998/5 "2021-09-01T20:16:07Z")

</div>

> [@CodeMasterX](#):
>
> Is the issue that I didn’t save the sketch yet? Dunno

> [@josephh](#):
>
> setting the sketch path?

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:

```auto
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:

```auto
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:

```auto
println(sketchPath());

```

You will see an output such as:

```auto
/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.

```auto
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.”
