How to Convert an Image to Black and White in PHP

How do you convert an image to black and white in PHP

Simply round the grayscale color to either black or white.

float gray = (r + g + b) / 3
if(gray > 0x7F) return 0xFF;
return 0x00;

How to convert a grayscale image to pure black and white in php?

Here's a way to do that with gd:

#!/usr/bin/php -f
<?php

// Open image and get dimensions
$im = imagecreatefromjpeg("cellule.jpg");
$w = imagesx($im);
$h = imagesy($im);

// Convert to greyscale
imagefilter($im,IMG_FILTER_GRAYSCALE);
imagepng($im, "grey.png"); // DEBUG only

// Allocate a new palette image to hold the b&w output
$out = imagecreate($w,$h);
// Allocate b&w palette entries
$black = imagecolorallocate($out,0,0,0);
$white = imagecolorallocate($out,255,255,255);

// Iterate over all pixels, thresholding to pure b&w
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
// Get current color
$index = imagecolorat($im, $x, $y);
$grey = imagecolorsforindex($im, $index)['red'];
// Set pixel white if below threshold - don't bother settting black as image is initially black anyway
if ($grey <= 190) {
imagesetpixel($out,$x,$y,$white);
}
}
}
imagepng($out, "result.png");
?>

Sample Image

Keywords: PHP, image processing, greyscale, grayscale, threshold, palette, PNG, gd, GD, imagecreate, imagepng, imagecreatefromjpeg, imagecolorallocate.

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

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);

?>

How to automaticly generate a black and white version of a picture

And that wordpress technique look cool : http://bavotasan.com/2011/create-black-white-thumbnail-wordpress/

convert uploaded image to grey scale in PHP

Try something along these lines:

<?php 
$source_file = "test_image.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);

// grayscale 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);
?>

Notice that I have shamelessly ripped this snippet from this article, which I found using a google search with the terms: php convert image to grayscale

[ edit ]
And from the comments, if you use PHP5, you could also use:

imagefilter($im, IMG_FILTER_GRAYSCALE); 

How to convert a true-color image to an one-color image, with PHP?

Just two lines of code will handle the grey scale conversion and reducing the image to only two colours (because a one-colour image is a blank canvas), with dithering:

$img = imagecreatefromjpeg('./38519049.jpg');

imagefilter($img, IMG_FILTER_GRAYSCALE); // convert to grey scale.
imagetruecolortopalette($img, true, 2); // 'true' for dithering, '2' for number of colours.

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

Result:

Sample Image

How to Black and White Image in PHP?

Try using the imagefilter() function, first with IMG_FILTER_GRAYSCALE to make it grayscale, then with IMG_FILTER_CONTRAST to make it full black and white.

Change web images to black and white

example from http://us2.php.net/manual/en/function.imagefilter.php

<?php
$im = imagecreatefrompng('yourfile.png');

if($im && imagefilter($im, IMG_FILTER_GRAYSCALE ))
{
echo 'Image brightness changed.';

imagepng($im, 'sean.png');
imagedestroy($im);
}
else
{
echo 'Image brightness change failed.';
}
?>

Converting images to greyscale using intervention

If you want to download then first you have to save the created file and need to give the path to download. Here is working example

$img_name=$sheet[0]->sheet_f_id . '-s.jpg';
$destination_path=public_path() . "/large/s/";

$file = $destination_path.$img_name;
$image = Image::make($file);

$image->greyscale()->save($destination_path.'gray-'.$img_name);
return Response::download($destination_path.'gray_'.$img_name);

And if you don't want to keep the file you can delete, replace the last line with below line.

return Response::download($destination_path.'gray_'.$img_name)->deleteFileAfterSend(true);

Hope it will work for you.



Related Topics



Leave a reply



Submit