如何使用 PHP 将 "true-color" 图像转换为 "black-and-white" 图像?
How to convert a "true-color" image to a "black-and-white" image, with PHP?
首先,我有一张原始图像,它是一张真彩色图像。它以JPEG格式保存:
这张原图保存在:24位图像。
然后,在 运行 这个简单的脚本之后,我可以将它转换成 灰度 图像:
<?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);
?>
下面是结果:
这张灰度图保存在:8位图像中。
现在,我想把它转换成真实 黑白图像:
这张黑白图片保存在:1 位 图像中。
你能告诉我:如何使用 PHP 将真彩色图像转换为黑白图像?
朋友在您的代码中将灰度颜色四舍五入为黑色或白色。
(根据您的要求更改或更改 if($g> 0x7F))
$g = (r + g + b) / 3
if($g> 0x7F) //you can also use 0x3F 0x4F 0x5F 0x6F its on you
$g=0xFF;
else
$g=0x00;
你的完整代码应该是这样的:
<?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);
?>
您还可以使用以下替代代码逻辑
<?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);
?>
首先,我有一张原始图像,它是一张真彩色图像。它以JPEG格式保存:
这张原图保存在:24位图像。
然后,在 运行 这个简单的脚本之后,我可以将它转换成 灰度 图像:
<?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);
?>
下面是结果:
这张灰度图保存在:8位图像中。
现在,我想把它转换成真实 黑白图像:
这张黑白图片保存在:1 位 图像中。
你能告诉我:如何使用 PHP 将真彩色图像转换为黑白图像?
朋友在您的代码中将灰度颜色四舍五入为黑色或白色。 (根据您的要求更改或更改 if($g> 0x7F))
$g = (r + g + b) / 3
if($g> 0x7F) //you can also use 0x3F 0x4F 0x5F 0x6F its on you
$g=0xFF;
else
$g=0x00;
你的完整代码应该是这样的:
<?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);
?>
您还可以使用以下替代代码逻辑
<?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);
?>