ImageMagick is a free software that lets you create/edit/convert images via command line. You can download and install ImageMagick for Linux, Windows, and Mac Operating System.
ImageMagick provides a lot of features that you can apply to an image like:
– get detailed image information
– resize, crop image
– flip and rotate image
– blur, sharpen, distort image
– adjust image colors
– draw text, line, and different shapes in image
– overlap one image into another
– add border or frame to an image
– convert an image from one format to another (e.g. PNG to JPEG)
– has support for large size images (mega, giga, tera pixel image size)
– recognize patterns and shapes in image
The basic style of the command of ImageMagick is as follows:
command [options] image1 [options] image2 [options] output_image
The ImageMagick site has lots of good examples of commands to perform various image operations. I will be listing some of the basic examples of the commands over here.
Get Image Information
We can use ImageMagick’s identify
program to get the detailed information about any image.
It can:
– describe the format and characteristics of one or more image files
– report if an image is incomplete or corrupt
– get width and height of an image
– check if the image is colormapped or not
– get the number of byets in the image
– get the format of the image (JPEG, PNG, etc.)
– get the number of seconds that ImageMagick took to read and process the image
– get more detailed information using the-verbose
option
The identify
program can also be used to get the installed version number of ImageMagick itself by running the following command:
magick identify --version
The output will be something like this:
Version: ImageMagick 7.0.7-23 Q16 x86_64 2018-02-19 http://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib
Get information of an image
magick identify shadow.png
Output will be something like this:
shadow.png PNG 82x58 82x58+0+0 8-bit sRGB 8702B 0.000u 0:00.000
Get verbose information of an image
Get the detailed information of the same image:
magick identify -verbose shadow.png
Output will be something like this:
Image: shadow.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 82x58+0+0
Units: Undefined
Colorspace: sRGB
Type: TrueColorAlpha
Base type: Undefined
Endianess: Undefined
Depth: 8-bit
Channel depth:
Red: 8-bit
Green: 8-bit
Blue: 8-bit
Alpha: 8-bit
Channel statistics:
Pixels: 4756
Red:
min: 0 (0)
max: 255 (1)
mean: 98.6529 (0.386874)
standard deviation: 88.8551 (0.348451)
kurtosis: -1.25895
skewness: 0.359817
entropy: 0.770452
Green:
min: 0 (0)
max: 255 (1)
mean: 60.4327 (0.236991)
standard deviation: 60.0569 (0.235517)
kurtosis: 1.88261
skewness: 1.31123
entropy: 0.700643
Blue:
min: 24 (0.0941176)
max: 255 (1)
mean: 95.8192 (0.375761)
standard deviation: 50.5063 (0.198064)
kurtosis: 1.16538
skewness: 1.07072
entropy: 0.69015
Alpha:
min: 0 (0)
max: 255 (1)
mean: 196.743 (0.771542)
standard deviation: 92.1225 (0.361265)
kurtosis: -0.347426
skewness: -1.1676
entropy: 0.406634
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 112.912 (0.442792)
standard deviation: 72.8852 (0.285824)
kurtosis: -1.18265
skewness: 0.436057
entropy: 0.641969
Alpha: srgba(0,0,128,0) #00008000
Rendering intent: Perceptual
Gamma: 0.45455
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Matte color: grey74
Background color: black
Border color: srgb(223,223,223)
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 82x58+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2018-08-12T16:24:30+06:00
date:modify: 2018-08-12T16:24:20+06:00
png:bKGD: chunk was found (see Background color, above)
png:cHRM: chunk was found (see Chromaticity, above)
png:gAMA: gamma=0.45454544 (See Gamma, above)
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 6
png:IHDR.color_type: 6 (RGBA)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 82, 58
png:sRGB: intent=0 (Perceptual Intent)
png:text: 2 tEXt/zTXt/iTXt chunks were found
png:tIME: 2018-08-12T22:24:20Z
signature: 817abb638e17f28c5ab4549ee9e1328850f1dfb9f23058ddb2150b809ea3ccb3
Artifacts:
verbose: true
Tainted: False
Filesize: 8702B
Number pixels: 4756
Pixels per second: 475600B
User time: 0.000u
Elapsed time: 0:01.009
Version: ImageMagick 7.0.7-23 Q16 x86_64 2018-02-19 http://www.imagemagick.org
Convert an image from one format to another
Here, we convert the image from PNG to JPEG format:
convert shadow.png shadow.jpeg
Resize and convert to another format:
convert shadow.jpeg -resize 50% shadow1.png
Resize Image
Resize image to 100×100 pixels by maintaining the aspect ratio
Suppose, if your input image is 800×600 pixels then the resized output image will be 100×75 pixels in size.
convert input.png -resize 100x100 output.png
Resize image to 100×100 pixels ignoring aspect ration
- the image will be resized exactly into 100×100 pixels
convert input.png -resize 100x100\! output.png
Only resize image that is larger than the resize size
- if we provide 200×200 pixel size image, then it will be resized to 100×100 px
- if we provide 50×50 pix image then it will not resize and remain as it is
- So, this is kind of Image Shrinking feature
convert input.png -resize 100x100\> output.png
Only resize image that is smaller than the resize size
- if we provide 200×200 pixel size image, then it will be resized to 100×100 px
- if we provide 50×50 pix image then it will not resize and remain as it is
- So, this is kind of Image Enlarging feature only
convert input.png -resize 100x100\< output.png
Resize image to percentage size by maintaining the aspect ratio
The following command will resize the image into half of its size.
Suppose, if your input image is 800×600 pixels then the resized output image will be 400×300 pixels in size.
convert input.png -resize 50% output.png
Rotate Image
Rotate image 90 degree clockwise
convert input.png -rotate "90" output.png
Rotate image 45 degree anti-clockwise/counter-clockwise
convert input.png -rotate "-45" output.png
Blur Image
Blur arguments are:
-blur {radius}x{sigma}
radius:
– how big an area the operator should look at when spreading pixels
– can be either ‘0’ or at a minimum double that of the sigma value
sigma:
– how much your want the image to ‘spread’ or blur, in pixels
– it’s kind of the size of the brush used to blur the image
– it can have floating point value
convert input.png -blur 0x8 output.png
Sharpen Image
Sharpen arguments are:
-blur {radius}x{sigma}
radius:
– can have integer value only
– should be at a minimum 1 or double of the sigma value
sigma:
– it is the real control of the sharpening operation
– it can have floating point value starting from .1
– the larger the sigma, the more it sharpens
– practically, no more than sigma value 3 for server sharpening
– 0.5 to 1 is a good range
convert input.png -sharpen 0x3 output.png
Encrypt and Decrypt an Image
You can scramble the entire image and make it unrecognizable by using the enciphering utility in ImageMagick.
-encipher option is used to scramble/encrypt the image
We will use a passphrase.txt
file to scramble the image. The file that is used to scramble the image is required to unscramble the image as well.
Let’s first run a command to create a file named passphrase.txt:
touch passphrase.txt
Scramble the image
convert shadow.jpeg -encipher passphrase.txt shadow-encrypted.jpeg
Unscramble the image
-decipher option is used to unscramble/decrypt the image
convert shadow-encrypted.jpeg -decipher passphrase.txt shadow-decrypted.jpeg
We can also use image file instead of the .txt
file to scramble and unscramble any image:
convert shadow.jpeg -encipher my-image.png shadow-encrypted.jpeg
convert shadow-encrypted.jpeg -decipher my-image.png shadow-decrypted.jpeg
Remember that the image named my-image.png
should be present in order to scramble and unscramble the image.
Overlap one image over another image
The composite program is used to overlay one image over another.
composite -gravity center smile.gif rose: rose-over.png
Convert Text into Image
convert -background lightyellow -fill brown -font TimesNewRoman \
-size 165x70 -pointsize 24 -gravity center \
label:Mukesh label_name.gif
Add multiple words text. But, if the text is long and the size of the image is small then the text will overflow from the image.
convert -background lightyellow -fill brown -font TimesNewRoman \
-size 200x100 -pointsize 24 -gravity center \
label:'My name is Mukesh Chapagain' label_name.gif
In that case, we can use caption instead of label. This will WordWrap the long text.
convert -background lightyellow -fill brown -font TimesNewRoman \
-size 200x100 -pointsize 24 -gravity center \
caption:'My name is Mukesh Chapagain' label_name.gif
You can use caption for multiple line words as well. It detects the \n character as new line.
convert -background lightyellow -fill brown -font TimesNewRoman \
-size 200x100 -pointsize 24 -gravity center \
caption:'My name is \n Mukesh Chapagain' label_name.gif
Draw Shapes in an Image
Using the -draw option in ImageMagick, we can draw various shapes like line, rectangle, circle, etc.
Create an Image and Draw a Point in it
convert -size 10x6 xc:skyblue -fill black \
-draw 'point 3,2' -scale 100x60 draw_point.gif
Create an Image and Draw a Rectangle in it
convert -size 100x60 xc:skyblue -fill white -stroke black \
-draw "rectangle 20,10 80,50" draw_rect.gif
convert -size 100x60 xc:skyblue -fill white -stroke black \
-draw "roundrectangle 20,10 80,50 20,15" draw_rrect.gif
Create an Image and Draw a Line in it
convert -size 100x60 xc:skyblue -fill white -stroke black \
-draw "line 20,50 90,10" draw_line.gif
Draw a line over an Image
The following command will draw a line over an already existing image.
convert rose: -fill none -stroke white -draw 'line 5,40 65,5' rose_raw.png
Hope this helps. Thanks.