Problem with FileSaver

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with FileSaver

christoph@boerlin.de
Dear ImageJ Community,

i"ve got a problem with the FileSaver Class, i want to write a plugin which is
able to save an Tiff Image in an chosen Directory.

Here's my example code:

public class My_Plugin2 implements PlugIn {

    public void run(String arg) {
    ImagePlus SourceImage = WindowManager.getCurrentImage();
    DirectoryChooser dc=new DirectoryChooser("Choose Save Directory");
    String dir=dc.getDirectory();

    new FileSaver(SourceImage).saveAsTiff(dir);


}
}

But every time i get a Error:

File Saver

An error occured writing the file.

Path (Acces is denied)


I have the right the write at this Path, it's on my own Hard disk, and from the
ImageJ Menu I'm able to save the image at this Path.

Why couldn't I save the Image via the plugin?

Thanks for your help.

Christoph

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with FileSaver

Michael Schmid
Hi Christoph,

as I understand it, the 'path' in Filesaver.saveAsTiff(String path) should include directory AND filename. If you use only a directory name, it would overwrite the directory with a file, and it seems that java (or the operating system) is clever enough to refuse this.

You could try
 new FileSaver(SourceImage).saveAsTiff(dir+File.separator+SourceImage.getTitle());

Michael
________________________________________________________________
On Jan 29, 2013, at 18:00, [hidden email] wrote:

> Dear ImageJ Community,
>
> i"ve got a problem with the FileSaver Class, i want to write a plugin which is
> able to save an Tiff Image in an chosen Directory.
>
> Here's my example code:
>
> public class My_Plugin2 implements PlugIn {
>
>   public void run(String arg) {
>   ImagePlus SourceImage = WindowManager.getCurrentImage();
>   DirectoryChooser dc=new DirectoryChooser("Choose Save Directory");
>   String dir=dc.getDirectory();
>
>   new FileSaver(SourceImage).saveAsTiff(dir);
>
>
> }
> }
>
> But every time i get a Error:
>
> File Saver
>
> An error occured writing the file.
>
> Path (Acces is denied)
>
>
> I have the right the write at this Path, it's on my own Hard disk, and from the
> ImageJ Menu I'm able to save the image at this Path.
>
> Why couldn't I save the Image via the plugin?
>
> Thanks for your help.
>
> Christoph

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with FileSaver

Michael Schmid
In reply to this post by christoph@boerlin.de
Hi Christoph,

your longer error message is not worse than the one without giving the filename - it's just longer  :-)  You have got a good bit further in processing the file, but there is something strange: ImageJ thinks that it should write 8-bit data (a byte array), but your data are something else.

This might be a bug in ImageJ - to help fixing it, can you please tell us what image type you have? (e.g. what does 'Show Info' tell?). How was the image created?

--
Anyhow, I have never used the FileSaver directly; the more convenient method to write a file is
  IJ.saveAs(ImagePlus imp, String format, String path)
where format is the String "tiff" or "tif"
or
  IJ.saveAsTiff(ImagePlus imp, String path)

(again, 'path' should include the directory and the filename)


Michael
________________________________________________________________
On Jan 29, 2013, at 19:18, [hidden email] wrote:

> Hi Michael,
>  
> thanks for your fast reply.
>  
> I already tried it with a full path include file name and file ending but then i get this exception:
>  
> java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [B
>      at ij.io.ImageWriter.write(ImageWriter.java:271)
>      at ij.io.TiffEncoder.write(TiffEncoder.java:119)
>      at ij.io.TiffEncoder.write(TiffEncoder.java:139)
>      at ij.io.FileSaver.saveAsTiff(FileSaver.java:110)
>      at My_Plugin2.run(My_Plugin2.java:32)
>      at fiji.scripting.java.PlugInExecutor.tryRun(PlugInExecutor.java:143)
>      at fiji.scripting.java.PlugInExecutor.tryRun(PlugInExecutor.java:104)
>      at fiji.scripting.java.PlugInExecutor.run(PlugInExecutor.java:85)
>      at fiji.scripting.java.PlugInExecutor.run(PlugInExecutor.java:79)
>      at fiji.scripting.java.PlugInExecutor.run(PlugInExecutor.java:75)
>      at fiji.scripting.java.Refresh_Javas.runOutOfTreePlugin(Refresh_Javas.java:436)
>      at fiji.scripting.java.Refresh_Javas.compileAndRun(Refresh_Javas.java:122)
>      at fiji.scripting.java.Refresh_Javas.runScript(Refresh_Javas.java:71)
>      at fiji.scripting.TextEditor$19.execute(TextEditor.java:1967)
>      at fiji.scripting.TextEditor$Executer$1.run(TextEditor.java:1776)
>  
> So it's even worse than without the filename ...
>  
> Christoph
>  
>
> Michael Schmid <[hidden email]> hat am 29. Januar 2013 um 18:36 geschrieben:
> > Hi Christoph,
> >
> > as I understand it, the 'path' in Filesaver.saveAsTiff(String path) should include directory AND filename. If you use only a directory name, it would overwrite the directory with a file, and it seems that java (or the operating system) is clever enough to refuse this.
> >
> > You could try
> > new FileSaver(SourceImage).saveAsTiff(dir+File.separator+SourceImage.getTitle());
> >
> > Michael
> > ________________________________________________________________
> > On Jan 29, 2013, at 18:00, [hidden email] wrote:
> >
> > > Dear ImageJ Community,
> > >
> > > i"ve got a problem with the FileSaver Class, i want to write a plugin which is
> > > able to save an Tiff Image in an chosen Directory.
> > >
> > > Here's my example code:
> > >
> > > public class My_Plugin2 implements PlugIn {
> > >
> > > public void run(String arg) {
> > > ImagePlus SourceImage = WindowManager.getCurrentImage();
> > > DirectoryChooser dc=new DirectoryChooser("Choose Save Directory");
> > > String dir=dc.getDirectory();
> > >
> > > new FileSaver(SourceImage).saveAsTiff(dir);
> > >
> > >
> > > }
> > > }
> > >
> > > But every time i get a Error:
> > >
> > > File Saver
> > >
> > > An error occured writing the file.
> > >
> > > Path (Acces is denied)
> > >
> > >
> > > I have the right the write at this Path, it's on my own Hard disk, and from the
> > > ImageJ Menu I'm able to save the image at this Path.
> > >
> > > Why couldn't I save the Image via the plugin?
> > >
> > > Thanks for your help.
> > >
> > > Christoph
> >

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with FileSaver

christoph@boerlin.de
Hi Michael,

thank you for your held,  if found my mistake, because the method
saveAsTiff() is for Tiff and Tiff Stacks, i thought saveAsTiff(path)
also works for Tiff and Tiff Stacks, but its only for Tiffs and for
Stacks i have to use saveAsTiffStack(path), with that it works as it should.

Nevertheless I'm still looking for a method to implement the Save as
Image Sequence from the Menu,
but without any manual Input at that time.
The Class StackWriter isn't really helpfull for me, because it only
shows the settings Menu and waits for manual input.
Is there a method to save a TiffStack as seperate Images, where  i could
set all neccesarry Settings like Name, Directory, StartingNumber, Number
of Digits, etc direct in the plugin?

Christoph




Am 29.01.2013 19:37, schrieb Michael Schmid:

> Hi Christoph,
>
> your longer error message is not worse than the one without giving the filename - it's just longer  :-)  You have got a good bit further in processing the file, but there is something strange: ImageJ thinks that it should write 8-bit data (a byte array), but your data are something else.
>
> This might be a bug in ImageJ - to help fixing it, can you please tell us what image type you have? (e.g. what does 'Show Info' tell?). How was the image created?
>
> --
> Anyhow, I have never used the FileSaver directly; the more convenient method to write a file is
>    IJ.saveAs(ImagePlus imp, String format, String path)
> where format is the String "tiff" or "tif"
> or
>    IJ.saveAsTiff(ImagePlus imp, String path)
>
> (again, 'path' should include the directory and the filename)
>
>
> Michael
> ________________________________________________________________
> On Jan 29, 2013, at 19:18, [hidden email] wrote:
>
>> Hi Michael,
>>  
>> thanks for your fast reply.
>>  
>> I already tried it with a full path include file name and file ending but then i get this exception:
>>  
>> java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [B
>>       at ij.io.ImageWriter.write(ImageWriter.java:271)
>>       at ij.io.TiffEncoder.write(TiffEncoder.java:119)
>>       at ij.io.TiffEncoder.write(TiffEncoder.java:139)
>>       at ij.io.FileSaver.saveAsTiff(FileSaver.java:110)
>>       at My_Plugin2.run(My_Plugin2.java:32)
>>       at fiji.scripting.java.PlugInExecutor.tryRun(PlugInExecutor.java:143)
>>       at fiji.scripting.java.PlugInExecutor.tryRun(PlugInExecutor.java:104)
>>       at fiji.scripting.java.PlugInExecutor.run(PlugInExecutor.java:85)
>>       at fiji.scripting.java.PlugInExecutor.run(PlugInExecutor.java:79)
>>       at fiji.scripting.java.PlugInExecutor.run(PlugInExecutor.java:75)
>>       at fiji.scripting.java.Refresh_Javas.runOutOfTreePlugin(Refresh_Javas.java:436)
>>       at fiji.scripting.java.Refresh_Javas.compileAndRun(Refresh_Javas.java:122)
>>       at fiji.scripting.java.Refresh_Javas.runScript(Refresh_Javas.java:71)
>>       at fiji.scripting.TextEditor$19.execute(TextEditor.java:1967)
>>       at fiji.scripting.TextEditor$Executer$1.run(TextEditor.java:1776)
>>  
>> So it's even worse than without the filename ...
>>  
>> Christoph
>>  
>>
>> Michael Schmid <[hidden email]> hat am 29. Januar 2013 um 18:36 geschrieben:
>>> Hi Christoph,
>>>
>>> as I understand it, the 'path' in Filesaver.saveAsTiff(String path) should include directory AND filename. If you use only a directory name, it would overwrite the directory with a file, and it seems that java (or the operating system) is clever enough to refuse this.
>>>
>>> You could try
>>> new FileSaver(SourceImage).saveAsTiff(dir+File.separator+SourceImage.getTitle());
>>>
>>> Michael
>>> ________________________________________________________________
>>> On Jan 29, 2013, at 18:00, [hidden email] wrote:
>>>
>>>> Dear ImageJ Community,
>>>>
>>>> i"ve got a problem with the FileSaver Class, i want to write a plugin which is
>>>> able to save an Tiff Image in an chosen Directory.
>>>>
>>>> Here's my example code:
>>>>
>>>> public class My_Plugin2 implements PlugIn {
>>>>
>>>> public void run(String arg) {
>>>> ImagePlus SourceImage = WindowManager.getCurrentImage();
>>>> DirectoryChooser dc=new DirectoryChooser("Choose Save Directory");
>>>> String dir=dc.getDirectory();
>>>>
>>>> new FileSaver(SourceImage).saveAsTiff(dir);
>>>>
>>>>
>>>> }
>>>> }
>>>>
>>>> But every time i get a Error:
>>>>
>>>> File Saver
>>>>
>>>> An error occured writing the file.
>>>>
>>>> Path (Acces is denied)
>>>>
>>>>
>>>> I have the right the write at this Path, it's on my own Hard disk, and from the
>>>> ImageJ Menu I'm able to save the image at this Path.
>>>>
>>>> Why couldn't I save the Image via the plugin?
>>>>
>>>> Thanks for your help.
>>>>
>>>> Christoph

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with FileSaver

Michael Schmid
Hi Christoph,

you can try Plugins>Macros>Record, and select 'Plugin' output.
It will give you something like

IJ.run("Image Sequence... ", "format=TIFF name=myFileName start=0 digits=4 save=myDisk/myDirectory");

Besides the
  IJ.run(String command, String options)
method created by recording, and using the current foreground image, there is also a method
  IJ.run(ImagePlus imp, String command, String options)
This one takes an ImagePlus as an argument, so you can select which image to process, even if it is not shown on the screen.

You have to create the options String with filename, directory, etc. by String concatenation. If the filename or directory may contain spaces, put them in square brackets, e.g. save=[myDisk/my directory]

Michael
________________________________________________________________
On Jan 29, 2013, at 22:16, Christoph Börlin wrote:

> Hi Michael,
>
> thank you for your held,  if found my mistake, because the method saveAsTiff() is for Tiff and Tiff Stacks, i thought saveAsTiff(path) also works for Tiff and Tiff Stacks, but its only for Tiffs and for Stacks i have to use saveAsTiffStack(path), with that it works as it should.
>
> Nevertheless I'm still looking for a method to implement the Save as Image Sequence from the Menu,
> but without any manual Input at that time.
> The Class StackWriter isn't really helpfull for me, because it only shows the settings Menu and waits for manual input.
> Is there a method to save a TiffStack as seperate Images, where  i could set all neccesarry Settings like Name, Directory, StartingNumber, Number of Digits, etc direct in the plugin?
>
> Christoph
>
>
>
>
> Am 29.01.2013 19:37, schrieb Michael Schmid:
>> Hi Christoph,
>>
>> your longer error message is not worse than the one without giving the filename - it's just longer  :-)  You have got a good bit further in processing the file, but there is something strange: ImageJ thinks that it should write 8-bit data (a byte array), but your data are something else.
>>
>> This might be a bug in ImageJ - to help fixing it, can you please tell us what image type you have? (e.g. what does 'Show Info' tell?). How was the image created?
>>
>> --
>> Anyhow, I have never used the FileSaver directly; the more convenient method to write a file is
>>   IJ.saveAs(ImagePlus imp, String format, String path)
>> where format is the String "tiff" or "tif"
>> or
>>   IJ.saveAsTiff(ImagePlus imp, String path)
>>
>> (again, 'path' should include the directory and the filename)
>>
>>
>> Michael
>> ________________________________________________________________
>> On Jan 29, 2013, at 19:18, [hidden email] wrote:
>>
>>> Hi Michael,
>>>  thanks for your fast reply.
>>>  I already tried it with a full path include file name and file ending but then i get this exception:
>>>  java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [B
>>>      at ij.io.ImageWriter.write(ImageWriter.java:271)
>>>      at ij.io.TiffEncoder.write(TiffEncoder.java:119)
>>>      at ij.io.TiffEncoder.write(TiffEncoder.java:139)
>>>      at ij.io.FileSaver.saveAsTiff(FileSaver.java:110)
>>>      at My_Plugin2.run(My_Plugin2.java:32)
>>>      at fiji.scripting.java.PlugInExecutor.tryRun(PlugInExecutor.java:143)
>>>      at fiji.scripting.java.PlugInExecutor.tryRun(PlugInExecutor.java:104)
>>>      at fiji.scripting.java.PlugInExecutor.run(PlugInExecutor.java:85)
>>>      at fiji.scripting.java.PlugInExecutor.run(PlugInExecutor.java:79)
>>>      at fiji.scripting.java.PlugInExecutor.run(PlugInExecutor.java:75)
>>>      at fiji.scripting.java.Refresh_Javas.runOutOfTreePlugin(Refresh_Javas.java:436)
>>>      at fiji.scripting.java.Refresh_Javas.compileAndRun(Refresh_Javas.java:122)
>>>      at fiji.scripting.java.Refresh_Javas.runScript(Refresh_Javas.java:71)
>>>      at fiji.scripting.TextEditor$19.execute(TextEditor.java:1967)
>>>      at fiji.scripting.TextEditor$Executer$1.run(TextEditor.java:1776)
>>>  So it's even worse than without the filename ...
>>>  Christoph
>>>  
>>> Michael Schmid <[hidden email]> hat am 29. Januar 2013 um 18:36 geschrieben:
>>>> Hi Christoph,
>>>>
>>>> as I understand it, the 'path' in Filesaver.saveAsTiff(String path) should include directory AND filename. If you use only a directory name, it would overwrite the directory with a file, and it seems that java (or the operating system) is clever enough to refuse this.
>>>>
>>>> You could try
>>>> new FileSaver(SourceImage).saveAsTiff(dir+File.separator+SourceImage.getTitle());
>>>>
>>>> Michael
>>>> ________________________________________________________________
>>>> On Jan 29, 2013, at 18:00, [hidden email] wrote:
>>>>
>>>>> Dear ImageJ Community,
>>>>>
>>>>> i"ve got a problem with the FileSaver Class, i want to write a plugin which is
>>>>> able to save an Tiff Image in an chosen Directory.
>>>>>
>>>>> Here's my example code:
>>>>>
>>>>> public class My_Plugin2 implements PlugIn {
>>>>>
>>>>> public void run(String arg) {
>>>>> ImagePlus SourceImage = WindowManager.getCurrentImage();
>>>>> DirectoryChooser dc=new DirectoryChooser("Choose Save Directory");
>>>>> String dir=dc.getDirectory();
>>>>>
>>>>> new FileSaver(SourceImage).saveAsTiff(dir);
>>>>>
>>>>>
>>>>> }
>>>>> }
>>>>>
>>>>> But every time i get a Error:
>>>>>
>>>>> File Saver
>>>>>
>>>>> An error occured writing the file.
>>>>>
>>>>> Path (Acces is denied)
>>>>>
>>>>>
>>>>> I have the right the write at this Path, it's on my own Hard disk, and from the
>>>>> ImageJ Menu I'm able to save the image at this Path.
>>>>>
>>>>> Why couldn't I save the Image via the plugin?
>>>>>
>>>>> Thanks for your help.
>>>>>
>>>>> Christoph
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html