2

我有一个图像目录,我需要将其分成彩色与黑白。使用 image magik getImageType 很容易检测到这一点。然而,有些图像会被认为是黑白的,但有一些黄色阴影。

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

算作黑白但返回为真彩色的图像:在此处输入图像描述

脚本

<?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
4

2 回答 2

0

除了迭代每个像素以检查灰度之外,您可以通过这些步骤简单地找出黑白图像

  • 将图像的色彩空间转换为 HSL
  • 获取绿色通道的平均值
  • 如果为零,则为黑白图像,否则不是

希望这可以帮助你

谢谢

于 2013-11-08T05:07:20.947 回答
0

可能有更好的方法,但是通过查看 Imagick 的文档,似乎有一种方法可以获取单个像素的颜色。也许您可以使用以下内容:

$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将保持为真。

请记住,我没有对此进行测试,因此这可能需要您做一些工作。祝你好运。

于 2013-10-04T16:23:59.923 回答