ImageMagick: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
Line 1: Line 1:
[[ImageMagick]] is ''the'' "swiss army knife" image manipulation program for the (FLOSS) world. In addition to a collection of powerful command line utilities, [[ImageMagick]] is available via libraries in most of the popular (web) scripting languages.
= Manipulating images on the command-line with ImageMagick =


== Installing ==


== Ubuntu / Debian ==


Imagemagick may very well be pre-installed on your system. Otherwise use synaptic or apt-get to pull in the "imagemagick" package.
== General ==


=== Gentoo Linux ===
=== Checking if you have ImageMagick installed ===


You should be able to simply "emerge imagemagick" using [[Portage]]. You'll probably want to make sure that "truetype" is enabled in the [[use flags]], as this allows you to open and use [[TrueType]] fonts.
Ubuntu generally comes with ImageMagick already installed. To check if ImageMagick is installed, you don't actually type ''imagemagick'', but rather one the several commands that are part of the package. For instance, try:
<source lang="text">
convert
</source>
 
 
=== Installing ImageMagick ===
 
<source lang="text">
sudo apt-get install imagemagick
</source>
 
 
=== Seeing what version of ImageMagick you have ===
 
Use the --version option with an ImageMagick command.
 
<source lang="text">
convert --version
</source>
 
 
=== Creating new images (without changing any originals) ===
 
Use convert. Convert always produces a new output file, generally you give the output file at the end of the command.
 
<source lang="text">
convert original.png -resize 160x120 icon.png
</source>
 
 
=== Modifying images "in-place" (changing the originals) ===
 
Use mogrify. Mogrify supports the same options as convert, only it modifies it's input, replacing the original (so you need to be careful with it).
 
Mogrify can be used with a wildcard to modify many files at once.
 
<source lang="text">
mogrify -resize 320x240 *.JPG
</source>
 
 
=== Getting Information about an Image ===
 
identify original.png
 
=== Displaying an image ===
 
display foo.png
 
=== Finding more examples of how to use ImageMagick ===
 
http://www.imagemagick.org/Usage/
 
== Cutting ==
 
=== Cutting out a particular rectangle from an image ===
 
The format of the crop option is: WIDTHxHEIGHT+LEFT+TOP
 
For instance, to extract a 256 pixel square from an image 500 pixels from the left, at the top (0):
 
<source lang="text">
convert FILE0058.JPG -crop 256x256+500+0 tile.jpg
</source>
 
 
=== Cuting an image up into tiles or strips / "Cookie Cutting" ===
 
The crop command, when not given a specific position to cut out, will repeat as many times as it can, "cookie-cutter style", producing a series of images.
 
<source lang="text">
convert original.png -crop 64x64 tile%04d.png
convert original.png -crop 64x strip%04d.png
convert original.png -crop x64 bar%04d.png
</source>


=== Mac OS X ===


There is now a "binary distribution" for Mac OS X on the [http://www.imagemagick.org main imagemagick website]. You simply unpack the files, move the resulting folder to a convenient location (say in your home directory), then tweak some environment variables so your system can find everything.
== Transforming ==


== Command line Tools ==
=== Converting/Changing image formats ===


Here's a list of the command-line tools that get installed:
<source lang="text">
convert original.png new.jpg
convert original.png -quality 1 lo.jpg
</source>


* animate
* compare
* composite
* conjure
* convert
* display
* identify
* import
* Magick-config
* Magick++-config
* mogrify
* montage
* Wand-config


== Resources ==
=== Resizing images ===


* [http://www.imagemagick.org/ Imagemagick website]
<source lang="text">
* [http://www.imagemagick.org/Usage Usage examples]
convert -resize 640x640 FILE0058.JPG work.png
* [http://www-128.ibm.com/developerworks/library/l-graf/?ca=dnt-428 Graphics from the command-line]
convert -resize 640x640! FILE0058.JPG work.png
</source>


== Working with raw image data ==


ImageMagick supports using an "explicit image format" syntax when opening image data. This includes special "raw" formats: gray, rgb, ...
== Assembling ==


Example:
=== Creating an animated gif from multiple images ===
<source lang="bash">
<source lang="text">
convert -size 640x480 -depth 8 rgb:image image.png
convert tile*.png slide.gif
convert -delay 5 -dispose background -page +0+0 tile*.png slide.gif
</source>
</source>


http://www.imagemagick.org/script/command-line-processing.php


=== Seeing available (output) formats ===
=== Overlaying/Combining 2 images into a new image ===
 
Use composite. Composite is both a command-line tool on its own, and also it's available via the "-composite" option of convert. Note that the order of things is different depending on which way you use.


<source lang="bash">
=== Creating a "contact sheet" / table of images ===
convert -list format
 
Use montage.
 
== Drawing ==
 
TODO: check circle parameters (radius one number only?!)
 
<source lang="text">
convert -size 300x300 xc:lightgray -fill black -draw 'rectangle 0,0 150,150' rect.gif
convert -size 300x300 xc:lightgray -fill black -draw 'circle 150,150 50,40' circle.gif
</source>
</source>
=== Finding more examples of drawing with ImageMagick ===
http://www.imagemagick.org/Usage/draw/#primitives
[[Category:Cookbook]]

Revision as of 21:32, 23 September 2010

Manipulating images on the command-line with ImageMagick

General

Checking if you have ImageMagick installed

Ubuntu generally comes with ImageMagick already installed. To check if ImageMagick is installed, you don't actually type imagemagick, but rather one the several commands that are part of the package. For instance, try:

convert


Installing ImageMagick

sudo apt-get install imagemagick


Seeing what version of ImageMagick you have

Use the --version option with an ImageMagick command.

convert --version


Creating new images (without changing any originals)

Use convert. Convert always produces a new output file, generally you give the output file at the end of the command.

convert original.png -resize 160x120 icon.png


Modifying images "in-place" (changing the originals)

Use mogrify. Mogrify supports the same options as convert, only it modifies it's input, replacing the original (so you need to be careful with it).

Mogrify can be used with a wildcard to modify many files at once.

mogrify -resize 320x240 *.JPG


Getting Information about an Image

identify original.png

Displaying an image

display foo.png

Finding more examples of how to use ImageMagick

http://www.imagemagick.org/Usage/

Cutting

Cutting out a particular rectangle from an image

The format of the crop option is: WIDTHxHEIGHT+LEFT+TOP

For instance, to extract a 256 pixel square from an image 500 pixels from the left, at the top (0):

convert FILE0058.JPG -crop 256x256+500+0 tile.jpg


Cuting an image up into tiles or strips / "Cookie Cutting"

The crop command, when not given a specific position to cut out, will repeat as many times as it can, "cookie-cutter style", producing a series of images.

convert original.png -crop 64x64 tile%04d.png
convert original.png -crop 64x strip%04d.png
convert original.png -crop x64 bar%04d.png


Transforming

Converting/Changing image formats

convert original.png new.jpg
convert original.png -quality 1 lo.jpg


Resizing images

convert -resize 640x640 FILE0058.JPG work.png
convert -resize 640x640! FILE0058.JPG work.png


Assembling

Creating an animated gif from multiple images

convert tile*.png slide.gif
convert -delay 5 -dispose background -page +0+0 tile*.png slide.gif


Overlaying/Combining 2 images into a new image

Use composite. Composite is both a command-line tool on its own, and also it's available via the "-composite" option of convert. Note that the order of things is different depending on which way you use.

Creating a "contact sheet" / table of images

Use montage.

Drawing

TODO: check circle parameters (radius one number only?!)

convert -size 300x300 xc:lightgray -fill black -draw 'rectangle 0,0 150,150' rect.gif
convert -size 300x300 xc:lightgray -fill black -draw 'circle 150,150 50,40' circle.gif


Finding more examples of drawing with ImageMagick

http://www.imagemagick.org/Usage/draw/#primitives