Convert an image file from one format to another using ImageMagick
Tested on |
Debian (Etch, Lenny, Squeeze) |
Ubuntu (Lucid, Maverick, Natty, Precise, Trusty) |
Objective
To convert an image file from one format to another using ImageMagick
Scenario
Suppose that the file input.png
contains a PNG-format image. You wish to convert it to a JFIF-format image called output.jpeg
using ImageMagick.
Method
The method described here uses ImageMagick to convert the image. On Debian-based systems, the command-line interface to ImageMagick is provided by the package imagemagick
:
apt-get install imagemagick
Conversion is performed using the convert
command:
convert input.png output.jpeg
By default:
- The input format is deduced from the file content if it begins with a recognised signature. Failing that, the format is determined by the filename extension if there is one.
- The output format is chosen to match the filename extension if there is one.
Either or both of these choices can be overridden by specifying the file format explicitly:
convert png:input.png jpeg:output.jpeg
ImageMagick supports a large number of file formats, including BMP, GIF, JPEG, PNM, PNG and TIFF. A full list can be obtained using the identify
command:
identify -list format
Note that some formats require additional information to be specified such as the number of bits per pixel. Also, some of the less common formats can be read but not written, or written but not read.
Testing
You can check the format of an image using the identify
command:
identify output.jpeg
The first two fields of the output are the filename and the image format:
output.jpeg JPEG 641x481 641x481+0+0 DirectClass 8-bit 45.627kb
Further Reading
Tags: imagemagick