Contrast & Brightness


PHP ImageMagick – Contrast & Brightness



”;


Different types of moods can be conveyed in images with the help of contrast. The term ‘contrast’ refers to the amount of color or grayscale differentiation. Images with higher contrast levels generally display a greater degree of color or grayscale variation than those of lower contrast. In this chapter, you will be learning about changing and adjusting the contrast and brightness.

Changing the Contrast

In this section, you will be learning about the process of changing the contrast. This can be done using a method called ‘contrastImage()’ which is provided by Imagemagick. It helps to enhance the differences between lighter and darker elements of the image.

Syntax


public Imagick::contrastImage(bool  $sharpen): bool

This method contains a single parameter which is ‘sharpen’. It is a boolean value that specifies the sharpen value. This method takes an image as an input and gives out the image after changing its contrast as output.

Example

In the below example, new imagick object is created and the input image is taken. Then, the contrastImage() function is applied on that image. Finally, the output image is obtained in the format ‘contrastImage.png’.


<?php
   $image=new Imagick($_SERVER[''DOCUMENT_ROOT'']."/test/image.jpeg");
   $image->contrastImage(true);
   $image->writeImage($_SERVER[''DOCUMENT_ROOT'']."/test/contrastImage.png");
?>

Assume that the following is the input image (image.jpeg) in the program −


Changing the Contrast

Output


Changing the Contrast

Changing the Brightness

ImageMagick provided a method called ‘brightnessContrastImage()’ which changes the brightness and contrast of an image. It converts the brightness and contrast parameters into slope and intercept and calls a polynomial function to apply to the image.

Syntax


Public Imagick::brightnessContrastImage(float $brightness, float $contrast, int $channel=Imagick::CHANNEL_DEFAULT):bool

This method contains 3 parameters which are brightness, contrast, and channel. ‘Brightness’ is used to store the value of brightness, ‘contrast’ is used to store the value of the contrast of the image, and ‘channel’ is used to store the value of the channel. The output obtained is an image with added brightness and contrast.

Example

In the below example, a new imagick object is created and the input image is taken. Then, the ‘brightnessContrastImage()’ function with parameters (brightness=15, contrast=20) is applied on that image. Finally, the output image is obtained in the format ‘brightnessContrastImage.png’.


<?php
   $image=new Imagick($_SERVER[''DOCUMENT_ROOT'']."/test/image5.jpeg");
   $image->brightnessContrastImage(15,50);
   $image->writeImage($_SERVER[''DOCUMENT_ROOT'']."/test/brightnessContrastImage.png");
?>

Assume that the following is the input image (image5.jpeg) in the program −


Changing the Brightness

Output


Changing the Brightness

Enhance the Contrast

Enhancement is the process of improving quality of an image. To enhance the contrast, Imagemagick has provided a method ‘contrastStretchImage()’ which enhances the contrast of the color image by adjusting the pixels” color to span the entire range of colors available.

Syntax


public Imagick::contrastStretchImage(float $black_point, float $white_point, int $channel = Imagick::CHANNEL_DEFAULT): bool

This method has three parameters which are black_point, white_point, and channel. ‘Black_point’ specifies the black point, ‘white_point’ specifies the white point and’ channel’ provides any channel constant that is valid for your channel mode.

Example

In the below example, a new Imagick object is created and the input image is taken. Then, the ‘contrastStretchImage()’ function with parameters(black_point=1000, white_point=5000) is applied on that image. Finally, the output image is obtained in the format ‘contrastStretchImage.png’.

This method has three parameters which are black_point, white_point, and channel. ‘Black_point’ specifies the black point, ‘white_point’ specifies the white point and’ channel’ provides any channel constant that is valid for your channel mode.


<?php
   $image=new Imagick($_SERVER[''DOCUMENT_ROOT'']."/test/image.jpeg");
   $image->contrastStretchImage(1000, 5000);
   $image->writeImage($_SERVER[''DOCUMENT_ROOT'']."/test/contrastStretchImage.png");
?>

Assume that the following is the input image (image.jpeg) in the program −


Enhance Contrast

Output


Enhance Contrast

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *