我有一个图像目录,我需要将其分成彩色与黑白。使用 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