Image Conversion with 'convert' Command
Basic Image Conversion
The convert command, part of ImageMagick or GraphicsMagick, is a powerful command-line utility for image manipulation. It allows you to convert images between various formats, resize them, and perform many other operations.
Common Conversion Tasks
Here are some common examples of using the convert command for image transformations:
Converting File Types
To convert a file from one format to another, simply specify the input and output filenames with their respective extensions.
# To convert a file from jpg to pdf
convert original.jpg converted.pdf
Resizing Images
You can resize images to specific dimensions or maintain aspect ratios.
# To resize an image to a fixed width and proportional height:
convert original.jpg -resize 100x converted.jpg
# To resize an image to a fixed height and proportional width:
convert original.jpg -resize x100 converted.jpg
# To resize an image to a fixed width and height (may distort aspect ratio):
convert original.jpg -resize 100x100 converted.jpg
# To resize an image and simultaneously change its file type:
convert original.jpg -resize 100x converted.png
Batch Image Conversion
For converting multiple images, a shell loop is an effective method.
# To resize all of the images within a directory:
for file in `ls original/image/path/`;
do new_path=${file%.*};
new_file=`basename $new_path`;
convert $file -resize 150 converted/image/path/$new_file.png;
done
Converting PDF Pages to Images
The convert command can also handle multi-page documents like PDFs.
# To convert an N page pdf to N images (will autonumber):
convert -density 150 arch1.pdf -quality 80 'output.jpg'
# To convert an N page pdf to N images with explicit filename formatting:
convert -density 150 arch1.pdf -quality 80 'output-%d.jpg'