
ImageMagick
Open Source C Library to Create & Convert Images
Create, edit, compose, read and convert Bitmap images using free C API. It allows to resize, flip, mirror, rotate, distort, shear and transform images inside C apps.
What is ImageMagick?
ImageMagick stands as a premier open source library, empowering software developers to seamlessly integrate advanced image processing capabilities directly into their own C applications. As a truly cross-platform solution, it operates smoothly across Linux, Windows, Mac OS X, iOS, Android OS, and numerous other environments. This versatile tool enables reading, displaying, creating, converting, and modifying raster images programmatically with C code. A cornerstone of its functionality is the expert convert images feature, which supports accurate transformation across an impressive roster of over 200 image file formats, including JPEG, PNG, GIF, HEIC, Exif, TIFF, PDF, SVG, WebP, and Postscript, ensuring broad compatibility for any project.
This feature-rich library excels in delivering a comprehensive suite of image processing tools for developers. Key operations such as image resizing, flipping, mirroring, rotating, distorting, and transforming images are handled with precision. Users can perform detailed image colors adjustment, apply special effects like blur or sharpening, utilize threshold filters, generate image gradients, and execute canny edge detection. Furthermore, it supports extensive drawing functions to draw text, lines, polygons, and ellipses, often controlled via mathematical expressions for granular control. Enhancing its utility, the library now also includes support for extracting text from images through integrated OCR capabilities, rounding out its profile as an indispensable resource for graphics and animated graphics manipulation.
Getting Started with ImageMagick
Clone the latest sources with using the following command
Install ImageMagick via GitHub.
git clone https://github.com/ImageMagick/ImageMagick.git Or use the following command to install it.
Install ImageMagick via RPM.
$ rpm -Uvh ImageMagick-libs-7.0.10-60.x86_64.rpm Convert Image to Other Formats
The open source C library ImageMagick enables software programmers to convert images to other supported file formats inside their own apps. Users need to provide the format parameter before converting to another format. The library internally converts the image to other formats earlier, before applying the transformations. The library supports conversion to several popular image formats such as BMP, CMYK, GIF, JPEG, PBM, PNG, RGB, SVG, TIFF, and XPM formats. One great aspect of the library is the ease of use while converting the image. It also provides the ability to reduce the size of the image and apply different effects before writing it in the desired format.
How to Convert GIF to JPEG Image via ImageMagick?
#include
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
InitializeMagick(*argv);
// Read GIF file from disk
Image image( "giraffe.gif" );
// Write to BLOB in JPEG format
Blob blob;
image.magick( "JPEG" ) // Set JPEG output format
image.write( &blob );
[ Use BLOB data (in JPEG format) here ]
return 0;
}
Image Gradients Support using C API
The open source C library ImageMagick enables software developers to create image gradients of multiple colors using C commands. You can easily create a gradual blend of one or multiple colors using horizontal, vertical, circular, or elliptical shapes. The library has enhanced the gradients through the use of several –defines such as specifying the direction of the linear gradient, Identifying coordinates of the center point for the radial gradient, Restricting the gradient to a larger or smaller region than the image dimensions, and so on. Moreover, for non-linear greyscale gradients, please add -colorspace RGB -colorspace gray before saving the output.
How to Create a Simple Grayscale Gradient via C API?
void test_wand(LPTSTR lpCmdLine)
{
MagickWand *m_wand = NULL;
PixelWand *p_wand = NULL;
PixelIterator *iterator = NULL;
PixelWand **pixels = NULL;
int x,y,gray;
char hex[128];
MagickWandGenesis();
p_wand = NewPixelWand();
PixelSetColor(p_wand,"white");
m_wand = NewMagickWand();
// Create a 100x100 image with a default of white
MagickNewImage(m_wand,100,100,p_wand);
// Get a new pixel iterator
iterator=NewPixelIterator(m_wand);
for(y=0;y<100;y++) {
// Get the next row of the image as an array of PixelWands
pixels=PixelGetNextIteratorRow(iterator,&x);
// Set the row of wands to a simple gray scale gradient
for(x=0;x<100;x++) {
gray = x*255/100;
sprintf(hex,"#%02x%02x%02x",gray,gray,gray);
PixelSetColor(pixels[x],hex);
}
// Sync writes the pixels back to the m_wand
PixelSyncIterator(iterator);
}
MagickWriteImage(m_wand,"bits_demo.gif");
// Clean up
iterator=DestroyPixelIterator(iterator);
DestroyMagickWand(m_wand);
MagickWandTerminus();
}
Apply Special Effects to Images via C
The open source C library ImageMagick has provided functionality for applying different types of effects to images. It has provided several important functions such as blurring, sharpening, threshold, creating shadows, feathering shapes, or tinting an image using a couple of lines of C code. There some special blur effects like radio blur and motion blur are also supported.
How to Apply Tiled Font Effect via C API?
magick_wand = NewMagickWand();
d_wand = NewDrawingWand();
p_wand = NewPixelWand();
set_tile_pattern(d_wand,"#check","pattern:checkerboard");
PixelSetColor(p_wand,"lightblue");
// Create a new transparent image
MagickNewImage(magick_wand,320,100,p_wand);
// Set up a 72 point font
DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
DrawSetFontSize(d_wand,72);
// Now draw the text
DrawAnnotation(d_wand,28,68,"Magick");
// Draw the image on to the magick_wand
MagickDrawImage(magick_wand,d_wand);
// Trim the image
MagickTrimImage(magick_wand,0);
// Add a transparent border
PixelSetColor(p_wand,"lightblue");
MagickBorderImage(magick_wand,p_wand,5,5);
// and write it
MagickWriteImage(magick_wand,"text_pattern.png");
Large Images Support
The C library ImageMagick has provided support for working with very large images. It enables developers to open, read, process, or write images up to Mega-, Giga-, or Tera-pixel sizes inside their own image processing applications. The library allows resizing an image to a quarter million pixels square. While working with large images you need to make sure the availability of large memory resources. If the default temporary disk partition is small then guide ImageMagick to use another partition with a sufficient amount of free space.
