16ctt1x
16ctt1x

Reputation: 331

How to convert a "true-color" image to a "black-and-white" image, with PHP?

First, I have an original image, which is a true-color image. It is saved in JPEG format:

*#1*. The original image.

This original picture is saved in: 24 bit image.


Then, I could convert it into a gray-scale image, after running this simple script:

<?php 

$source_file = "1.JPG";

$im = ImageCreateFromJpeg($source_file); 

$imgw = imagesx($im);
$imgh = imagesy($im);

for ($i=0; $i<$imgw; $i++)
{
        for ($j=0; $j<$imgh; $j++)
        {

                // Get the RGB value for current pixel

                $rgb = ImageColorAt($im, $i, $j); 

                // Extract each value for: R, G, B

                $rr = ($rgb >> 16) & 0xFF;
                $gg = ($rgb >> 8) & 0xFF;
                $bb = $rgb & 0xFF;

                // Get the value from the RGB value

                $g = round(($rr + $gg + $bb) / 3);

                // Gray-scale values have: R=G=B=G

                $val = imagecolorallocate($im, $g, $g, $g);

                // Set the gray value

                imagesetpixel ($im, $i, $j, $val);
        }
}

header('Content-type: image/jpeg');
imagejpeg($im);

?>

And, below is result:

*#2*. The gray-scale image.

This gray-scale picture is saved in: 8 bit image.


Now, I want to convert it into a real black-and-white image:

*#3*. The black-and-white image.

This black-and-white picture is saved in: 1 bit image.


Can you show me: How to convert a true-color image to a black-and-white image, with PHP?

Upvotes: 3

Views: 5021

Answers (1)

sms247
sms247

Reputation: 4504

friend round the grayscale color to either black or white in your code. (change or vary if($g> 0x7F) on your requirement)

 $g = (r + g + b) / 3
    if($g> 0x7F) //you can also use 0x3F 0x4F 0x5F 0x6F its on you 
   $g=0xFF;
   else
   $g=0x00;

your full code should be like :

<?php 

$source_file = "1.JPG";

$im = ImageCreateFromJpeg($source_file); 

$imgw = imagesx($im);
$imgh = imagesy($im);

for ($i=0; $i<$imgw; $i++)
{
        for ($j=0; $j<$imgh; $j++)
        {

                // Get the RGB value for current pixel

                $rgb = ImageColorAt($im, $i, $j); 

                // Extract each value for: R, G, B

                $rr = ($rgb >> 16) & 0xFF;
                $gg = ($rgb >> 8) & 0xFF;
                $bb = $rgb & 0xFF;

                // Get the value from the RGB value

                $g = round(($rr + $gg + $bb) / 3);

                // Gray-scale values have: R=G=B=G

                 //$g = (r + g + b) / 3
    if($g> 0x7F) //you can also use 0x3F 0x4F 0x5F 0x6F its on you 
   $g=0xFF;
   else
   $g=0x00;



                $val = imagecolorallocate($im, $g, $g, $g);

                // Set the gray value

                imagesetpixel ($im, $i, $j, $val);
        }
}

header('Content-type: image/jpeg');
imagejpeg($im);

?>

your can also use following alternative code logic

<?php 

header("content-type: image/jpeg");
$img = imagecreatefromjpeg('1.jpg');
imagefilter($img, IMG_FILTER_GRAYSCALE); //first, convert to grayscale
imagefilter($img, IMG_FILTER_CONTRAST, -255); //then, apply a full contrast
imagejpeg($img);

?>

Upvotes: 6

Related Questions