Convert Images Safely with ImageMagick 6 on Linux
A single -resize typo can silently distort every image convert touches, and it will not warn you. By the end of this guide you will have converted an image, made a predictable thumbnail, cropped a region and checked the result from the shell. The examples use the installed ImageMagick 6 Q16 command, convert-im6.q16. The shorter names convert and convert-im6 are aliases for the same command on this system.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about 10 minutes if ImageMagick is already installed. You need a shell, a readable source image and write access to the directory holding the output. No example needs elevated privileges. Do not use sudo to work around an ordinary file permission problem without first checking the path and its owner.
1. Check the installed command
This guide was tested with ImageMagick 6.9.12-98 Q16, from the Debian package imagemagick-6.q16, version 8:6.9.12.98+dfsg1-5.2ubuntu0.1~esm13. Check your own host because option support and delegate support can differ between package builds:
$ convert-im6.q16 -version
Version: ImageMagick 6.9.12-98 Q16 x86_64
$ dpkg-query -W -f='${Package} ${Version}\n' imagemagick-6.q16
imagemagick-6.q16 8:6.9.12.98+dfsg1-5.2ubuntu0.1~esm13
The exact feature and delegate lines vary. The useful checks are the command name, the ImageMagick major version and the package version. If your host only provides convert, substitute it in the commands below.
2. Prepare a source and inspect it
Set a source path that really exists. The shell variable keeps the examples readable and avoids repeating a filename. The command writes nothing:
$ SRC="$HOME/Pictures/input.jpg"
$ test -r "$SRC" && echo "source is readable"
source is readable
$ identify-im6.q16 "$SRC"
/home/you/Pictures/input.jpg JPEG 2400x1600 2400x1600+0+0 8-bit sRGB 1.2MiB 0.000u 0:00.000
identify-im6.q16 is useful for inspection, but the conversion syntax itself is the important part here: input options and the input file come first, followed by output options and the output file. ImageMagick normally infers the format from the filename suffix. You can also force one with a prefix such as png: or with a suffix such as output.png.
If the input is supplied by another person or service, treat it as untrusted data. Do not grant the conversion process unnecessary access to secrets, and use a disposable working directory when the file's origin is uncertain. Image processing can consume substantial CPU, memory or disk space on very large files.
3. Resize without overwriting the original
Make an output directory, then resize to fit within 1200 by 1200 pixels. The \> suffix means an image already smaller than that geometry is not enlarged:
$ OUT="$HOME/Pictures/converted"
$ mkdir -p "$OUT"
$ convert-im6.q16 "$SRC" -resize '1200x1200>' "$OUT/input-1200.jpg"
$ identify-im6.q16 "$OUT/input-1200.jpg"
/home/you/Pictures/converted/input-1200.jpg JPEG 1200x800 1200x800+0+0 8-bit sRGB 148KiB 0.000u 0:00.000
The output dimensions depend on the source aspect ratio. ImageMagick preserves that ratio when the geometry has both dimensions but no exclamation mark. A geometry such as 1200x1200! forces both dimensions and can distort the image, so use it only when distortion is intentional.
Do not put the source and destination in the same command position. A separate destination gives you a simple recovery path: if the result is wrong, delete only the generated file and run the command again.
4. Create a thumbnail and choose metadata behaviour
The -thumbnail operator is intended for small previews and removes profiles as part of its normal operation. Use it when that trade-off is acceptable:
$ convert-im6.q16 "$SRC" -thumbnail '320x320>' "$OUT/input-thumb.jpg"
$ identify-im6.q16 -format '%m %wx%h %[channels]\n' "$OUT/input-thumb.jpg"
JPEG 320x213 srgb
For a normal resize where embedded profiles and comments matter, use -resize and decide explicitly whether to strip metadata. The -strip option removes profiles and comments, which can reduce file size but can also remove colour-management data and useful attribution. This command deliberately produces a metadata-light derivative:
$ convert-im6.q16 "$SRC" -resize '1200x1200>' -strip "$OUT/input-web.jpg"
$ identify-im6.q16 "$OUT/input-web.jpg"
/home/you/Pictures/converted/input-web.jpg JPEG 1200x800 1200x800+0+0 8-bit sRGB 132KiB 0.000u 0:00.000
There is no undo inside convert for metadata removal. Keep the original until the derivative has been checked, and remove only the derivative if you need to recover from this example.
5. Crop a known rectangle
Crop uses geometry in the form widthxheight+x+y. This example takes a 600 by 400 pixel rectangle starting 100 pixels from the left and 50 pixels from the top:
$ convert-im6.q16 "$SRC" -crop '600x400+100+50' +repage "$OUT/input-crop.png"
$ identify-im6.q16 -format '%m %wx%h\n' "$OUT/input-crop.png"
PNG 600x400
+repage removes the virtual canvas offset left by some crop operations. It is useful when the cropped image will be used by another program that should see a clean 600 by 400 canvas. If the requested rectangle extends beyond the source, the result may include background or have different dimensions than expected, so inspect it rather than trusting the command's exit status alone.
6. Read properties without creating a file
Use info: as the output target when you want formatted information on standard output instead of another image. The -format setting accepts image properties such as width, height and the first pixel:
$ convert-im6.q16 "$SRC" -format '%wx%h %[channels]\n' info:
2400x1600 sRGB
This is convenient in scripts, but quote the format string so the shell does not interpret characters in it. For a batch job, check the command's exit status and validate the output files after conversion. A successful process does not tell you whether the chosen dimensions, format or metadata policy match your requirements.
7. Avoid the common traps
- Keep options in order. The documented shape is input options, input file, output options, output file. A setting can affect only images that follow it in a sequence.
- Quote filenames and geometries. Spaces, wildcard characters and shell metacharacters are easy to misread when paths are assembled from user input.
- Use a new output filename while testing. ImageMagick can replace an existing destination, and that replacement is not a reversible edit.
- Check format support first. The installed version reports delegates such as JPEG, PNG, TIFF and WebP, but another package build may differ.
- Use
-qualitydeliberately. For example, JPEG quality changes compression behaviour; it is not a universal measure of visual quality for every output format.
If a command fails, read the first diagnostic, check that the input is readable, and inspect the destination directory. Do not add sudo until you know the operation genuinely needs access outside your account. If a generated file is wrong, remove that generated file and rerun from the unchanged source:
$ rm -- "$OUT/input-1200.jpg"
$ convert-im6.q16 "$SRC" -resize '1200x1200>' "$OUT/input-1200.jpg"
The rm command is destructive and needs no privilege in this example. Confirm the exact path before running it; never substitute a broad directory or an unreviewed variable.
Done means
- Version checked. You confirmed the installed ImageMagick 6 Q16 version.
- Source preserved. You inspected a readable source and kept it as the recovery copy.
- Output created. You created a resized image, thumbnail or crop at a separate destination.
- Result verified. You checked the output format and dimensions with
identify-im6.q16. - Metadata decided. You made an explicit decision about profiles, comments and metadata.
- Recovery clean. You can remove a generated derivative without touching the source.