Running and Saving an Image Simulation using ZPL

  • 31 March 2021
  • 4 replies

I am wanting to use a macro to automatically run and store image simulations using a ZPL Macro. Does anyone have example code? 


Best answer by David.Nguyen 1 April 2021, 13:01

View original

4 replies

Userlevel 6
Badge +2

Hi Karen,


It depends on what you want from that analysis window, and whether you intend to change the settings in between different uses.

If you want the result from Image Analysis as if you'd have typed a file name in the Output File settings, and if you don't mind the image being overwritten. Then, you can save your Image Analysis settings, and then simply use:


Lookup the OPENANALYSISWINDOW keyword if you have relocated the setting file for Image Analysis.

If you rather want the result as if you'd have pressed the floppy icon from Image Analysis, then the keyword you need is EXPORTBMP, or EXPORTJPG and the code can be something like:

# Analysis window number
sim_window = WINL()
# File path
path$ = 'E:\MYBMPFILE'
# Export window to BMP
EXPORTBMP sim_window, path$

If you need something more involved, I'd recommend using the ZOS-API for this kind of task. It'll give you more freedom to get the results that you want.

Let me know if this is clear, and take care,



Hi David,

Thank you for your useful answer.

By doing your method, I observed that a duplicate file of the exported image is created in the folder “IMAFiles” of Zemax root folder. Do you have any idea to avoid this? My aim is to save time in a loop for example of a lot of image simulations.


Userlevel 2
Badge +2

Hi @Robin.Pierron,

I’m not aware of setting to avoid that. As a simple option, you could use DELETEFILE command in ZPL to ensure that the IMAFiles folder doesn’t get inundated with every saved image.

As David mentioned, you can achieve this also with the ZOS API, which will give you more options and other language native means to manage the files.


Userlevel 1

I have done this in the past using a for loop to append a timestamp or sequence number to the file name. I don’t have access to that code any longer, but the goal was to use Geometric Image Analysis to look at the shape of a “collimated” beam from a multimode fiber, as a function of distance from the collimator lens. I used EXPORTJPG in the for loop. It worked pretty well, and I was able to combine the images into a movie file using other software. 

One shortcoming to this is the inability to add a text note to the Geometric Image Analysis window using ZPL. I would have liked to label each distance step on the actual image. Maybe there is a way to access the Annotation tools (box, arrow, line, text) from ZPL, but I haven’t found it. Perhaps ZOS-API would be able to accommodate this.