Padding Images

Image padding introduces new pixels around the edges of an image. The border provides space for annotations or acts as a boundary when using advanced filtering techniques.

This exercise adds a 10-pixel border to left, right and bottom of the image and a 30-pixel border at the top allowing space for annotation. The diagonal lines in the following image represent the area that will be added to the original image. For an example of padding an image, complete the following steps.

Example Code
See paddedimage.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering paddedimage at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT paddedimage.pro.

Figure 2-3: Diagonal Lines Indicate Padding

imggeom05.jpg

To add a border around the earth image, complete the following steps:

  1. Open the world image file:
  2. earth = READ_PNG(FILEPATH('avhrr.png', $ 
       SUBDIRECTORY = ['examples', 'data']), R, G, B) 
    
  3. Prepare the display device:
  4. DEVICE, DECOMPOSED = 0, RETAIN = 2 
    
  5. Load the color table with the red, green and blue values retrieved from the image in step 1 and modify the color table so that the final index value of each color band is the maximum color value (white):
  6. TVLCT, R, G, B 
    maxColor = !D.TABLE_SIZE - 1 
    TVLCT, 255, 255, 255, maxColor 
    
  7. Get the size of the image by entering the following line:
  8. earthSize = SIZE(earth, /DIMENSIONS) 
    
  9. Define the amount of padding you want to add to the image. This example adds 10 pixels to the right and left sides of the image equalling a total of 20 pixels along the x-axis. We also add 30 pixels to the top and 10 pixels to the bottom of the image for a total of 40 pixels along the y-axis.
  10. Using the REPLICATE syntax, Result = REPLICATE (Value, D1 [, ..., D8]), create an array of the specified dimensions, and set Value equal to the byte value of the final color index to make the white border:

    paddedEarth = REPLICATE(BYTE(maxColor), earthSize[0] + 20, $ 
       earthSize[1] + 40) 
     
    
    

    Note
    The argument BYTE(maxColor) in the previous line produces a white background only when white is designated as the final index value for the red, green and blue bands of the color table you are using. As shown in step 3, this can be accomplished by setting each color component (of the color table entry indexed by maxColor) to 255.

    See Graphic Display Essentials (Using IDL) for detailed information about modifying color tables.

  11. Copy the original image, earth, into the appropriate portion of the padded array. The following line places the lower-left corner of the original image array at the coordinates (10, 10) of the padded array:
  12. paddedEarth [10,10] = earth 
    
  13. Prepare a window to display the image using the size of the original image plus the amount of padding added along the x and y axes:
  14. WINDOW, 0, XSIZE = earthSize[0] + 20, $ 
       YSIZE = earthSize[1] + 40 
    
  15. Display the padded image.
  16. TV, paddedEarth 
    
  17. Place a title at the top of the image using the XYOUTS procedure.
  18. x = (earthSize[0]/2) + 10 
    y = earthSize[1] + 15 
    XYOUTS, x, y, 'World Map', ALIGNMENT = 0.5, COLOR = 0, $ 
       /DEVICE 
    

The resulting image should appear similar to the following figure.

Figure 2-4: Resulting Padded Image

imggeom06.gif