- Home
- Programming ZEMAX
- ZPL
- How to manipulate BMP and JPG files using the IMAGEEXTRACT and IMAGECOMBINE keywords
How to manipulate BMP and JPG files using the IMAGEEXTRACT and IMAGECOMBINE keywords
- By Sanjay Gangadhara
- Published 2 May 2008
- ZPL
- Unrated
How to split and combine images in ZPL
The ZPL macro keyword IMAGEEXTRACT allows users to extract portions of an image and save them into separate files. The keyword IMAGECOMBINE allows users to combine different images into a single file. An example macro (IMAGE_MANIPULATE.ZPL) which demonstrates how to use these features is provided in the .ZIP file located at the end of this article. The test image file used by this macro (Campus.BMP) is also provided in the .ZIP file. Before running the macro, start by placing the image file in the directory C:\Program Files\ZEMAX\IMAFiles\ (if you are running ZEMAX from a different directory, please place the image file in the corresponding \ZEMAX\IMAFiles\ directory on your machine, and then modify the macro to specify the correct location of this directory on your machine).
The macro can then be run from any ZEMAX lens file (e.g. the default LENS.ZMX file which comes up when you first open the ZEMAX application). The macro first reads in the input image file, and determines how many pixels are in this file using the ZPL functions PIXX and PIXY:
! Read in the input file
A$ = "C:\Program Files\ZEMAX\IMAFiles\Campus.BMP"
! Determine how many pixels are in the input file
A_PIXX = PIXX(A$)
A_PIXY = PIXY(A$)
PRINT "Number of X pixels = ", A_PIXX
PRINT "Number of Y pixels = ", A_PIXY
Files are then defined for the 4 images that will be extracted from the input file in this case:
! Define 4 files corresponding to the 4 slices that the input file will be cut in to
A1$ = "C:\Program Files\ZEMAX\IMAFiles\Campus1.BMP"
A2$ = "C:\Program Files\ZEMAX\IMAFiles\Campus2.BMP"
A3$ = "C:\Program Files\ZEMAX\IMAFiles\Campus3.BMP"
A4$ = "C:\Program Files\ZEMAX\IMAFiles\Campus4.BMP"
The IMAGEEXTRACT keyword is then used to split the input image into 4 sections along the horizontal direction and store each image section into a corresponding BMP file:
! Splice the input file into 4 even pieces in the X direction
DIVX = 4.0
IMAGEEXTRACT A$, A1$, 1, 1, A_PIXX/DIVX, A_PIXY
IMAGEEXTRACT A$, A2$, 1.0*A_PIXX/DIVX + 1.0, 1, A_PIXX/DIVX, A_PIXY
IMAGEEXTRACT A$, A3$, 2.0*A_PIXX/DIVX + 1.0, 1, A_PIXX/DIVX, A_PIXY
IMAGEEXTRACT A$, A4$, 3.0*A_PIXX/DIVX + 1.0, 1, A_PIXX/DIVX, A_PIXY
Each of the split image files may be viewed and manipulated individually. For example, while the original BMP image looks like:
the first slice along the horizontal direction looks like:
The IMAGECOMBINE keyword is then used to re-form the original image from the 4 pieces:
! Combine the slices back into a single image
A12$ = "C:\Program Files\ZEMAX\IMAFiles\CampusInput_12.BMP"
A34$ = "C:\Program Files\ZEMAX\IMAFiles\CampusInput_34.BMP"
A_ALL$ = "C:\Program Files\ZEMAX\IMAFiles\CampusInput_ALL.BMP"
M$ = "LEFTRIGHT"
IMAGECOMBINE A1$, A2$, A12$, M$ # Combine input files 1+2
IMAGECOMBINE A3$, A4$, A34$, M$ # Combine input files 3+4
IMAGECOMBINE A12$, A34$, A_ALL$, M$ # Combine input files 1+2+3+4
= all
This is done in three steps, since IMAGECOMBINE can only be used to combine two images at a time. The “mode$” parameter is set as “LEFTRIGHT” because the original image was split in the horizontal direction. The recombined image is stored in the file CampusInput_ALL.BMP, and looks identical to the original image. If we had mistakenly set the “mode$” parameter to “TOPBOTTOM”, the sliced images would have been stacked vertically, as seen here. (click on link to observe the image in a separate window).