We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to safely load an image from the web into a PImage in Processing 3. I tried to adapt the try-catch example from the website but IOExceptions was not the right kind of exception, I tried with a general Exception but that doesn't seem to work either. How would I find the right kind of exception to try to catch? Is the following snippet the right thing (other than the exception kind or are there better ways to check for errors like this one?
PImage webImg;
String urls[];
void setup() {
size(200,200);
urls = new String[2];
// working URL
urls[0] = "https://"+"processing.org/img/processing-web.png";
// bad URL
urls[1] = "https://"+"**********.org/img/processing-web.png";
}
void draw() {
background(0);
try {
webImg = loadImage(urls[frameCount%2], "png");
} catch (Exception e) {
//e.printStackTrace();
webImg = null;
println("Error!");
}
if (webImg != null) {
println("It Worked!");
image(webImg,0,0);
}
}
Answers
What exception is happening that you're trying to guard against?
This is what I get when I try to fetch an image from *****.org: (the code i posted should be copy pastable except for the urls being formatted by the forum)
Don't load images in draw. Do it in setup.
Calling loadImage, OVER THE INTERNET, 60 times a second is bad.
Okay, so it sounds like you need to catch
UnknownHostException
.However, your general
Exception
catch should have caught this, so something is off with your code. What did you expect to happen? What happened instead?That was going to be my follow up, this loads a webcam image in real time over wifi-LAN.
I know processing doesn't do multithreading, is there any way to do this in an asynchronous way?
The image is always being updated in real time, i can't pre-load it
Good call @koogs. Definitely fix that problem before trying to figure out your try-catch logic.
I really can't pre-load the images, please read previous comment
You're loading 60 images per second. After 10 seconds you're going to have 600 requests out to the internet. Something is going to break. You need to fix this problem, because it's going to interfere with any other problems you're having.
Doing this asynchronously is going to make the problem worse, not better.
Your example can use a simple
for
loop to load the images insetup()
. Surely you aren't loading an infinite number of images, are you?This is a small recreation of the issue, my sketch does a lot more. I just want the catch execution block to run, and from my console output it doesn't seem like it is. furthermore i would not expect the error message to be displayed if the exception is caught. Is that just my assumption?
My original sketch is loading small compressed images from a server in the same LAN. It's only using one at the time so it's overwriting the same PImage. It's only doing this for 5 seconds every minute. It will be running 24/7 for a couple of weeks as part of an installation. While I'd like to hear more about the possible ways this could break right now my most pressing concern is how to catch this exception.
Please could I get some help with the issue I asked about?
here pretend i posted this:
Thanks for the improved example.
From the reference:
The stack trace is coming from inside Processing. Look at the
loadImage()
function here.In this case,
loadImage()
will complete but returnnull
. So instead of using a catch block to detect this case, just check whether the image isnull
.From the comments in the code it seems that requestImage is the async version of loadImage right?
Should I use that instead?
there's requestImage also
https://processing.org/reference/requestImage_.html
but looking at the source of loadImage there's already a try catch around the loading code
https://github.com/processing/processing/blob/e83ae2054a3d3c52b1663385673fdfb26abcc96b/core/src/processing/core/PApplet.java#L5412
so there is no exception.
but you could clone that code...
so something like this would work?
The best thing to do is just try it. What happened when you ran that code?
It worked but i mean is this the ideal solution for this case scenario? Does it allocate loads of ram? Should I explicitly invoke the garbage collector after a fixed number of frames? any suggestion since it needs to run for two weeks?
Like we said before, the biggest problem is loading images from inside the
draw()
function. At the very least, you should slow down your FPS so this isn't firing 60 times per second.Other than that, the best thing you can do is run it for a while and monitor the memory usage. Use a profiler and leave it running overnight or something.