Showing posts with label imagemagick. Show all posts
Showing posts with label imagemagick. Show all posts

Wednesday, June 21, 2017

Combine separate gray scale TIFF images into a single RGB TIFF image

Some multi spectral cameras capture images as individual gray scale TIFF images - each file containing data for a single light wavelength e.g. red, green, blue, or infra-red. Example files are the gray scale TIFF files shown below, each file representing a single color band.

Combining these individual color bands into a single RGB color composite can be done using the free and open source ImageMagick software, as shown in the steps below.

  1. Open up a Command Prompt.
  2. At the prompt, type in the ImageMagick convert command:

    C:\> magick convert -verbose band_red.tif -channel R band_green.tif -channel G band_blue.tif -channel B -combine -channel RGB -alpha off -colorspace sRGB out_rgb.tif

    Note:
    band_*.tif are the input TIFF files. Each input file must be followed with the correct channel option.
    out_rgb.tif is the output RGB TIFF file.
    -verbose is used to print out processing messages.
    -combine -channel RGB are for combining the bands.
    -alpha off is used to disable the creation of the alpha channel in the output RGB file.
    -colorspace sRGB is used to set the output color space.
  3. Run the command.

    Processing messages appear. The output file out_rgb.tif is created.
  4. Open up the resultant file in an image editor, e.g. GIMP.

    The file is displayed in color and the the bands are combined into the image's red, green and blue channels.

Monday, October 26, 2015

Extract a subset of an image using ImageMagick

ImageMagick's convert tool (now called magick in version 7) can be used to perform a large number of tasks. Recently, I had to extract out a portion of a large number of images at certain locations and at certain dimensions. Performing this interactively is tedious especially if there are hundreds of photos. Luckily, magick can be used to do the job in batch, with the right parameters.

The input image for extracting a subset

  1. Open up a Command Prompt.
  2. At the prompt, type in the following:

    C:\> magick input.jpg -crop 328x380+6776+192 output.jpg

    Note: 328 is the image width to extract
    380 is the input image height to extract
    6776 is the input image top left pixel x location to extract
    192 is the input image top left pixel y location to extract

  3. Press RETURN.

    The image is extracted out.

Monday, October 19, 2015

Quickly generate a spreadsheet of image properties from a large number of images

Recently there was a need to find out some image properties e.g. the pixel width and height of a large number of photo images. In Windows Explorer, you could enable the Dimensions property to be displayed in the detail view, but you could not do too much with it. You could not do additional calculations on top of the view.

Fortunately, ImageMagick has an identify command to print out information about one or more images. The following shows how to use identify to print out the image file names and dimensions.


  1. Open up a Command Prompt.
  2. At the prompt, type in the command e.g.

    C:\> identify -ping -format "%i,%w,%h\n" *.jpg

    Note: the ping option will prevent the loading of the entire file.
    The format string has place holders for the properties to be printed.
    %i is for the image filename
    %w is for the image width
    %h is for the image height
    And optionally some delimters such as commas between the properties and terminated with a new line character
    '\n'.
    For more information about the string format placeholders, visit http://www.imagemagick.org/script/escape.php
  3. Press RETURN to run the command.

    The information is printed to the screen.

  4. If you want to print the information to a file, type in the following command.

    C:\> identify -ping -format "%i,%w,%h\n" > output.csv
  5. The resultant file can be displayed and further manipulated in a spreadsheet as shown below.



Monday, December 15, 2014

Create a Colored Infra Red (CIR) image from a multi-channel RGBI TIFF file using ImageMagick

Some remote sensing imagery TIFF files contain multiple bands of data such as red, green, blue, and infra-red data. In order to visualize these bands, it is necessary to combine them into an RGB format such as what is called a Colored Infra Red (CIR) display. The open source software ImageMagick can be used to generate the CIR display from the multi-channel TIFF files.

This post illustrates how to create a CIR image from an input RGBI TIFF file. In order to generate a CIR image, the input channels - RGBI has to be reordered to IRG i.e.

  • the file's output red channel is replaced with the input Infra red channel, 
  • the output green channel is replaced with the input red channel, 
  • the output's green channel is replaced with the input green channel, 
  • and the input blue channel is essentially thrown away. 


The ImageMagick command to do this is quite simple, as shown below.

$ convert input_rgbi.tif -channel rgba -separate -swap 1,2 -swap 0,1 -swap 0,3 -channel rgb -combine output_cir.tif

Note: the sequence of options is important.

  • The option -channel rgba instructs the command to read in 4 channels.
  • The option -separate splits the input channels
  • The option -swap 1,2 swaps the 2nd and 3rd channels from RGBI to RBGI
  • The option -swap 0,1 swaps the 1st and 2nd channels from RBGI to BRGI
  • The option -swap 0,3 swaps the 1st and 4th channels from BRGI to IRGB
  • The option -rgb removes the 4th channel to become IRG
  • The option -combine merges all the 3 channels back
The input RGBI file (only the RGB channels are displayed)
The CIR image generated from ImageMagick

Monday, July 22, 2013

Create cloud masks for aerial photos in batch

Clouds in aerial photos can interfere in the feature identification and matching stages of the ortho-photo rectification process. Depending on the software algorithm, it may be necessary to either remove the entire photo or mask out the cloud portions from the photo. If there are hundreds or thousands of photos with clouds, then it will be very tedious to manually digitize the cloud masks with a photo editing software. It would be nice to have some tool to automate the task somewhat.

Here is a simple procedure of using the free and open source software Imagemagick and the threshold option to generate the cloud masks from the photos. The assumption is that clouds in aerial photos would be brighter than the terrain underneath. An example aerial photo with clouds is shown below.
Photo credit: Kevin Payravi, Wikimedia Commons

  1. In the Windows Command Prompt, type in the following:

    C:/> convert Overview_head_of_cloud_with_shadow.jpg -threshold 40% cloud_mask.png

    Note: the threshold option tells Imagemagick to set pixel values below 40% to 0 (black) and above to white.

    The mask is created.

  2. Optional. In some software, black is the mask so it may be necessary to invert the mask with the following command.

    C:/> convert cloud_mask.png -channel rgba -transparent black -fill black -opaque white -fill white -opaque none -alpha off -define png:color-type=6 cloud_invert_mask.png

    The mask is inverted as RGB file type
Note: if there are bright objects on the ground e.g. river, stream, road, metallic rooftops, then the mask may contain these objects too. It may be necessary to edit the mask to remove these objects.

Monday, May 21, 2012

Adjust brightness and contrast of intensity images using Imagemagick

Sometimes the intensity images generated from LiDAR las files are too dark or too bright. An example of a dark intensity image is shown below.

It is useful to be able to apply some post-processing brightness and contrast adjustments to the images. Photoshop can be used to interactively adjust the brightness and contrast but I like to use the free Imagemagick open source software to process images in batch. Imagemagick has a convert utility which can perform brightness and contrast adjustments on images from the command line.

To make an intensity image brighter, type in the following at the command line prompt with the brightness-contrast option.
c:\> convert -brightness-contrast 50x20 input.tif out_lighter.tif

where the 50 tells the utility to increase the brightness by +50 and 20 is to increase the contrast by 20.



To make an intensity image darker, type in the following at the command line prompt with negative brightness and/or contrast values.
c:\> convert -brightness-contrast -30x10 input.tif out_darker.tif

where -30 is to darken by 30 and +10 is to increase the contrast by 10.

More information about the convert utility is available on this link.

Wednesday, June 18, 2008

Batch Resize Image Files with ImageMagick

There is a useful open source software for manipulating image files. No, I'm not talking about GIMP but rather another software suite called ImageMagick. ImageMagick can read, convert and write images in a variety of formats including JPEG, PNG and even PDF. It comes with command line utilities like convert, mogrify, identify, composite, etc. The main reason I am mentioning ImageMagick is that I like to use its mogrify utility to resize a large number of image files for display on my Palm Tungsten device. High resolution images cannot be displayed on my PDA due to insufficient memory - so I have to resize the images to a lower resolution. Photoshop has a graphical resize tool but Photoshop is not free and it takes a longer time to fire up. With ImageMagick, all I have to do is open up a Command Window and type in the following commands:

C:\> mogrify -resize 90% *.jpg

The above command will resize all files with the extension JPG to 90% of their original resolutions.

Note: Make copies of your files before using mogrify as it will make the adjustments on the actual files.