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 & HelpSyntax Questions › loadImage : possible with local relative address
Page Index Toggle Pages: 1
loadImage : possible with local relative address ? (Read 1495 times)
loadImage : possible with local relative address ?
Nov 9th, 2009, 5:54pm
 
Hello,

I would like to know whether it is possible to load an image from a different folder than data folder but not from a distant server ?
I would like export an app (with an array in the code) and would have the possibility later to replace images in that "external folder".
I'm working on Mac : is there a specific local URL to write ?


Sorry for my poor english skills.

Thanks in advance.

f
Re: loadImage : possible with local relative address ?
Reply #1 - Nov 9th, 2009, 8:37pm
 
sf,

Do you want to open an image file on the local computer where the processing code runs on? In this case, try selectInput(). It opens a select file dialog so you may select a file.
Re: loadImage : possible with local relative address ?
Reply #2 - Nov 10th, 2009, 2:17am
 
You can just put an absolute path in loadImage().
Re: loadImage : possible with local relative address ?
Reply #3 - Nov 10th, 2009, 4:34am
 
Thank you liudr and PhiLho !
As I would like to load an image among an array I will use the selectFolder() for the first part of the path.
Re: loadImage : possible with local relative address ?
Reply #4 - Nov 10th, 2009, 3:12pm
 
Sf,

So you're loading a folder full of images and store in an array of PImage? In this case you may use selectFolder() to get the folder path. Here's my code to get file names from a directory. If you just want to load one image, use selectInput() to pick a file. It returns the full path of the file, which you may use in loadImage().


 String fp=selectFolder();
 File file = new File(fp);
 String names[];
 if (file.isDirectory())
 {
   names = file.list();// This extract all file names in the folder
 }
 
 else
 {
   return false;
 }


I also have some code that will tell whether a file is an image file. If you need that, let me know so I can send to you.
Re: loadImage : possible with local relative address ?
Reply #5 - Nov 11th, 2009, 9:30am
 
Thank you very much liudr !

Originally I wanted to pick at random an image and a sound from a folder to record them into a movie playing together with FTT minim but now knowing the selectInput() changes the logic I'm building the app with.

Nevertheless, no doubt your selectFolder() script will help me very much for futur development.

I'm still fighting with Ftt synchronisation part but indeed I'm interested by the code checking the correct type of the choosen file anf it would be very kind of you if you could post it.

Thanks a lot.
Re: loadImage : possible with local relative address ?
Reply #6 - Nov 11th, 2009, 2:07pm
 
Here you go. It's a hodgepodge of string comparison and regular expression, reflecting my inability to use regular expression for the entire job. On the other hand, the function is easy to understand for fellows with my level of basic understanding.

The isPicture() returns true if the file name has the right extension as a picture file. You can easily adapt it to your sound files. This routine needs to call extension() which returns the extension of a file regardless what file it is.

To use this, get the user to select a file with selectFolder() or selectInput(). Then call the isPicture() with a file name. If it returns true, open it, it's a picture file. If the function returns false, it's not a picture file, time for revenge ^_^

boolean isPicture(String name)
{
 name=extension(name).toLowerCase();
 if (name.equals(".png")||name.equals(".jpg")||name.equals(".bmp")||name.equals(".gi
f")||name.equals(".tga")||name.equals(".jpeg")) return true;
 else return false;
}


String extension(String name)
{  
 String[] result = match(name,"\\.[a-zA-Z]{1,}?$");
 String[] result2={""};
 if (result==null) result=result2;
 return result[0];
}


Re: loadImage : possible with local relative address ?
Reply #7 - Nov 12th, 2009, 1:52am
 
liudr, while your regular expression is correct, it isn't optimal. You reject extensions with numbers (which is OK here, but not generic), you use {1,} which can be replaced with a simple +, the ? isn't really necessary.
You could have written also "\\.(?:png|gif|jpe?g|tiff?|tga)" for example.
But well, even if I love REs' flexibility, the usual way to write your filter is to use a simpler and perhaps faster (but speed is quite irrelevant here!) string method:
Code:
boolean isPicture(String name)
{
 String ext = name.toLowerCase();
 return ext.endsWith("png") || ext.endsWith(".gif") ||
ext.endsWith(".jpg") || ext.endsWith(".jpeg") ||
ext.endsWith(".bmp") || ext.endsWith(".tga");
}

Smiley
Re: loadImage : possible with local relative address ?
Reply #8 - Nov 14th, 2009, 9:13am
 
Thanks a lot !
I'm not at ease with return Keyword; I will never find by myself.
Page Index Toggle Pages: 1