We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › PImage very slow with web images
Page Index Toggle Pages: 1
PImage very slow with web images (Read 711 times)
PImage very slow with web images
Jan 26th, 2008, 4:06am
 
Hello,

Sorry for this stupid question but I've a strange problem :
I need a slideshow for a project and I use web images but it's incredibly slow :(
I use something simple like :
PImage myImg = loadImage("http://flickr.com/image.jpg");

but it takes about 4 s for a 500x500px image. When I try to use a stupid javascript slideshow maker, for loading the same files it's instantaneous or near. Why ??
The worst of that is during the loading, processing doesn't respond any more, and can't even draw a loading bar.
It doesn't respond too when I load a XML file with the proXML library, and it isn't very fast too.
Can anyone help me ?
thank you very much :)

Re: PImage very slow with web images
Reply #1 - Jan 26th, 2008, 7:29am
 
Quote:
The worst of that is during the loading, processing doesn't respond any more, and can't even draw a loading bar.


You can use Java Threads. Loading your images in another thread would allow you to display a progress bar or do anything you want while the images are loading.

Code:
import java.lang.Thread;

class LoadingImageThread extends Thread {
public void run() {
// <-- put the loading code here
PImage img = loadImage(...);
}
}


Then :
Code:
LoadingImageThread loading = new LoadingImageThread();
loading.start();
// <-- it won't wait until the image is loaded to continue
Re: PImage very slow with web images
Reply #2 - Jan 27th, 2008, 11:53am
 
OK thank you very much !
I'll try that, it will resolve a lot of problems Smiley

but I'm still surprised of the long time of loading images..

thank you again.
Re: PImage very slow with web images
Reply #3 - Feb 2nd, 2008, 6:34pm
 
Most JavaScript slideshow code includes something to "pre-fetch" the next few images so they appear to load instantly.  If you post a link to the JavaScript code you're using I'd be happy to point out where that's happening and you can try the same technique in your Processing sketch.
Re: PImage very slow with web images
Reply #4 - Feb 19th, 2008, 8:19pm
 
where should be the code locate?

LoadingImageThread loading = new LoadingImageThread();
loading.start();
// <-- it won't wait until the image is loaded to continue
Re: PImage very slow with web images
Reply #5 - Mar 1st, 2008, 8:57pm
 
Ok I tried this and it works very well, two much well Smiley
I have a lot of pictures to load from the web, and it does it too much fast, so I think, it doesn't have the time to load it...
for example, I tried this :

for(int i=0;i<counter;i++){
images[i]=loadImage("http://server.com/"+i+".jpg");
}

it doesn't work at all Sad
I think that the for loop is too fast so I tried with a counter :

counter=0;
images[counter]=loadImage("http://server.com/"+counter+".jpg");
if (counter<limit) counter++;

But it doesn't work too. When I want to show the images, I can often see a null pointer exception.
How can I be sure that the image was loaded ? :/
thank you !
Re: PImage very slow with web images
Reply #6 - Mar 2nd, 2008, 2:32am
 
The documentation for loadImage (http://processing.org/reference/loadImage_.html) indicates:

Quote:
If an image is not loaded successfully, the null value is returned and an error message will be printed to the console. The error message does not halt the program, however the null value may cause a NullPointerException if your code does not check whether the value returned from loadImage() is null.


Check your Java console for error messages that might help you debug.  Your code can also check for null values to be sure it doesn't attempt to draw an image that didn't load.

Your for loop looks fine, so if the error messages don't help, try printing out the URLs your code is generating and see if you can get them via a web browser.  Also make sure you have placed the code block in setup() as indicated in the loadImage() documentation or in a separate thread as antiplastik indicated earlier.
Page Index Toggle Pages: 1