English 中文(简体)
将上载图像转换为平流层
原标题:convert uploaded image to grey scale in PHP

我有一幅画面,把照片翻一番,所有照片都用得很美,但我希望能够把颜色从照片中抹掉,留下黑白的图像(各种灰尘基本上)。 我肯定了如何实现这一目标?

增 编

最佳回答

2. 遵循这些思路:

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

通知说,我无耻地从上渗入了这条,我发现该条使用了“ogle”搜索术语: php形象改为“graycal <>。

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

imagefilter($im, IMG_FILTER_GRAYSCALE); 
问题回答

The easiest solution is to use imagefilter($im, IMG_FILTER_GRAYSCALE); But every single method mentioned here is not working for 100%. All of them is counting on color palette of the image, but the shades of gray might be missing and another color from the palette is used.

我的解决办法是用图像col器替换彩色盒。

$colorsCount = imagecolorstotal($img->getImageResource());
for($i=0;$i<$colorsCount;$i++){
    $colors = imagecolorsforindex( $img->getImageResource() , $i );
    $g = round(($colors[ red ] + $colors[ green ] + $colors[ blue ]) / 3);
    imagecolorset($img->getImageResource(), $i, $g, $g, $g);
}

It s better to use command tool to convert such type of image.

While animated gif is also support.

例如:

$file =  image.jpg ;
$file =  image.gif ;
$file =  image.png ;
$image_type = getimagesize($file);

switch (strtolower($image_type[ mime ])) {
    case  image/png :
       exec("convert $file -colorspace Gray dummy.png");
        break;
    case  image/jpeg :
       exec("convert $file -colorspace Gray dummy.jpeg");
        break;
    case  image/gif :
       exec("convert $file -colorspace Gray dummy.gif");
        break;
    default:
        die;
}




相关问题
Resources for Image Recognition

I am looking for a recommendation for an introduction to image processing algorithms (face and shape recognition, etc.) and wondered if anyone had an good recommendations, either for books, ...

Good reference book for digital image processing? [closed]

I am learning digital image processing on my own and would like recomendations on good reference books. If you know of books to definately stay away from that would be useful as well. Thanks

Python Tesseract can t recognize this font

I have this image: I want to read it to a string using python, which I didn t think would be that hard. I came upon tesseract, and then a wrapper for python scripts using tesseract. So I started ...

What s the quickest way to parallelize code?

I have an image processing routine that I believe could be made very parallel very quickly. Each pixel needs to have roughly 2k operations done on it in a way that doesn t depend on the operations ...

Computing object statistics from the second central moments

I m currently working on writing a version of the MATLAB RegionProps function for GNU Octave. I have most of it implemented, but I m still struggling with the implementation of a few parts. I had ...

Viola-Jones face detection claims 180k features

I ve been implementing an adaptation of Viola-Jones face detection algorithm. The technique relies upon placing a subframe of 24x24 pixels within an image, and subsequently placing rectangular ...