Save image error with "Image Pyramid During Acquisition" setting

Post your acquisition-related questions and macros here
Post Reply
user-6053
Posts: 9
Joined: Thu Jan 01, 1970 1:00 am

Save image error with "Image Pyramid During Acquisition" setting

Post by user-6053 »

Dear ZEN developer:

I am trying to use MATLAB to control ZEN blue to acquire some tile images. Everything worked fine until I tried to untick the "Image Pyramid During Acquisition" tab in the "Tiles" setup tab.
So I defined a tile scan experiment with 2 channels in ZEN and saved it with name "test.czexp", and then I started working on MATLAB as follows:

%link MATLAB to ZEN, and get the experiment with name
enableservice('AutomationServer',true)
Zen = actxGetRunningServer('Zeiss.Micro.Scripting.ZenWrapperLM');
Zen.Application.Documents.Add(img);
ZEN_Experiment = 'test.czexp';
exp = Zen.Acquisition.Experiments.GetByName(ZEN_Experiment);

%execute the experiment from MATLAB, return the image acquired
img = Zen.Acquisition.Execute(exp);
Zen.Application.Documents.Add(img);
filename = [savefolder,img.Name]
img.Save_2(filename);

So each time when I check the saved images on disk, it was just a very small (normally 285kb) file and cannot be displayed by either ZEN or ImageJ.
However if I execute the experiment purely within ZEN, I can right click and save the images (although no preview since no image pyramid enabled) onto the disk and then the image appeared in the ZEN window and everything looks fine. The file on disk is comfortably around 60 Mb.
And if I ticked the "Image Pyramid During Acquisition" box and save the experiment, then the above MATLAB command can result in a successfully saved image, that can be read either through bioformat, or ZEN. It's just the different image pyramid are also taken and saved, and even worse are then recognized as different series of images taken together which are not useful and not necessary.

I tried to look into the method Save_2(), but there's no document, and Save(),Save_3(),Save_4() just simply doesn't work. I have no clue why Save_2() was the one to choose and how it works. I tried to use the macro recorded when I manually save the images onto the disk:
img2 = Zen.Application.Documents.GetByName("Experiment-01.czi")
Zen.Application.Save(img2, savefolder, True)
But it doesn't work either. The result looks exactly the same as from img.Save2(filename).

I am wondering if there's a way to get around the image pyramid and still be able to save the images with MATLAB onto the disk? And if possible if there's some document I can refer to the different methods within the Zen library for example what exactly happening with Save(), Save_2(), Save_3() and so on...?

Thanks a lot for your help!

Best Regards,
Ziqiang Huang
user-4
Posts: 398
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Hello Ziqiang Huang,

There are many questions in your post, so let us go step by step.

1. It is a good idea to test your code in the OAD-Editor with Python

The main idea is:

myimage = Zen.Acquisition.Execute(myexperiment)

As you unchecked the image pyramid a

Zen.Application.Documents.Add(myimage)

does not make sense as ZEN will not display the image anyway.

2. If an image save does not work after this, you simply discovered a bug that we have to fix. But there is a simple workaround! Before saving the image use

myimage.Load(myimage.FileName)

and then

myimage.Save(myfilename)

or even

Zen.Application.Documents.Add(myimage)

should work.

3. Save(), Save_2() etc. comes from the COM interface. The problem is, that COM does not allow you to ‘override’ function calls like Save, Save(filename) etc. but generates a new function name each time resulting in Save(), Save_2() etc. If you have an environment that supports IntelliSense of COM like the macro environment of Excel, you can see that Save_2 expects the filename as a parameter.
user-6053
Posts: 9
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6053 »

Dear CZ3

myimage.Load(myimage.FileName) doesn't work either. It seems now I will have to always check the "image pyramid" box to get an image.
It will be certainly helpful if someone can look into this, or come up with another way to bypass it. I still feel it's possible since I can manually save the images fine with "image pyramid" unchecked.

By the way, myimage.Save(myfilename) never work since for some reason Save_2() is the function to save.
So regarding the COM interface, could you shed more light on how I can see the codes of the functions? Our system is AxioObserver Z1 with ZEN blue 2.3 installed on windows 7 64bit system. I did a lot VBA macro scripting with Excel but don't understand what is "IntelliSense of COM". I googled for a while but still couldn't figure it out. Any help will be appreciated.

Best Regards,
Ziqiang Huang
user-4
Posts: 398
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Hello Ziqiang Huang,

myimage.Load(myimage.FileName) worked on our ZEN 2.3 with current Hotfix. This has been tested in the macro environment of ZEN.
It is recommended that any COM-related programming should be first formulated in the Python macro language and then transferred to COM.

As discussed in point 3. of my last post myimage.Save(myfilename) is just valid for a Python macro and will, of course, never work in COM since Save_2 is needed as COM cannot override functions.
user-6053
Posts: 9
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6053 »

Thank you CZ3,

It turns out the Macro Editor within ZEN actually can provide the IntelliSense interpretation environment that indicating the input argument of certain methods provided by ZEN. So I called ZEN application as COM object from within the macro editor, and then I can check the code. I know this is quite unnecessary and might be funny, but somehow it's helping me to understand better the OAD environment of ZEN.
However I found again another mismatch there (see attached screenshots), that this time ZenImage object has no Save_2() method any more, but only Save(). I found this is quite difficult to understand since everything else is the same when I called ZEN from MATLAB through COM interface.
Since you emphasized the macro editor is based on python I tried run the same experiment through COM but from python as well. The code I tested is as follows:
##########################################################
import os

savefolder = 'C:\\Python_ZEN_Output\\'

Zen = win32com.client.GetActiveObject("Zeiss.Micro.Scripting.ZenWrapperLM")

ZEN_Experiment = 'EF_ZHtest_1_guidedAcquisition_OverviewScan.czexp';

exp = Zen.Acquisition.Experiments.GetByName(ZEN_Experiment)
#run AutoFocus
Zen.Acquisition.FindAutofocus_2(exp)

img = Zen.Acquisition.Execute(exp)
Zen.Application.Documents.Add(img)
filename = savefolder + img.Name_2
img.Save_2(filename)

##########################################################
Somehow the FindAutofocus() is also not working and again FindAutofocus_2(exp) did the job... But anyway it worked. So here it seems the code worked more like the example in MATLAB. If the version of ZEN we are using has the same hotfix as yours, I will test the img.Load(img.FileName) or something like img.Load_2(img.FileName_2) tomorrow with unchecked "image pyramid" and see if it works.
Besides this, is it possible to get some documentation of the ZENWrapperLM object? It would be quite helpful to improve and speed up the OAD development so one don't have to go through many trial-and-errors to get the right methods called in the right way.

Thanks for your patience and help.

Bests,
Ziqiang Huang
Attachments
ZenImage object has no attribute Save_2.jpg
ZenImage object has no attribute Save_2.jpg (78.01 KiB) Viewed 2678 times
CallZenAsCOMObject_from_Macro.jpg
CallZenAsCOMObject_from_Macro.jpg (78.5 KiB) Viewed 2678 times
user-4
Posts: 398
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

is it possible to get some documentation of the ZENWrapperLM object?
Hello Ziqiang Huang,

in the macro Editor of ZEN select Help in the menu bar.

Then click 'Macro Object Model...' and open the 'Zeiss.Micro.Scripting' tree.

You will find ZENWrapperLM at the end of the list.

But, I think that this is not what you expected, because this shows more or less what IntelliSense in the editor shows. The interface of ZENWrapperLM is also used by Python and in the Macro Editor this mainly corresponds with the Zen object (Zen.Application…etc.). Most probably the misunderstanding is (again), that COM transfers the functions of ZENWrapperLM into function, function2, etc. if function has overridings like function(), function(parameter1), function(parameter1, parameter2), etc.

To have a look at the COM interface of ZENWrapperLM I use the macro environment of Microsoft Excel as this environment allows you to ‘bind’ to the .tlb file of a COM interface and IntelliSense of the Excel Macro Editor shows the parameters of function, function2. This possibility has been suggested already in my first post.
user-6053
Posts: 9
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6053 »

Hello

I tried many different ways with image.Load(), but it really doesn't work whenever "image pyramid" is unchecked. I understand now the COM interface interpret function overload with "_number" when called externally. And I tried to always run command with the macro editor first. However even with the macro editor, if a tile scan experiment saved with "image pyramid" unchecked, following the same sequence of command, an image won't be successfully "added" or "loaded" into ZEN document, and of course it can not be saved properly.
I basically followed line by line of the command, and everything looks OK up to img = ZEN.Acquisition.Execute(exp), or even after img.Load(img.FileName)
Zen.Application.Documents.Add(img);
When I execute the code until this point, I can still then manually save the image. I recorded macro(s) to see if I can replace the manual operation with macro command. So the manual save operation appeared in macro recorder are:
image1 = Zen.Application.Documents.GetByName("Experiment-01.czi")
Zen.Application.Save(image1, <path to file>, True)

When I added these two lines of code in the end of the original script then it doesn't work. I even tried to predict the name of the next image will be acquired by ZEN, to give a string variable instead of img.Name, it didn't work. I tried with image1 = Zen.Application.Documents.ActiveDocument, and it didn't work either. At this point I am actually curious how it works from your version of ZEN, since we probably don't have the hotfix that you mentioned.

Alternatively I tried with Image Export in Processing tab. Unfortunately there I encountered some other problems.
So I first recorded a macro when manually exporting acquired image as tiff and in the macro editor it appeared to be the following codes (I modified the naming a little but it should work the same way):

image1 = Zen.Application.Documents.GetByName(img.Name)
processingsetting1 = Zen.Processing.Utilities.Settings.SingleFileExportSetting(ZenExportFileType.TIFF)
processingsetting1.Compression = ZenTiffCompression.None
processingsetting1.ConvertTo8Bit = False
processingsetting1.DestinationFolder = outputFolder
processingsetting1.SubsetString = ''
processingsetting1.OriginalData = True
processingsetting1.ApplyDisplayCurveAndChannelColor = False
processingsetting1.UseChannelNames = True
processingsetting1.Resize = 1
processingsetting1.GenerateXMLFile = True
processingsetting1.CreateFolder = True
processingsetting1.Tiles = ZenTileMode.ExistingTiles
processingsetting1.Columns = 1
processingsetting1.Rows = 1
processingsetting1.Overlap = 0
processingsetting1.GenerateZipFile = False
processingsetting1.Prefix = img.Name.Replace(".czi","")
processingsetting1.MergedChannelsImage = False
processingsetting1.IndividualChannelsImages = True
processingsetting1.BurnInGraphics = True
Zen.Processing.Utilities.ExportSingleFile(image1, processingsetting1)

First thing again, it doesn't work when "image pyramid" is unchecked. An Error window will pop in the end saying in the code at last line (where the exportSingleFile command lines) "One or more errors occurred.".
It can run nicely when the image pyramid box checked. Since my major problem with "image pyramid" is the downsampled images created together with the original images, I can live with this as long as the original images exported to the location I want. However this can not be done externally, neither from Matlab nor python calling through COM interface. The common error is "ZenExportFileType" is not defined. I am guessing the objects and/or methods within Processing or Utilities classes are not callable through COM server, is this correct? I checked through the Macro Object Model as you suggested, and it seems Zeiss.Micro.Scripting.ZenWrapperLM class doesn't contain certain methods. Is this intentionally designed like that so I shouldn't expected to run the methods within Processing from external application? Or there is some way around that this can still be done? I assume the only available COM server provided by ZEN is the class ZenWrapperLM, is that right?

Best Regards,
Ziqiang Huang
user-4
Posts: 398
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Hello Ziqiang Huang,

you are right! Maybe we tested the code with the ZEN Version to be released ;-(

Anyway, the following code worked for me with a small experiment of 4x4 tiles without image pyramid during acquisition:

Code: Select all

myfilename = r'C:\Users\.......\Test_Image\MyTestImage.czi'

myexperiment = Zen.Acquisition.Experiments.ActiveExperiment
myimage = Zen.Acquisition.Execute(myexperiment)
tmp = myimage.FileName
myimage.Close()
myimage.Load(tmp)
Zen.Application.Documents.Add(myimage)
myimage.Save(myfilename)
I hope this solves all your problems ...
user-6053
Posts: 9
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6053 »

Dear CZ3,

I feel so sorry I haven't get back to you of the update. In short, the problem solved, horray.

So it turns out what I have been missing all the time was actually "myimage.Close()" in my scripts. After adding this, everything works fine now.

And the "image export" which belongs to image processing class, can it be registered similarly through Zeiss.Micro.Scripting.Core.dll or Zeiss.Micro.Scripting.Core.Utilities.dll, just like Zeiss.Micro.Scripting.dll Zeiss.Micro.LM.Scripting.dll, Presumably via a batch script? Is this doable, or it's simply not supported so far by Zeiss? It will be really helpful for me to know this.

Thanks a lot for your help from the beginning!

Best Regards,
Ziqiang
Post Reply