How to Convert Images Using Linux

woman using laptop
PeopleImages.com/DigitalVision/Getty Images

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 resized 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 you can 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.

-adjoinjoin images into a single multi-image file
-affinedrawing transform matrix
-antialiasremove pixel aliasing
-appendappend a set of images
-averageaverage a set of images
-backgroundthe background color
-blur xblur the image with a gaussian operator
-border xsurround the image with a border of color
-bordercolorthe border color
-boxset the color of the annotation bounding box
-cachemegabytes of memory available to the pixel cache
-channelthe type of channel
-charcoalsimulate a charcoal drawing
-chop x{+-}{+-}{%}remove pixels from the interior of an image
-clipapply the clipping path, if one is present
-coalescemerge a sequence of images
-colorizecolorize the image with the pen color
-colorspreferred number of colors in the image
-colorspacethe type of colorspace
-commentannotate an image with a comment
-composethe type of image composition
-compressthe type of image compression
-contrastenhance or reduce the image contrast
-crop x{+-}{+-}{%}preferred size and location of the cropped image
-cycledisplace image colormap by amount
-debugenable debug printout
-deconstructbreak down an image sequence into constituent parts
-delay <1/100ths of a second>display the next image after pausing
-density xvertical and horizontal resolution in pixels of the image
-depthdepth of the image
-despecklereduce the speckles within an image
-displayspecifies the X server to contact
-disposeGIF disposal method
-ditherapply Floyd/Steinberg error diffusion to the image
-drawannotate an image with one or more graphic primitives
-edgedetect edges within an image
-embossemboss an image
-encodingspecify the font encoding
-endianspecify endianness (MSB or LSB) of output image
-enhanceapply a digital filter to enhance a noisy image
-equalizeperform histogram equalization to the image
-fillcolor to use when filling a graphic primitive
-filteruse this type of filter when resizing an image
-flattenflatten a sequence of images
-flipcreate a "mirror image"
-flopcreate a "mirror image"
-fontuse 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
-gammalevel of gamma correction
-gaussian xblur the image with a gaussian operator
-geometry x{+-}{+-}{%}{@} {!}{<}{>}preferred size and location of the Image window.
-gravitydirection primitive gravitates to when annotating the image.
-helpprint usage instructions
-implodeimplode image pixels about the center
-intentuse this type of rendering intent when managing the image color
-interlacethe type of interlacing scheme
-labelassign a label to an image
-leveladjust the level of image contrast
-listthe type of list
-loopadd Netscape loop extension to your GIF animation
-mapchoose a particular set of colors from this image
-maskSpecify a clipping mask
-mattestore matte channel if the image has one
-medianapply a median filter to the image
-modulatevary the brightness, saturation, and hue of an image
-monochrometransform the image to black and white
-morphmorphs an image sequence
-mosaiccreate a mosaic from an image sequence
-negatereplace every pixel with its complementary color
-noiseadd or reduce noise in an image
-noopNOOP (no option)
-normalizetransform image to span the full range of color values
-opaquechange this color to the pen color within the image
-page x{+-}{+-}{%}{!}{<}{>}size and location of an image canvas
-paintsimulate an oil painting
-penspecify the pen color for drawing operations
-pingefficiently determine image characteristics
-pointsizepointsize of the Postscript, OPTION1, or TrueType font
-previewimage preview type
-processprocess a sequence of images
-profileadd ICM, IPTC, or generic profile to image
-qualityJPEG/MIFF/PNG compression level
-raise xlighten 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
-samplescale image with pixel sampling
-sampling_factor xsampling factors used by JPEG or MPEG-2 encoder and YUV decoder/encoder.
-scalescale the image.
-sceneset scene number
-seedpseudo-random number generator seed value
-segment xsegment an image
-shade xshade the image using a distant light source
-sharpen xsharpen the image
-shave xshave pixels from the image edges
-shear xshear the image along the X or Y axis
-size x{+offset}width and height of the image
-solarizenegate all pixels above the threshold level
-spreaddisplace image pixels by a random amount
-strokecolor to use when stroking a graphic primitive
-strokewidthset the stroke width
-swirlswirl image pixels about the center
-texturename of texture to tile onto the image background
-thresholdthreshold the image
-tiletile image when filling a graphic primitive
-transformtransform the image
-transparentmake this color transparent within the image
-treedepthtree depth for the color reduction algorithm
-trimtrim an image
-typethe image type
-unitsthe type of image resolution
-unsharp xsharpen the image with an unsharp mask operator
-use_pixmapuse the pixmap
-verboseprint detailed information about the image
-viewFlashPix viewing parameters
-wave xalter an image along a sine wave
-writewrite an image sequence [convert, composite]

 

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

Was this page helpful?