How To Convert Images Using Linux

woman using laptop
PeopleImages.com/DigitalVision/Getty Images

Introduction

This guide will show you how to manipulate images using the Linux command line.

You will find out how to resize an image both in terms of file size and in scale. You will also learn how to convert between multiple file types such as from JPG to PNG or GIF to TIF.

The Convert Command

The convert command is used to convert an image. The format is as follows:

convert [input options] input file [output options] output file.

How To Resize An Image

If you are going to include an image on a webpage and you want it to be a particular size then you could use some CSS to resize the image.

It is actually better though to upload the image as the correct size in the first place and insert it into the page.

This is of course just one example why you might want to resize an image. 

To resize an image use the following command

convert imagename.jpg -resize dimensions newimagename.jpg

For example to convert an image to be 800x600 you would use the following command:

convert imagename.jpg -resize 800x600 newimagename.jpg

If by converting to the specified dimensions the aspect ratio will be messed up the image will be resize to the closest ratio.

To force the conversion to be the exact size use the following command:

convert imagename.jpg -resize 800x600! newimagename.jpg

You don't have to specify the height and the width as part of the resize command.

For example if you want the width to be 800 and you don't care about the height use the following command:

convert imagename.jpg -resize 800 newimagename.jpg

To resize an image to be a specified height use the following command:

convert imagename,jpg -resize x600 newimagename.jpg

How To Convert From One Image Format To Another

If you have a JPG file and you wish to convert it to a PNG then you would use the following command:

convert image.jpg image.png

You can combine many different file formats. For example

convert image.png image.gif

convert image.jpg image.bmp

convert image.gif image.tif

How To Adjust The File Size For An Image

There are a number of ways to change the physical file size of an image. 

  1. Change the aspect ratio (make it smaller)
  2. Change the file format
  3. Change the compression quality

Reducing the size of the image will make the file size smaller. In addition using a file format that includes compression such as JPG will enable you to reduce the physical file size.

Finally adjusting the quality will make the physical file size smaller.

The previous 2 sections showed you how to adjust the size and file type. To compress the image try the following command:

convert imagename.jpg -quality 90 newimage.jpg

The quality is specified as a percentage. The lower the percentage the smaller the output file but obviously the final output quality is not as good.

How To Rotate Images

If you have taken a photo in portrait but you want it to be a landscape image you can rotate the image using the following command:

convert imagename.jpg -rotate 90 newimage.jpg

You can specify any angle for rotation.

For example try this out

convert imagename.jpg -rotate 45 newimage.jpg

Convert Command Line Options

There are dozens of command line options that can be used with the convert command as shown here:

Options are processed in command line order. Any option you specify on the command line remains in effect for the set of images that follows, until the set is terminated by the appearance of any option or -noop. Some options only affect the decoding of images and others only the encoding. The latter can appear after the final group of input images.

For a more detailed description of each option, see Imagemagick.

-adjoin

join images into a single multi-image file

-affine

drawing transform matrix

-antialias

remove pixel aliasing

-append

append a set of images

-average

average a set of images

-background

the background color

-blur x

blur the image with a gaussian operator

-border x

surround the image with a border of color

-bordercolor

the border color

-box

set the color of the annotation bounding box

-cache

megabytes of memory available to the pixel cache

-channel

the type of channel

-charcoal

simulate a charcoal drawing

-chop x{+-}{+-}{%}

remove pixels from the interior of an image

-clip

apply the clipping path, if one is present

-coalesce

merge a sequence of images

-colorize

colorize the image with the pen color

-colors

preferred number of colors in the image

-colorspace

the type of colorspace

-comment

annotate an image with a comment

-compose

the type of image composition

-compress

the type of image compression

-contrast

enhance or reduce the image contrast

-crop x{+-}{+-}{%}

preferred size and location of the cropped image

-cycle

displace image colormap by amount

-debug

enable debug printout

-deconstruct

break down an image sequence into constituent parts

-delay <1/100ths of a second>

display the next image after pausing

-density x

vertical and horizontal resolution in pixels of the image

-depth

depth of the image

-despeckle

reduce the speckles within an image

-display

specifies the X server to contact

-dispose

GIF disposal method

-dither

apply Floyd/Steinberg error diffusion to the image

-draw

annotate an image with one or more graphic primitives

-edge

detect edges within an image

-emboss

emboss an image

-encoding

specify the font encoding

-endian

specify endianness (MSB or LSB) of output image

-enhance

apply a digital filter to enhance a noisy image

-equalize

perform histogram equalization to the image

-fill

color to use when filling a graphic primitive

-filter

use this type of filter when resizing an image

-flatten

flatten a sequence of images

-flip

create a "mirror image"

-flop

create a "mirror image"

-font

use this font when annotating the image with text

-frame x++

surround the image with an ornamental border

-fuzz {%}

colors within this distance are considered equal

-gamma

level of gamma correction

-gaussian x

blur the image with a gaussian operator

-geometry x{+-}{+-}{%}{@} {!}{<}{>}

preferred size and location of the Image window.

-gravity

direction primitive gravitates to when annotating the image.

-help

print usage instructions

-implode

implode image pixels about the center

-intent

use this type of rendering intent when managing the image color

-interlace

the type of interlacing scheme

-label

assign a label to an image

-level

adjust the level of image contrast

-list

the type of list

-loop

add Netscape loop extension to your GIF animation

-map

choose a particular set of colors from this image

-mask

Specify a clipping mask

-matte

store matte channel if the image has one

-median

apply a median filter to the image

-modulate

vary the brightness, saturation, and hue of an image

-monochrome

transform the image to black and white

-morph

morphs an image sequence

-mosaic

create a mosaic from an image sequence

-negate

replace every pixel with its complementary color

-noise

add or reduce noise in an image

-noop

NOOP (no option)

-normalize

transform image to span the full range of color values

-opaque

change this color to the pen color within the image

-page x{+-}{+-}{%}{!}{<}{>}

size and location of an image canvas

-paint

simulate an oil painting

-pen

specify the pen color for drawing operations

-ping

efficiently determine image characteristics

-pointsize

pointsize of the Postscript, OPTION1, or TrueType font

-preview

image preview type

-process

process a sequence of images

-profile

add ICM, IPTC, or generic profile to image

-quality

JPEG/MIFF/PNG compression level

-raise x

lighten or darken image edges

-region x{+-}{+-}

apply options to a portion of the image

-resize x{%}{@}{!}{<}{>}

resize an image

-roll {+-}{+-}

roll an image vertically or horizontally

-rotate {<}{>}

apply Paeth image rotation to the image

-sample

scale image with pixel sampling

-sampling_factor x

sampling factors used by JPEG or MPEG-2 encoder and YUV decoder/encoder.

-scale

scale the image.

-scene

set scene number

-seed

pseudo-random number generator seed value

-segment x

segment an image

-shade x

shade the image using a distant light source

-sharpen x

sharpen the image

-shave x

shave pixels from the image edges

-shear x

shear the image along the X or Y axis

-size x{+offset}

width and height of the image

-solarize

negate all pixels above the threshold level

-spread

displace image pixels by a random amount

-stroke

color to use when stroking a graphic primitive

-strokewidth

set the stroke width

-swirl

swirl image pixels about the center

-texture

name of texture to tile onto the image background

-threshold

threshold the image

-tile

tile image when filling a graphic primitive

-transform

transform the image

-transparent

make this color transparent within the image

-treedepth

tree depth for the color reduction algorithm

-trim

trim an image

-type

the image type

-units

the type of image resolution

-unsharp x

sharpen the image with an unsharp mask operator

-use_pixmap

use the pixmap

-verbose

print detailed information about the image

-view

FlashPix viewing parameters

-wave x

alter an image along a sine wave

-write

write an image sequence [convert, composite]

Summary

For more information read the manual page for the convert command:

man convert