使用imagemagick计算出图像是黑白的

时间:2013-10-04 15:48:18

标签: php image image-processing imagemagick

我有一个图像目录,我需要分成颜色与黑白分开。使用image magik getImageType很容易检测到这一点。但是有些图像会被认为是黑色和白色,但有一些黄色。

是否有一种已知的方法来查看某些值并确定其技术上仍为黑白

图像计为黑白但显示为真彩色:enter image description here

脚本

<?php

$dir = getcwd();
$dirToImages = getcwd().'/imagesToScan/';
$files = scandir($dirToImages);
$badFiles = array('.DS_Store', '.', '..', 'index.php', 'bAndW', 'color');
@mkdir('colour');
@mkdir('bAndW');
echo "\n\n starting script, counting images: \n\n";
echo "image count: ".count($files)."  \n\n ";

foreach($files as $file) {
    /* if the file is shit, its not an image, skip it */
    if (in_array($file, $badFiles)) {
        continue;
    }
    /* directories or bad files don't have an extension, fool proof */
    $exploded = explode('.', $file);
    if (!isset($exploded[1])) {
        continue;
    }
    echo $file;
    /* okay do the work */
    $imagick_type = new Imagick();
    $file_to_grab = $dirToImages.'/'.$file;
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
    $image_type = $imagick_type->getImageType();

    switch($image_type)
    {
        case imagick::IMGTYPE_UNDEFINED:
            $image_type_title = "Undefined";
            break;

        case imagick::IMGTYPE_BILEVEL:
            $image_type_title = "Bilevel";
            break;

        case imagick::IMGTYPE_GRAYSCALE:
            $image_type_title = "Grayscale";
            break;

        case imagick::IMGTYPE_GRAYSCALEMATTE:
            $image_type_title = "Grayscale Matte";
            break;

        case imagick::IMGTYPE_PALETTE:
            $image_type_title = "Palette";
            break;

        case imagick::IMGTYPE_PALETTEMATTE:
            $image_type_title = "Palette Matte";
            break;

        case imagick::IMGTYPE_TRUECOLOR:
            $image_type_title = "Truecolor";
            break;

        case imagick::IMGTYPE_TRUECOLORMATTE:
            $image_type_title = "Truecolor Matte";
            break;

        case imagick::IMGTYPE_COLORSEPARATION:
            $image_type_title = "Color Separation";
            break;

        case imagick::IMGTYPE_COLORSEPARATIONMATTE:
            $image_type_title = "Color Separation Matte";
            break;

        case imagick::IMGTYPE_OPTIMIZE:
            $image_type_title = "Optimize";
            break;
    }

    if ($image_type == 2) {
//        copy($dirToImages.'/'.$file, $dir.'/bAndW/'.$file);
    } else {
//        copy($dirToImages.'/'.$file, $dir.'/colour/'.$file);
    }
    echo $file."
    \n Filename: ".$image_type.":
    \n Type: ".$image_type_title ."
    \n reddPrimary: ".print_r($imagick_type->getImageRedPrimary(),true)."
    \n getImageBackgroundColor: ".print_r($imagick_type->getImageBackgroundColor(),true)."
    \n ColourSpace: ".print_r($imagick_type->getColorspace(),true)."
    \n\n ";

}

继承脚本的输出,图像的文件名将其丢弃

starting script, counting images: 

image count: 8  

 523340_10151281257702912_1277096031_n-fixed.jpg523340_10151281257702912_1277096031_n-fixed.jpg

 Filename: 6:

 Type: Truecolor

 reddPrimary: Array
(
    [x] => 0.63999998569489
    [y] => 0.33000001311302
)


 getImageBackgroundColor: ImagickPixel Object
(
)


 ColourSpace: 0


 black-and-white.jpgblack-and-white.jpg

 Filename: 6:

 Type: Truecolor

 reddPrimary: Array
(
    [x] => 0.63999998569489
    [y] => 0.33000001311302
)


 getImageBackgroundColor: ImagickPixel Object
(
)


 ColourSpace: 0


 colour-gif.gifcolour-gif.gif

 Filename: 5:

 Type: Palette Matte

 reddPrimary: Array
(
    [x] => 0.63999998569489
    [y] => 0.33000001311302
)


 getImageBackgroundColor: ImagickPixel Object
(
)


 ColourSpace: 0


 colour-image.pngcolour-image.png

 Filename: 6:

 Type: Truecolor

 reddPrimary: Array
(
    [x] => 0.63999998569489
    [y] => 0.33000001311302
)


 getImageBackgroundColor: ImagickPixel Object
(
)


 ColourSpace: 0


 yellow-should-be-black-and-white.jpgyellow-should-be-black-and-white.jpg

 Filename: 6:

 Type: Truecolor

 reddPrimary: Array
(
    [x] => 0.63999998569489
    [y] => 0.33000001311302
)


 getImageBackgroundColor: ImagickPixel Object
(
)


 ColourSpace: 0

2 个答案:

答案 0 :(得分:0)

可能有更好的方法,但通过Imagick的文档查看它看起来像there's a way to get the color of a single pixel。也许您可以使用以下内容:

$im=new Imagick( 'image.png' );
$iterator=$im->getPixelIterator();

// Tracks whether the image is black and white or not.
$b_and_w = true;

/* The tolerance value. This is how close the color of the pixel
 *must be to gray for it to count the image as black and white.
 */
$t = 5;

/* Check the value of every $x pixels. This is to allow the script
 * to run faster, but will lower the accuracy.
 */
$x = 2;

// Count of current pixel
$c = 0;

foreach( $iterator as $row => $pixels ) {
    foreach ( $pixels as $column => $pixel ){

        // If this pixel is not every $x pixel, skip it
        if(++$c % $x !== 0) continue;

        // Find unnormal color of pixel
        $color = $pixel->getColor();

        // Find average color of pixel
        $average = array_sum($color)/3;

        /* Compare the rgb values of this pixel to the average.
         * If any of them aren't close, that means it's not a shade of gray.
         */
        if(!in_array($color['r'],range(max(0,$average-$t),min(255,$average+$t)))
        || !in_array($color['g'],range(max(0,$average-$t),min(255,$average+$t)))
        || !in_array($color['b'],range(max(0,$average-$t),min(255,$average+$t)))){

            // Set our boolean to false
            $b_and_w = false;

            // Now we abandon the loops
            break 2;
        }
    }
}

if($b_and_w) echo "This image is black and white";
else         echo "This image has at least one pixel that's colored";

它会将每个$x像素的颜色与每个像素的平均颜色(黑色和白色)进行比较。如果有时它们不够接近,则循环将退出,$b_and_w将设置为false。如果未找到彩色像素,$b_and_w将保持为真。

请记住,我没有测试过这个,所以这可能需要你做一些工作。祝你好运。

答案 1 :(得分:0)

除了迭代每个像素以检查灰色与否之外,你可以通过这些步骤找到黑白图像

  • 将图像的色彩空间转换为HSL
  • 获取greenchannel的意思
  • 如果它为零,那么它是黑白图像,否则不是

希望这可以帮到你

由于