Difference between revisions of "ImageMagick"

From Noah.org
Jump to navigationJump to search
Line 18: Line 18:
 
[[Image:median_still.png|frameless]]
 
[[Image:median_still.png|frameless]]
  
<video>median_source</video>
+
<video>median_source.mov</video>
  
 
== Find differences in images ==
 
== Find differences in images ==

Revision as of 11:09, 1 August 2012


ImageMagick is your friend, but your friend has terrible man pages. The man pages just tell you to go to the web site and read docs in a browser.

blend images removing differences

If you take a number of pictures of the same scene you can create a single image that removes the differences between each image. In other words, you can preserve only the common elements. This is useful for removing people or cars moving in front of a background. After this transformation you will have only the background.

convert -evaluate-sequence Median stack*.jpg output.jpg

example

I took a short video using my iPhone4. I then converted the video to individual frames. I applied the Median filter to remove most moving distractions. Note that a few people in the background remained because they were not moving very much. Note the blue blob in the left third of the image. This was due to a woman walking across the frame and then into the background. In certain parts of the frame the woman's blue pants were on longer than the backgound, so the blue "outvoted" the other pixels.

File:median source.mov

median still.png

<video>median_source.mov</video>

Find differences in images

quick hack to finding differences in images

This is useful to search for unusual pictures in a large set of images that are mostly the same.

In this example, I'm expecting a relatively bright blue object to appear in some images of a set that are otherwise nearly identical. I was searching a long video stream for a blue scintillation flash triggered by an x-ray emitter. This creates a log file showing the average blue value for each frame of the video. After the log is generated you simply look for the frames that have a much higher average than surrounding frames.

for fn in *.png; do 
    echo -n "${fn} : "
    convert ${fn} -colorspace rgb -scale 1x1 -format "%[fx:floor(100*b)]" info:
done | tee -a blue_average.log

Create an animated gif with a 'patrol cycle'

Create an animation with a sequence of frames forward and then backward. Actually, this example creates them backward then forward. This is called a patrol cycle after the back-and-forth march that a guard walks.

convert seq-*.png -set delay 10 -reverse \
    seq-*.png -set delay 10 \
    -loop 0 animation2.gif

Convert 16-bit grayscale PPM or TIFF images to 8-bit PNG

The correct way (or at least the best way) to convert 16-bit grayscale TIFF or PPM file to PNG is to use the following command:

mogrify -colorspace gray -type grayscale -depth 8 -format png ct-scan-frames-*.ppm

If you run without the two options, colorspace gray -type grayscale, then you will end up with 8-bit PNG files that may look right, but they may behave oddly in some applications. In particular, they will annoying `mplayer` and `mencoder` and cause an annoying flicker when you try to convert them to a video format. The problem is that the first image `mogrify` converts will be in class 8-bit PseudoClass 256c, but all subsequent images will be in class 8-bit DirectClass 129KiB. You can see this if you run identify *.png... I have no idea what is the difference between PseudoClass and DirectClass.

Convert a TIFF image stack into individual images

Tiff image stacks are often used in scientific visualization and medical imaging. It's basically a way to store multiple images in a single file. It's kind of like a GIF animation. Sometimes you see the phrase "multi-page images" or "multi-image sequence". Formats like JPEG and PNG do not support image stacks.

You might also want to see the -adjoin and +adjoin command-line options.

The simplest way is to specify an output filename with a C-style integer format string. This will automatically enable the +adjoin feature (the opposite of -adjoin). The following is an example using a C-style format %02d to number a sequence of PNG images converted from a TIFF image stack:

convert ct_scan_brain_abby_normal.tif ct_scan_stack_%02d.png

Contact Sheet / Thumbnail sheet

Where 'images_*.png' may be a filename glob pattern or a list of filenames.

montage -label '%f' images_*.png contact-sheet.png

Convert an image to an icon

This creates an 80x80 icon. If an edge needs to be cropped then it tries to do the right thing. This first does a resize to force an image to at most 80x80; then this does an extent which sets the page to exactly 80x80. If one or more edges are smaller than 80x80 then the image it padded on the borders to fill exactly the given extent.

convert input.jpg -resize 80x80^ -gravity center -extent 80x80 icon.png

Convert all images in a directory from one format to another

This is easy. Use Mogrify, not Convert. The following example converts all PNG files to JPEG:

mogrify -format jpg -quality 85 *.png

I always forget this. Maybe it's because the ImageMagick docs say:

Mogrify overwrites the original image file, whereas, 
convert writes to a different image file.

Which is true unless you want to convert a group of images. In that case, you use Mogrify instead of Convert. Why? I would call this a bug. If you try this with Convert it will not work (and will, in fact, destroy the last image in your directory).

It beats doing it this way:

for filename in *.png ; do convert -quality 85 $filename `basename $filename .png`.jpg; done

Resize all images in a directory

This converts all the images in the current directory to JPEG images scaled to fit a 480x272 screen. This is the resolution of the PSP. Sometimes it's just easier to use `find` rather than try to think the way ImageMagick thinks.

find . -exec convert {} -format jpg -quality 80 -resize 480x272^ -gravity center -extent 480x272 ../output/{}.jpg \;

Remove Transparency

This will turn all transparent pixels in an image to the given background color.

convert input.png -background white -flatten output.png

Display Alpha Transparency over white background

This displays a transparent image without that stupid gray checkerboard pattern which is totally useless for looking at postscript images.

display -background white -flatten image.ps

Working with RAW files (NEF)

Sometimes I have to work with raw image files from a Nikon D2X (saved as NEF file). ImageMagick cannot work directly with raw NEF files. You need to install a few extra tools: netpbm and dcraw.

aptitude install netpbm dcraw

The `dcraw` command converts raw files from various cameras to PBM format. The netpbm commands are used to convert the PBM format to something easier for ImageMagick to deal with.

Convert all NEF images to PNG:

dcraw -c -w input.NEF | pnmtopng > output.png

To convert an entire directory:

for filename in *.NEF ; do dcraw -c -w $filename | pnmtopng > $filename.png ; done