2013-10-04 2 views
2

컬러와 흑백으로 분리해야하는 이미지 디렉토리가 있습니다. 이것은 이미지 magik getImageType을 사용하여 쉽게 감지 할 수있었습니다. 그러나 일부 이미지는 흑백으로 간주되지만 약간의 색조가 있습니다.imagemagick을 사용하여 이미지가 흑백 인 경우 그림은

로 트루 컬러 흑백으로 간주하지만, 돌아 오면 그 여전히 기술적으로 흑백

이미지를 일부 값을보고 결정하는 알려진 방법이 : 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 "; 

} 

Heres는 스크립트의 출력은, 화상의 파일명은

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 
,536,913,632 멀리 수득 10
+0

복제 된 회색조 버전의 이미지를 원본과 비교합니까? http://www.imagemagick.org/Usage/compare/#type_greyscale 어떻게 PHP에서 보일지 확실하지 않음 – mzmm56

+0

http://stackoverflow.com/questions/23922654/detect-if-image-is-grayscale-or- color-using-imagick –

답변

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이 true로 유지됩니다.

나는 이것을 테스트하지 않았 음을 명심하십시오. 이렇게하면 약간의 작업이 필요할 수 있습니다. 행운을 빕니다.

반복하는 차별화
+0

옐로우 때문에 게시 한 그림의 색상 수가 높습니다. – azz0r

+0

@ azz0r 불행히도, 일하러 가야하므로 스크립트를 수정할 수 없습니다. 그러나 노란색은 파란색과 초록색이므로 비교하기 전에 'g'와 'b'값을 뒤집어 볼 수도 있습니다. 'abs (255- $ color [ 'g'])'와 같은 것은''색상을 뒤집을 때 작동해야합니다. 여전히 문제가 해결되지 않으면 b 및 g 색상 값을 비교하지 말고 빨간색 만 확인하십시오. –

+0

@ azz0r 실수는 노란색이며 g와 r이 아니라 g와 b입니다. 파란색 값만 확인하는 것 외에 위에서 언급 한 작업을 시도합니다. –

0

회색 여부를 확인하기위한 모든 픽셀, 당신은 단순히 다음 단계로 흑백 이미지를 찾을 수 있습니다 것은

  • HSL
  • 에 이미지의 색 공간 변환하는 것은 greenchannel
  • 의 평균 받기
  • 제로 다음, 그에게 흑백 이미지 인 경우, 그렇지 않으면하지

희망이 당신에게

도움이 될

감사합니다.

+0

이 솔루션은 이론적으로는 효과가 있지만 실제로는 피사체의 이미지에 "0.176712"와 같은 것을 출력합니다. 이미지를 "그레이 스케일"로 간주하는 데 필요한 값을 임계 값으로 설정하는 가장 좋은 방법이 무엇인지 모릅니다. – webdevbyjoss

관련 문제