User-Defined Filter Kernel

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.

Key Concepts

Overview

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.

Implementation

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.

 

Include("Include\\LoadImages.lua")

-- inserts LoadImages() into the script

S = LoadImages()

-- load images into an image set

T = CTextView:new("Image Info")

-- create a Text Editor object

for i=1,S:Count() do

-- loop over all images

  I = S:GetImage(i)

-- get a reference to the image

  w=I:Cols() ; h=I:Rows()

-- get image width and height

  s=I:Path(50)

-- shorten the file name

  T:Printf("%s [%d,%d]\r\n", s,w,h)

-- list the information

end

-- 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.

Improving the Script

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:

  1. 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.

  2. Following the image deletion loop, we might also delete the CImageSet object using S:delete.

  3. Other image properties could be listed by adding statements likeI:Exptime and I:DatatypeStr.

  4. You might terminate the script with the Exit function to show a message that the script exited.

Related Topics

Script Examples