When image is loaded do something

Hi I need some help with an “if” line.
i want to know if i can so something like this:

if(image is loaded)
{
 b = true;
 } else {
 b = false;
}

if (b==true)
{
 statement
}

if (b== false)
{
 statement
}

the problem is i don’t know how to write “if a picture is loaded”

When you are loading your picture, just add a boolean behind the statement that goes like isLoaded = true; and then use that as variable or use the variable with you picture, lets say it is img and do something like if (img != null) {}

And please don’t move the topics back to the wrong cathegory, after someone (only people with Trust level 3 can move others topics) moved your question to the right cathegory.

One method is to set the image to null, then check if it is null after each loadImage operation.

PImage img = null;

img = loadImage("DoesNotExist.png");

if(img!=null){
  println("miracle image");
} else {
  println("unloaded image");
}

img = loadImage("https://processing.org/img/processing3-logo.png");

if(img!=null){
  println("expected image");
} else {
  println("website down!");
}

Output:

The file “DoesNotExist.png” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

unloaded image
expected image

3 Likes