Example: Listing Image Information
This example shows a simple script that lists information about any number of images, such as its number of columns and rows, in a Mira Text window. This short script illustrates some basic Pro Script concepts.
An external Lua function named LoadImages() is called to open images. This function is supplied as the text file ListImageInfo.lua in the Scripts\Include subdirectory of your Mira installation.
A CImageSet object is used for holding references to all the images you open. The image information is later gathered from the image set.
Afor loop is used to process each image of the image set.
A CTextView object is used for listing information in a Mira Text Editor window. From the Editor, you may format the text and save it, print it, or copy it to the Windows clipboard for pasting into another program.
C-language style print formatting is used to list information to the Text Editor.
Semicolons are used to separate multiple statements on the same line.
This script first calls LoadImages() to open images. These images are loaded into memory but are not displayed. When LoadImages()is called, it creates a CImageSet object into which it will save references to 1 or more images that it opens from files. The function then enters a loop in which it repeatedly calls the Windows Open dialog for you to select each file in turn. To open a file, select it and click [OK] in the Open dialog. This loads the file into memory and adds a reference to the CImageSet object. When you click[Cancel] , the function returns to the main script and returns a reference to the CImageSet it created. The script then loops over each image in the image set, listing information about it on a new line in the Text Editor window.
The script to list image information is shown below. Note that this script loads the entire image into memory each time. Although this script uses only the image header, the entire image is held in memory.
|
|
|
-- inserts LoadImages() into the script |
|
-- load images into an image set |
|
-- create a Text Editor object |
|
-- loop over all images |
|
-- get a reference to the image |
|
-- get image width and height |
|
-- shorten the file name |
|
-- list the information |
|
-- end of loop |
|
|
After the end of the for loop, there are no more statements, so the script exits. The listing in the Text Editor window remains open for use.
This is a complete and useful script that you can type into a Script Editor and execute from Mira. However, to implement good programming practice, there are some changes we might implement:
The script does not explicitly delete the opened images from memory; it depends upon Mira to do this behind the scenes. Actually, Mira does automatic automatically delete the images and reclaim memory at the end of the script. However, if the images were no longer needed after the for loop, and there were additional statements that needed to use a large amount of memory, then it would be a good thing to delete the images just after the end statement. We would simply create another, similar loop but with one internal statement: I:delete.
Following the image deletion loop, we might also delete the CImageSet object using S:delete.
Other image properties could be listed by adding statements likeI:Exptime and I:PixelTypeStr.
You might terminate the script with the Exit function to show a message that the script exited.
Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics,
Inc. All Rights Reserved.