January 22, 2021
Hot Topics:

GraphicsMagick: Image Processing Add-In as Easy as Abra Cadabra!

  • By Victor Volkman
  • Send Email »
  • More Articles »

A Simple Demo Program

Last, look at a small demo program that will read in a .GIF file and crop it to 100x100 pixels at (100,100) from the origin. Then, it will write out a new GIF file with the cropped image. In the documentation, using the C++ object-oriented interface is called Magick++ for some reason.

#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
   // Construct the image object. Separating image construction
   // from the read operation ensures that a failure to read the
   // image file doesn't render the image object useless.

Image image; try { // Read a file into image object image.read( "girl.gif" ); // Crop the image to specified size (width, height, // xOffset, yOffset) image.crop( Geometry(100,100, 100, 100) ); // Write the image to a file image.write( "x.gif" ); } catch( Exception &error_ ) { cout << "Caught exception: " << error_.what() << endl; return 1; } return 0; }

The basic idea is that an Image type object has lots of operators to do image processing with. This example only used the read(), crop(), and write() methods but of course there are dozens more. These methods mimic what you can do in Adobe Photoshop and most popular photo/image manipulation programs including shear(), solarize(), swirl(), transform(), unsharpmask(), rotate(), scale(), quantize(), magnify(), zoom(), and everything else you could possibly need. You can even burn text into the images with annotate().

Here's another example that creates a 100x100 image on the fly, plunks a red pixel in the center, and dumps it out to a PNG file.

#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
   Image image( "100x100", "white" );
   image.pixelColor( 49, 49, "red" );
   image.write( "red_pixel.png" );
   return 0;

Magick++ provides integrated support for the Standard Template Library (STL) so that the powerful containers available (such as deque, vector, list, and map) can be used to write programs similar to those possible with PERL & PerlMagick. STL-compatible template versions of GraphicsMagick's list-style operations are provided so that operations may be performed on multiple images stored in STL containers.


Magick++ includes everything you can possibly want to automate image processing whether you are writing a program that produces web graphics on-the-fly or the next great application to clean and process digital photos. Your imagination is the only limit to what you can do with GraphicsMagick.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for the Washtenaw Community College CIS department. Volkman is the editor of numerous books including, C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an email to sysop@HAL9K.com.

Page 2 of 2

This article was originally published on August 22, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date