Grayscale Images with ImageMagick

Author: akeil
Date: 2014-05-10
Version: 2

Converting a photo to a grayscale version is simple enough with the ImageMagick [1] convert command:

$ convert photo.jpg -colorspace Gray out.jpg

However, the result leaves something to be desired. It looks "flat" and boring. Better results can be achieved with this command:

$ convert photo.jpg -type Grayscale out.jpg

If you want more control, use parameters to increase contrast. To make the best use of the available range from pure black to pure white, one can stretch contrast. That means the lightest pixels are converted to white and the darkest pixels to black. The other colors are distributed among the available range.

The -linear-stretch option allows a threshold parameter which defines the number of pixels that are allowed to be converted to pure white an d pure black. A higher threshold means that have a greater range of values to depict the midtones at the expense of details in the highlights and/or shadows.

$ convert photo.jpg -linear-stretch 1x1 -colorspace Rec709Luma out.jpg

Examples

Lock

Original photo

The original photo.

Photo converted to grayscale with default settings.

Simple conversion to grayscale colorspace loses most of the detail in the dark areas.

$ convert photo.jpg -colorspace Gray out.jpg
Photo with conversion after increased contrast

Increasing contrast before conversion makes some of the details and textures stand out more. The effect of pure black and pure white pixels is clearly visible.

$ convert photo.jpg -contrast-stretch 4x4 colorspace Gray out.jpg
Using a non-linear colorspace and linear stretch

Linear-stretch and non-linear colorspace gives a balanced result with good textures.

$ convert photo.jpg -linear-stretch 1x1 -colorspace Rec709Luma out.jpg

Window

Original photo

The original photo.

Photo converted to grayscale with default settings.

Simple conversion to grayscale colorspace clearly looks less interesting than the color version.

$ convert photo.jpg -colorspace Gray out.jpg
Photo with conversion after increased contrast

Increase contrast before conversion:

$ convert photo.jpg -contrast-stretch 4x4 colorspace Gray out.jpg
Using a non-linear colorspace and linear stretch

linear-stretch and non-linear colorspace:

$ convert photo.jpg -linear-stretch 1x1 -colorspace Rec709Luma out.jpg

Doors

Original photo

The original photo.

Photo converted to grayscale with default settings.

Simple conversion to grayscale colorspace loss of details is visible at the top areas of the doors.

$ convert photo.jpg -colorspace Gray out.jpg
Photo with conversion after increased contrast

Increase contrast before conversion reveals more details on the doors. The wall looks "whiter".

$ convert photo.jpg -contrast-stretch 4x4 colorspace Gray out.jpg
Using a non-linear colorspace and linear stretch

Linear-stretch and non-linear colorspace shows almost no difference to the simpler contrast stretch.

$ convert photo.jpg -linear-stretch 1x1 -colorspace Rec709Luma out.jpg

[1] http://www.imagemagick.org