I am writing a plugin which needs to process all of the image files in a folder. To save myself having to manage filenames, I'm using Open next, so my code looks something like this:
<User opens an image> <User starts the plugin> <User uses plugin to change image> //At this point, I need to save the changed image without overwriting the initial data. //I'm trying to do something like: FileInfo fileInfo = Image.getOriginalFileInfo(); IJ.save(image, fileInfo.directory + "Processed versions\\" + fileInfo.fileName); //Now I move on to the next image in the folder, using the line IJ.run("Open Next"); <User uses plugin to change next image> <Continue until I get back to the first image> The problem is, the IJ.save resets the current folder, so when the Open Next runs, it is now pointing at the wrong folder. I can see several possible solutions: Save the processed images in the same folder as the originals (but then the Open Next will start picking them up and presenting them to the user!). Find a way to save which doesn't reset the current folder (looked around, but didn't see one. Anybody know of one?). Reset the current folder somehow so that the Open Next will find the correct next file. Or scrap this approach and try another, probably going through the hassle of reading the folder, building a list of files in it, figuring out which ones are actually images, and then opening the next one myself (seems a shame to do all this when Open Next is out there). I found a promising looking function in the ImageJ API documention. IJ.getDirectory will tell me where I am. Unfortunately, there seems to be no corresponding IJ.setDirectory which would allow me to reset it in the program. Several other approaches all caused dialog boxes to pop up. If I knew what variable IJ.getDirectory was reading, I might be able to reset it directly, but I haven't managed to find that in the documentation, either. I suspect there are others who've done something like this, and I'm missing an easier way. Thanks to anyone who can offer any suggestions. |
Hi Bob,
ij.io.OpenDialog has public static getDefaultDirectory() and setDefaultDirectory(String defaultDir) methods. You can use these (even in a macro, by the 'call' command). Michael ________________________________________________________________ On 4 Mar 2010, at 01:10, Bob Loushin wrote: > I am writing a plugin which needs to process all of the image files > in a folder. To save myself having to manage filenames, I'm using > Open next, so my code looks something like this: > > <User opens an image> > <User starts the plugin> > <User uses plugin to change image> > > //At this point, I need to save the changed image without > overwriting the initial data. > //I'm trying to do something like: > FileInfo fileInfo = Image.getOriginalFileInfo(); > IJ.save(image, fileInfo.directory + "Processed versions\\" + > fileInfo.fileName); > > //Now I move on to the next image in the folder, using the line > IJ.run("Open Next"); > > <User uses plugin to change next image> > <Continue until I get back to the first image> > > The problem is, the IJ.save resets the current folder, so when the > Open Next runs, it is now pointing at the wrong folder. > > I can see several possible solutions: Save the processed images in > the same folder as the originals (but then the Open Next will start > picking them up and presenting them to the user!). Find a way to > save which doesn't reset the current folder (looked around, but > didn't see one. Anybody know of one?). Reset the current folder > somehow so that the Open Next will find the correct next file. Or > scrap this approach and try another, probably going through the > hassle of reading the folder, building a list of files in it, > figuring out which ones are actually images, and then opening the > next one myself (seems a shame to do all this when Open Next is out > there). > > I found a promising looking function in the ImageJ API documention. > IJ.getDirectory will tell me where I am. Unfortunately, there seems > to be no corresponding IJ.setDirectory which would allow me to > reset it in the program. Several other approaches all caused dialog > boxes to pop up. If I knew what variable IJ.getDirectory was > reading, I might be able to reset it directly, but I haven't > managed to find that in the documentation, either. > > I suspect there are others who've done something like this, and I'm > missing an easier way. Thanks to anyone who can offer any suggestions. |
In reply to this post by Bob Loushin
Michael,
Thanks for your suggestion. Unfortunately, it isn't a fix, but in trying it I realized what is actually happening . It turns out that Open Next isn't using the DefaultDirectory, as I thought. It's actually looking at the top open image, and using the FileInfo directly from that instead. When I resave the modified file in a different directory, its path changes, and Open Next looks in its new directory. Once I figured that out I realized it wasn't going to work the way I hoped. Instead, I managed to dig the code for Open Next out of the browsable source and hack a version of it which accepts FileInfo (saved from the original file, before the file was modified and resaved) passed into it instead of extracting it from the top open image. This lives inside my class, and will work fine, although it isn't ideal from a code maintanence standpoint. So I got it working. I'm still wondering if/how others have solved this problem in a more elegant way within the more common paths of ImageJ (my way is definitely inelegant and off the beaten path). Bob ----- Original Message ----- From: "Michael Schmid" <[hidden email]> To: [hidden email] Sent: Thursday, March 4, 2010 11:52:13 AM GMT -06:00 US/Canada Central Subject: Re: Resetting focus of Open Next Hi Bob, ij.io.OpenDialog has public static getDefaultDirectory() and setDefaultDirectory(String defaultDir) methods. You can use these (even in a macro, by the 'call' command). Michael ________________________________________________________________ On 4 Mar 2010, at 01:10, Bob Loushin wrote: > I am writing a plugin which needs to process all of the image files > in a folder. To save myself having to manage filenames, I'm using > Open next, so my code looks something like this: > > <User opens an image> > <User starts the plugin> > <User uses plugin to change image> > > //At this point, I need to save the changed image without > overwriting the initial data. > //I'm trying to do something like: > FileInfo fileInfo = Image.getOriginalFileInfo(); > IJ.save(image, fileInfo.directory + "Processed versions\\" + > fileInfo.fileName); > > //Now I move on to the next image in the folder, using the line > IJ.run("Open Next"); > > <User uses plugin to change next image> > <Continue until I get back to the first image> > > The problem is, the IJ.save resets the current folder, so when the > Open Next runs, it is now pointing at the wrong folder. > > I can see several possible solutions: Save the processed images in > the same folder as the originals (but then the Open Next will start > picking them up and presenting them to the user!). Find a way to > save which doesn't reset the current folder (looked around, but > didn't see one. Anybody know of one?). Reset the current folder > somehow so that the Open Next will find the correct next file. Or > scrap this approach and try another, probably going through the > hassle of reading the folder, building a list of files in it, > figuring out which ones are actually images, and then opening the > next one myself (seems a shame to do all this when Open Next is out > there). > > I found a promising looking function in the ImageJ API documention. > IJ.getDirectory will tell me where I am. Unfortunately, there seems > to be no corresponding IJ.setDirectory which would allow me to > reset it in the program. Several other approaches all caused dialog > boxes to pop up. If I knew what variable IJ.getDirectory was > reading, I might be able to reset it directly, but I haven't > managed to find that in the documentation, either. > > I suspect there are others who've done something like this, and I'm > missing an easier way. Thanks to anyone who can offer any suggestions. |
Hi Bob,
sorry, I mistook it; you were talking about 'Open Next', I was talking about the 'Open...' Dialog. OpenDialog.get/setDefaultDirectory is for The 'Open...' and 'Save...' dialogs, and there is is really helpful. For 'Open Next', another workaround might be keeping the original file and saving a copy, which is closed after saving. And you could also hack the directory in the fileinfo, by cloning the fileinfo before saving and writing back the clone therafter. Best wishes, Michael ________________________________________________________________ On 4 Mar 2010, at 23:43, Bob Loushin wrote: > Michael, > > > > Thanks for your suggestion. Unfortunately, it isn't a fix, but in > trying it I realized what is actually happening . It turns out > that Open Next isn't using the DefaultDirectory, as I thought. > It's actually looking at the top open image, and using the FileInfo > directly from that instead. When I resave the modified file in a > different directory, its path changes, and Open Next looks in its > new directory. Once I figured that out I realized it wasn't going > to work the way I hoped. Instead, I managed to dig the code for > Open Next out of the browsable source and hack a version of it > which accepts FileInfo (saved from the original file, before the > file was modified and resaved) passed into it instead of extracting > it from the top open image. This lives inside my class, and will > work fine, although it isn't ideal from a code maintanence standpoint. > > > > So I got it working. I'm still wondering if/how others have solved > this problem in a more elegant way within the more common paths of > ImageJ (my way is definitely inelegant and off the beaten path). > > > > Bob > > > ----- Original Message ----- > From: "Michael Schmid" > > Hi Bob, > > ij.io.OpenDialog has public static getDefaultDirectory() and > setDefaultDirectory(String defaultDir) methods. > > You can use these (even in a macro, by the 'call' command). > > Michael > ________________________________________________________________ > > On 4 Mar 2010, at 01:10, Bob Loushin wrote: > >> I am writing a plugin which needs to process all of the image files >> in a folder. To save myself having to manage filenames, I'm using >> Open next, so my code looks something like this: >> >> <User opens an image> >> <User starts the plugin> >> <User uses plugin to change image> >> >> //At this point, I need to save the changed image without >> overwriting the initial data. >> //I'm trying to do something like: >> FileInfo fileInfo = Image.getOriginalFileInfo(); >> IJ.save(image, fileInfo.directory + "Processed versions\\" + >> fileInfo.fileName); >> >> //Now I move on to the next image in the folder, using the line >> IJ.run("Open Next"); >> >> <User uses plugin to change next image> >> <Continue until I get back to the first image> >> >> The problem is, the IJ.save resets the current folder, so when the >> Open Next runs, it is now pointing at the wrong folder. >> >> I can see several possible solutions: Save the processed images in >> the same folder as the originals (but then the Open Next will start >> picking them up and presenting them to the user!). Find a way to >> save which doesn't reset the current folder (looked around, but >> didn't see one. Anybody know of one?). Reset the current folder >> somehow so that the Open Next will find the correct next file. Or >> scrap this approach and try another, probably going through the >> hassle of reading the folder, building a list of files in it, >> figuring out which ones are actually images, and then opening the >> next one myself (seems a shame to do all this when Open Next is out >> there). >> >> I found a promising looking function in the ImageJ API documention. >> IJ.getDirectory will tell me where I am. Unfortunately, there seems >> to be no corresponding IJ.setDirectory which would allow me to >> reset it in the program. Several other approaches all caused dialog >> boxes to pop up. If I knew what variable IJ.getDirectory was >> reading, I might be able to reset it directly, but I haven't >> managed to find that in the documentation, either. >> >> I suspect there are others who've done something like this, and I'm >> missing an easier way. Thanks to anyone who can offer any >> suggestions. |
Free forum by Nabble | Edit this page |