The convert command (part of ImageMagick) can change the resolution of image files faster than you can count to F in hex.
The convert command from the ImageMagick suite of tools provides ways to make all sorts of changes to image files. Among these is an option to change the resolution of images. The syntax is simple, and the command runs extremely quickly. It can also convert a image from one format to another (e.g., jpg to png) as well as blur, crop, despeckle, dither, flip and join images and more.
Although the commands and scripts in this post mostly focus on jpg files, the convert command also works with a large variety of other image files, including png, bmp, svg, tiff, gif and such.
Basic resizing
To resize an image using the convert, you would use a command like this:
$ convert -resize 1200x1000 smile.jpg smile-2.jpg
The syntax is “convert -resize resolution currentfile newfile”.
The resolution should be expressed as the desired width (in pixels), followed by an “x” and then the desired height. Note that if the numbers aren’t numerically related to the current dimensions of the image, the resultant resolution might not be what you expect. Generating a 1200×1000 image from a 2400×2000 is one thing. Asking for it to be saved as a 2000×1200 will result in one that is only 1440×1200.
Using a script
If you intend to convert a number of images or will be resizing images often, it’s a good idea to use a script. The first script shown below would create a “smile_2.jpg” file from a “smile.jpg” file using the 1200×800 resolution. Note how it extracts the file extension from the filename so that it can build the new filename.
#!/bin/bash
if [ $# -eq 0 ]; then
echo -n "image file: "
read img
else
img=$1
fi
add=2
# get filetype and base name from argument
filetype=`echo $img | awk -F . '{print $2}'`
basename=`echo $img | sed 's/.jpg//'`
# target resolution
resolution=1200x800
# new file name will include "_2"
newfile="${basename}_${add}.$filetype"
# run convert command
convert -resize $resolution $img $newfile
# display the new file
ls -l $newfile
Resizing a group of image files
The next script will create a 1200×800 resolution file from each of the jpg files in the current directory and will display each of the new files after it has been set up.
#!/bin/bash
num=2
resolution=1200x800
for image in `ls *.jpg`; do
basename=`echo $image | sed "s/.jpg//"`
convert -resize $resolution $image ${basename}_${num}.jpg
ls -ltr | tail -1
done
If you run the script, you would see something like this:
$ resize_jpg_files -rw-r--r--. 1 shs shs 49321 May 25 09:52 camper_2.jpg -rw-r--r--. 1 shs shs 3872 May 25 09:52 map_2.jpg -rw-r--r--. 1 shs shs 3872 May 25 09:52 pig_2.jpg -rw-r--r--. 1 shs shs 130432 May 25 09:52 tree-cutting_2.jpg -rw-r--r--. 1 shs shs 45082 May 25 09:52 volcano_rings_2.jpg
Resizing by file type
The next script will ask you what type of image files to convert and will then run through the files of the type in the current directory. Like the previous script, it adds a “_2” to the file names to differentiate them from the originals. This can easily be changed, of course, if some other character or string works better for you.
#!/bin/bash
echo -n "file type: "
read filetype
add=2
resolution=800x600
for image in `ls *.$filetype`; do
echo $image
basename=`echo $image | sed "s/.$filetype//"`
convert -resize $resolution $image ${basename}_${add}.$filetype
ls -ltr | tail -1
done
Using multiple image resolutions
This last script asks for the name of a single image file and then creates new versions of the image using three different resolutions. It also adds the intended resolution to the file name.
#!/bin/bash
# ask for image file name
echo -n "image: "
read image
if [ ! -f $image ]; then
echo "No such file: $image"
exit 1
fi
# NOTE: resolution is height x width
for reso in 400x500 600x800 800x1200; do
basename=`echo $image | sed 's/.jpg//'`
convert -resize $reso $image ${basename}_${reso}.jpg
ls -ltr | tail -1
done
The resultant files might look like this:
$ ls -l dog* -rw-r--r--. 1 shs shs 14501 May 25 12:29 dog_400x500.jpg -rw-r--r--. 1 shs shs 28658 May 25 12:29 dog_600x800.jpg -rw-r--r--. 1 shs shs 45082 May 25 12:29 dog_800x1200.jpg -rw-r--r--. 1 shs shs 58628 May 25 12:25 dog.jpg
Wrap-up
The convert command makes resizing image files extremely easy. To learn more about some of the command’s other options, check out this post on converting and manipulating image files on the Linux command line.




