Pixel iterative capabilities of Imagick and Gmagick comparisons

Source: Internet
Author: User
Tags foreach array getcolor require

Usually, image processing work is such as thumbnails, watermarks and other simple work, but sometimes more complex, such as pixel iterations, this article through an example of Imagick and Gmagick pixel iterative function:

Pixel Data Generation Code

<?php

$data = Array ();

for ($row = 0; $row < $row + +) {

for ($column = 0; $column < $column + +) {

$data [$row] [$column] = ' # '. Str_repeat ($column% 10, 6);

}

}

?>

Imagick iterative Write pixels

<?php

Require ' data.php ';

$image = new Imagick ();

$image->newimage (+, ' white ', ' png ');

$iterator = $image->getpixeliterator ();

foreach ($iterator as $row => $pixels) {

foreach ($pixels as $column => $pixel) {

$pixel->setcolor ($data [$row] [$column]);

}

$iterator->synciterator ();

}

$image->writeimage (' pixel.png ');

?>

Note: You need to invoke the Synciterator action (read pixels not) When you use Pixeliterator to write pixels in Imagick.

Gmagick iterative Write pixels

<?php

Require ' data.php ';

$image = new Gmagick ();

$image->newimage (+, ' white ', ' png ');

$pixel = new Gmagickpixel ();

$draw = new Gmagickdraw ();

for ($row = 0; $row < $row + +) {

for ($column = 0; $column < $column + +) {

$pixel->setcolor ($data [$row] [$column]);

$draw->setfillcolor ($pixel);

$draw->point ($column, $row);

$image->drawimage ($draw);

$pixel->clear ();

$draw->clear ();

}

}

$image->writeimage (' pixel.png ');

?>

The resulting picture looks like this:

  

Pixel.png

The preceding shows how to write pixels in an iteration, and then see how to read pixels (using the generated pixel.png):

Imagick iterative Read pixels

<?php

$data = Array ();

$image = new Imagick (' pixel.png ');

$iterator = $image->getpixeliterator ();

foreach ($iterator as $row => $pixels) {

foreach ($pixels as $column => $pixel) {

$data [$row] [$column] = $pixel->getcolor ();

}

}

Print_r ($data);

?>

Gmagick iterative Read pixels

<?php

$data = Array ();

$image = new Gmagick (' pixel.png ');

$width = $image->getimagewidth ();

$height = $image->getimageheight ();

for ($row = 0; $row < $width; $row + +) {

for ($column = 0; $column < $height; $column + +) {

$cropped = Clone $image;

$histogram = $cropped->cropimage (1, 1, $column, $row)

->quantizeimage (1, Gmagick::colorspace_rgb, 0, False, false)

->getimagehistogram ();

$data [$row] [$column] = $histogram [0]->getcolor ();

}

}

Print_r ($data);

?>

Note: In Imagick and Gmagick, the color of the pixel is RGB, but the data format is different.

Overall, the implementation of Imagick is simpler, and the implementation of Gmagick is slightly more complex because there is no pixeliterator concept. However, the concept of Gmagick without pixeliterator is not a bug, but to be consistent with the GraphicsMagick Wand C API interface.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.