2

我已经使用 getImagePixelColor 在特定点获取图像的图像像素。

$pixel = $image -> getImagePixelColor($x,$y);

现在我已经使用某种方法修改了该像素的颜色,现在我想设置该像素的新颜色。

我能怎么做 ?

有一个 setColor 函数。但我从 Imagick 类中得到了像素。但是 setColor 函数在 ImagickPixel 类中。那么我该怎么做呢?

4

2 回答 2

7

ImagickPixel::setColor()是正确的功能,但还需要同步像素迭代器,以便将您的操作写回图像。

这是一个简短但(几乎)完整的示例,它读取图像文件,操作每个像素,并将其转储到浏览器:

$img = new Imagick('your_image.png');

$iterator = $img->getPixelIterator();
foreach ($iterator as $row=>$pixels) {
  foreach ( $pixels as $col=>$pixel ){
    $color = $pixel->getColor();      // values are 0-255
    $alpha = $pixel->getColor(true);  // values are 0.0-1.0

    $r = $color['r'];
    $g = $color['g'];
    $b = $color['b'];
    $a = $alpha['a'];

    // manipulate r, g, b and a as necessary
    //
    // you could also read arbitrary pixels from 
    // another image with similar dimensions like so:
    // $otherimg_pixel = $other_img->getImagePixelColor($col,$row);
    // $other_color = $otherimg_pixel->getColor();
    //
    // then write them back into the iterator
    // and sync it

    $pixel->setColor("rgba($r,$g,$b,$a)");
  }
  $iterator->syncIterator();
}

header('Content-type: '.$img->getFormat());
echo $img->getimageblob();
于 2013-07-09T06:30:49.907 回答
0

->getImagePixelColor()无论如何都会返回一个 ImagickPixel 对象,所以$pixel->setColor(...);你只需要:

参考: http: //php.net/manual/en/imagick.getimagepixelcolor.php

于 2013-06-28T14:35:45.260 回答