dtxw20878 2013-09-06 16:55
浏览 156
已采纳

使用ImageMagick更改特定位置的像素颜色值

I have a script like this:

// Loop over image
$size = $im->getImageGeometry(); 
$w = $size['width']; 
$h = $size['height']; 

for ($i=0; $i<$w; $i++) {
    for ($j=0; $j<$h; $j++) {
        $pixel = $im->getImagePixelColor($j,$i);
        $color = $pixel->getColor();
        $pixel->setColor("rgb(0,0,255)");     
    }
}

Preferably I'd like the setColor command to change the color of the pixel at the specified (x,y) location, so that when I call something like:

echo '<img src="data:image/jpg;base64,'.base64_encode($im->getImageBlob()).'" alt="" />';

An updated image will show. However, this does not work since I guess the $pixel was returned by value instead of by reference. It would actually be preferable if there was a getImagePixelColor method but I can't seem to find anything on it.

Does anyone know of a way to do this with imagemagick, or maybe recommend a library in php that can do this easily?

  • 写回答

1条回答 默认 最新

  • doulan6150 2013-09-06 21:14
    关注

    See this question, "Imagick setColor not working with php". You'll need to initialize a pixel iterator to access a specific pixel, and sync any color changes back to the image.

    $image = new Imagick("someimage.png");
    $pixel_iterator = $image->getPixelIterator();
    foreach($pixel_iterator as $y => $pixels)
    {
      foreach($pixels as $x => $pixel)
      {
       $pixel->setColor("rgb(0,0,255)");
      }
     $pixel_iterator->syncIterator();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug