dousong9729 2012-08-19 11:32
浏览 332

如何在Imagick中设置图像像素颜色?

i am using Imagick for image processing. I am unable to find any native function to set a particular pixel color while there is one to get one. I mean,

$image->getimagepixelcolor($i, $j);

will return an object to get the color of pixel at ($i, $j) but there doesn't seems to be a method to set color. Something like this,

$image->setimagepixelcolor($i, $j, "#FFFFFF");
  • 写回答

1条回答 默认 最新

  • dqyknf4423 2013-09-26 14:10
    关注

    There would be no setImagePixelColor method on the Imagick class. I can't speak of why there isn't one, but one can achieve the same effects by invoking the ImagickPixelIterator, or ImagickDraw class.

    Iterator Example:

    The proper way would be to iterator over the pixels in an image, alter a pixels color, then sync the data back to the image.

    $image = new Imagick('source.png');
    
    // Grab iterator
    $iterator = $image->getPixelIterator();
    // Seek to row at Y
    $iterator->setIteratorRow($j);
    // Get pixel in row
    $row = $iterator->getCurrentIteratorRow();
    // Seek to pixel at X
    $pixel = $row[$i];
    // Set Color
    $pixel->setColor('#FFFFFF');
    // Sync data back to image
    $iterator->syncIterator();
    
    $image->writeImage('out.png');
    

    Draw Example:

    The other option would be to draw the new pixel on top of the existing image.

    $image = new Imagick('source.png');
    $draw  = new ImagickDraw();
    $color = new ImagickPixel('#FFFFFF');
    
    // Set fill color
    $draw->setFillColor($color);
    // Draw a point at X,Y
    $draw->point($i,$j);
    // Apply the drawing on top of the image
    $image->drawImage($draw);
    
    $image->writeImage('dest.png');
    

    Of course with Object's in PHP, you can simple extend the core Imagick object, and create your own setImagePixelColor method

    class MyImagick extends Imagick 
    {
       /**
        * Alter image by defining the color of a single pixel
        *
        * @param integer $x
        * @param integer $y
        * @param string $color
        */
       public function setImagePixelColor($x,$y, $color) 
       {
          //... Apply one of the methods here
          $draw  = new ImagickDraw();
          $draw->setFillColor(new ImagickPixel($color));
          $draw->point($x,$y);
          $this->drawImage($draw);
       }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度