Difference between revisions of "Make, modify and convert images with ImageMagick"

From Parallel Library Services
Jump to navigation Jump to search
 
(35 intermediate revisions by the same user not shown)
Line 1: Line 1:
ImageMagick is a suite of [[command line]] tools for manipulating images. It's often called the "swiss army knife" of image tools. It is [[free software]].
[[ImageMagick]] is a powerful [[free software]] suite of [[command line]] tools for manipulating images. For this reason, it's often called the "swiss army knife" of image tools.


== Links ==
== Resources ==
* http://www.imagemagick.org/
* [http://www.imagemagick.org/ imagemagick.org]
* [http://www.imagemagick.org/Usage/ Usage tutorial]
* [http://www.imagemagick.org/script/command-line-processing.php#geometry Geometry specifications]


* [http://www.imagemagick.org/Usage/ usage] tutorial
== ImageMagick tools ==
ImageMagick provides a number of [[command line]] tools, none of which are named ImageMagick, but instead:


* Several commands use [http://www.imagemagick.org/script/command-line-processing.php#geometry geometry] specifications
* '''convert''',  which creates/modifies an image and saves it as a new file
* '''mogrify''', which makes changes to an image "in-place"
* '''montage''', which creates a "thumbnail" image of a number of images in a grid
* '''identify''', which displays information about an image
* '''display''', which shows an image in a window


== General ==
ImageMagick often comes with operating systems, such as Linux distributions.
Imagemagick provides a number of [[command line]] tools, none of which are named ImageMagick, but rather:
Type the name of one of the above commands in a terminal/shell, such as:
* ''convert'': create/modify an image and save as a new file
 
* ''mogrify'': makes changes to an image "in-place" (replacing the original -- use with caution!)
<syntaxhighlight lang="bash">convert</syntaxhighlight>
* ''montage'': automated "poster" maker that creates a "thumbnail" image of a number of images (in a grid with variable spacing and options like showing the filename)
* ''identify'': display information about an image
* ''display'': show an image in a window (may not work on OS X, use the "open" command instead)


== How to check if ImageMagick is installed ==
If ImageMagick is installed on your machine, you should see an extensive manual of options for the convert tool.
Type the name of one of the above commands in a terminal/shell, such as:
convert


=== Installing ImageMagick ===
== Installing ImageMagick ==


In Linux, ImageMagick should either already be installed, or is available via whatever package manager the distribution supports. In Debian/Ubuntu:
In Linux, ImageMagick should either already be installed, or is available via whatever package manager the distribution supports. In Debian/Ubuntu:


sudo apt-get install imagemagick
<syntaxhighlight lang="bash">sudo apt-get install imagemagick</syntaxhighlight>


or on Mac OS, try:
or on Mac OS, try:


brew install imagemagick
<syntaxhighlight lang="bash">brew install imagemagick</syntaxhighlight>


== Resizing A folder of Images with mogrify ==
== A (basic) guide to ImageMagick tools and their options ==


DANGER: this replaces all the images in the current folder with thumbnails, so you'd better make a copy first!
=== Checking the version of ImageMagick you have ===


mogrify -resize 320x240 *
Use the --version option with an ImageMagick command.


=== Seeing what version of ImageMagick you have ===
convert --version


Use the --version option with an ImageMagick command.
You should see something like this:


convert --version
<syntaxhighlight lang="bash">Version: ImageMagick 7.1.0-5 Q16 arm 2021-08-22 https://imagemagick.org</syntaxhighlight>


=== Creating new images (without changing any originals) ===
=== 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.
Use convert. This command always produces a new output file, named it is given at the end of the command.


convert original.png -resize 160x120 icon.png
<syntaxhighlight lang="bash">convert original.png -resize 160x120 icon.png</syntaxhighlight>


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


==== Resizing a folder of images with mogrify ====


=== Modifying images "in-place" (changing the originals) ===
<span style="background-color: yellow;">WARNING:</span> This command replaces all the images in the current folder with thumbnails, writing over the files that are there. If you want to keep the originals, make a copy first!
<syntaxhighlight lang="bash">mogrify -resize 320x240 *</syntaxhighlight>


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


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


mogrify -resize 320x240 *.JPG
<syntaxhighlight lang="bash">mogrify -resize 320x240 *.JPG</syntaxhighlight>
 


=== Getting information about an image ===
=== Getting information about an image ===


<pre>identify original.png
<syntaxhighlight lang="bash">identify original.png</syntaxhighlight>


=== Displaying an image ===
=== Displaying an image ===


<pre>display foo.png
<syntaxhighlight lang="bash">display foo.png</syntaxhighlight>


== pdfunite ==
=== Cutting ===


From package: poppler-utils
==== Cutting out a particular-shaped rectangle from an image ====
 
== Cutting ==
 
=== Cutting out a particular-shaped rectangle from an image ===


The format of the crop option is: WIDTHxHEIGHT+LEFT+TOP
The format of the crop option is: WIDTHxHEIGHT+LEFT+TOP
Line 79: Line 79:
For instance, to extract a 256 pixel square from an image 500 pixels from the left, at the top (0):
For instance, to extract a 256 pixel square from an image 500 pixels from the left, at the top (0):


<pre>convert FILE0058.JPG -crop 256x256+500+0 tile.jpg
<syntaxhighlight lang="bash">convert FILE0058.JPG -crop 256x256+500+0 tile.jpg</syntaxhighlight>


=== Cutting an image up into tiles or strips, aka "Cookie Cutting" ===
==== Cutting an image up into tiles or strips, aka "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.
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.


<pre>
<syntaxhighlight lang="bash" line>
convert original.png -crop 64x64 tile%04d.png
convert original.png -crop 64x64 tile%04d.png
convert original.png -crop 64x strip%04d.png
convert original.png -crop 64x strip%04d.png
convert original.png -crop x64 bar%04d.png
convert original.png -crop x64 bar%04d.png
</pre>
</syntaxhighlight>


== Transforming ==
=== Transforming ===


=== Converting/Changing image formats ===
==== Converting/Changing image formats ====


<pre>
<syntaxhighlight lang="bash" line>
convert original.png new.jpg
convert original.png new.jpg
convert original.png -quality 1 lo.jpg
convert original.png -quality 1 lo.jpg
</source>
</syntaxhighlight>


==== Resizing images ====


=== Resizing images ===
<syntaxhighlight lang="bash" line>
 
<pre>
convert -resize 640x640 FILE0058.JPG work.png
convert -resize 640x640 FILE0058.JPG work.png
convert -resize 640x640! FILE0058.JPG work.png
convert -resize 640x640! FILE0058.JPG work.png
</source>
</syntaxhighlight>


=== Assembling ===


== Assembling ==
==== Creating an animated gif from multiple images ====
 
<syntaxhighlight lang="bash" line>
=== Creating an animated gif from multiple images ===
<pre>
convert tile*.png slide.gif
convert tile*.png slide.gif
convert -delay 5 -dispose background -page +0+0 tile*.png slide.gif
convert -delay 5 -dispose background -page +0+0 tile*.png slide.gif
</source>
</syntaxhighlight>
 


=== Overlaying/Combining 2 images into a new image ===
=== 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.
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 it.


=== Creating a "contact sheet" / table of images ===
=== Creating a "contact sheet" / table of images ===
This tool arranges images in a grid, based on the specifications given in the options and output filename.


Use ''montage''.
Using montage, arrange the frames as tiles on new jpg files. The -geometry option describes how big each image is resized to (640) and the spacing in between the tiles (+10+10).
 
Arrange the frames as tiles on new jpg files.
Geometry describes how big each image is resized to (640) and the spacing in between the tiles (+10+10).


%04d in the output filename means that the results are numbered starting from 0001 (0-padded to 4 places).
%04d in the output filename means that the results are numbered starting from 0001 (0-padded to 4 places).


<syntaxhighlight lang=bash>
<syntaxhighlight lang="bash">
montage -geometry '640x640>+10+10' -tile 1x3 /*.jpg test%04d.jpg
montage -geometry '640x640>+10+10' -tile 1x3 /*.jpg test%04d.jpg
</syntaxhighlight>
</syntaxhighlight>


== Drawing ==
=== Drawing ===
 
Create shapes using convert.
TODO: check circle parameters (radius one number only?!)
<syntaxhighlight lang="bash" line>
 
<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 'rectangle 0,0 150,150' rect.gif
convert -size 300x300 xc:lightgray -fill black -draw 'circle 150,150 50,40' circle.gif
convert -size 300x300 xc:lightgray -fill black -draw 'circle 150,150 50,40' circle.gif
</source>
</syntaxhighlight>
 


=== Finding more examples of drawing with ImageMagick ===
=== More examples of drawing with ImageMagick ===


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


[[Category:Cookbook]]
[[Category:Cookbook]]
 
[[Category:ImageMagick]]
 
https://imagemagick.org/index.php
 
Use ImageMagick® to create, edit, compose, or convert digital images. It can read and write images in a variety of formats (over 200) including PNG, JPEG, GIF, WebP, HEIC, SVG, PDF, DPX, EXR and TIFF. ImageMagick can resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.
 
ImageMagick is free software delivered as a ready-to-run binary distribution or as source code that you may use, copy, modify, and distribute in both open and proprietary applications. It is distributed under a derived Apache 2.0 license.
 
[[Category: Tools]]

Latest revision as of 17:54, 2 November 2021

ImageMagick is a powerful free software suite of command line tools for manipulating images. For this reason, it's often called the "swiss army knife" of image tools.

Resources

ImageMagick tools

ImageMagick provides a number of command line tools, none of which are named ImageMagick, but instead:

  • convert, which creates/modifies an image and saves it as a new file
  • mogrify, which makes changes to an image "in-place"
  • montage, which creates a "thumbnail" image of a number of images in a grid
  • identify, which displays information about an image
  • display, which shows an image in a window

ImageMagick often comes with operating systems, such as Linux distributions. Type the name of one of the above commands in a terminal/shell, such as:

convert

If ImageMagick is installed on your machine, you should see an extensive manual of options for the convert tool.

Installing ImageMagick

In Linux, ImageMagick should either already be installed, or is available via whatever package manager the distribution supports. In Debian/Ubuntu:

sudo apt-get install imagemagick

or on Mac OS, try:

brew install imagemagick

A (basic) guide to ImageMagick tools and their options

Checking the version of ImageMagick you have

Use the --version option with an ImageMagick command.

convert --version

You should see something like this:

Version: ImageMagick 7.1.0-5 Q16 arm 2021-08-22 https://imagemagick.org

Creating new images (without changing any originals)

Use convert. This command always produces a new output file, named it is given at the end of the command.

convert original.png -resize 160x120 icon.png

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

Resizing a folder of images with mogrify

WARNING: This command replaces all the images in the current folder with thumbnails, writing over the files that are there. If you want to keep the originals, make a copy first!

mogrify -resize 320x240 *

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

It 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

Cutting

Cutting out a particular-shaped 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

Cutting an image up into tiles or strips, aka "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 it.

Creating a "contact sheet" / table of images

This tool arranges images in a grid, based on the specifications given in the options and output filename.

Using montage, arrange the frames as tiles on new jpg files. The -geometry option describes how big each image is resized to (640) and the spacing in between the tiles (+10+10).

%04d in the output filename means that the results are numbered starting from 0001 (0-padded to 4 places).

montage -geometry '640x640>+10+10' -tile 1x3 /*.jpg test%04d.jpg

Drawing

Create shapes using convert.

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

More examples of drawing with ImageMagick

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