使用Php ImageMagick将8位图像转换为16位

时间:2016-05-20 16:38:43

标签: php imagemagick imagick

我要做的是使用php ImageMagick将8位图像转换为16位图像。使用Imagick,它就像convert /location/image.jpg -colors 8 -depth 16 txt:-一样简单。在PHP文档中,它显示您可以使用$imagick->setImageDepth(16);来执行此操作,但问题是它不会为您提供16位十六进制/ rgb数字。

尝试从图像中提取颜色时使用16位是否有优势?如果不是那么我想知道php功能不起作用吗?

以下是我从$mapColor->getColor()获得的信息:

Array
(
    [r] => 51
    [g] => 51
    [b] => 51
    [a] => 1
)
Array

以下是获取颜色的代码:

// Create new instance
$imagick = new Imagick($image);

// Set number of colors
$numberColors = 6;

// Set colorspace
$colorSpace = Imagick::COLORSPACE_SRGB;

// Set tree depth
$treeDepth = 0;

// Set dither
$dither = false;

// Convert to 16 bit
$imagick->setImageDepth(16);

// Quantize image to number of colors
$imagick->quantizeImage($numberColors, $colorSpace, $treeDepth, $dither, false);

// Get Image Dimensions
$sizes = $imagick->getImageGeometry();

// Get image's histogram
$pixels = $imagick->getImageHistogram();

// Temp cache dimensions
$totalSize = $sizes['width'] * $sizes['height'];


// Loop through each pixel
foreach($pixels as $k => $v) {

    // Get total number of color instances
    $count = $v->getColorCount();

    // Get colormap at pixel
    $mapColor = $imagick->getImageColormapColor($k);

    // Get colore
    $rgbColor = $mapColor->getColor();

    // Convert to RGB hex values
    $red = str_pad(dechex($rgbColor['r']), 2, 0, STR_PAD_LEFT);
    $green = str_pad(dechex($rgbColor['g']), 2, 0, STR_PAD_LEFT);
    $blue = str_pad(dechex($rgbColor['b']), 2, 0, STR_PAD_LEFT);

    // Set hexadecimal
    $hex = $red.$green.$blue;

    // Calculate color percentage
    $percentage = (100 * $count) / $totalSize;

    // Push colors into array
    $colors[] = array(
        'red'           => $rgbColor['r'],
        'green'         => $rgbColor['g'],
        'blue'          => $rgbColor['b'],
        'hex'           => $hex,
        'count'         => $count,
        'percentage'    => $percentage,
    );
}

print_r($colors);

0 个答案:

没有答案