Compare the pixel iteration functions of Imagick and Gmagick

Source: Internet
Author: User
Generally, image processing is simple, such as thumbnails and watermarks, but sometimes it is more complicated, such as pixel iteration. in this article, we use an example to compare the pixel iteration functions of Imagick and Gmagick: pixel data generation code & lt ;? Php $ data = array ()... "> <LINKhref =" http://www.php100.com//st

Generally, image processing is simple, such as thumbnails and watermarks, but sometimes it is more complicated, such as pixel iteration. in this article, we use an example to compare the pixel iteration functions of Imagick and Gmagick:

Pixel data generation code

  

$ Data = array ();

For ($ row = 0; $ row <100; $ row ++ ){

For ($ column = 0; $ column <100; $ column ++ ){

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

}

}

?>

 

Imagick iterative write pixel

  

Require 'data. php ';

$ Image = new Imagick ();

$ Image-> newimage (100,100, '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: When PixelIterator is used in Imagick to write pixels, you need to call the syncIterator operation (read pixels are not required ).

Gmagick iterative write pixel

  

Require 'data. php ';

$ Image = new Gmagick ();

$ Image-> newimage (100,100, 'white', 'PNG ');

$ Pixel = new GmagickPixel ();

$ Draw = new GmagickDraw ();

For ($ row = 0; $ row <100; $ row ++ ){

For ($ column = 0; $ column <100; $ 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 generated image is as follows:

  

 

Pixel.png

Previously, I demonstrated how to write a script during the workshop and read it again (the generated pixel.png is used ):

Imagick iterative read pixel

  

$ 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 pixel

  

$ 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: The color obtained by reading pixels in Imagick and Gmagick is in RGB format, but the data format is different.

In general, the implementation of Imagick is simpler, while the implementation of Gmagick is a little complicated because it does not have the concept of PixelIterator. However, Gmagick does not have the PixelIterator concept and is not a Bug, but to be consistent with the GraphicsMagick Wand c api.

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.