Powered by NetworkEleven ImageMagick logo

This document describes how ImageMagick performs color reduction on an image. To fully understand this document, you should have a knowledge of basic imaging techniques and the tree data structure and terminology.

> Description

For purposes of color allocation, an image is a set of n pixels, where each pixel is a point in RGB$imagelob=>$blob); This is a more efficient and less memory intensive way to query if an image exists and what its characteristics are.

PreviewImage() tiles 9 thumbnails of the specified image with an image processing operation applied at varying strengths. This may be helpful pin-pointing an appropriate parameter for a particular image processing operation. Choose from these operations: Rotate, Shear, Roll, Hue, Saturation, Brightness, Gamma, Spiff, Dull, Grayscale, Quantize, Despeckle, ReduceNoise, AddNoise, Sharpen, Blur, Threshold, EdgeDetect, Spread, Solarize, Shade, Raise, Segment, Swirl, Implode, Wave, OilPaint, CharcoalDrawing, JPEG. Here is an example:

    $preview = $image->Preview('Gamma');
    $preview->Display();

To have full control over text positioning you need font metric information. Use

    ($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) =
$image->QueryFontMetrics(parameters);
Where parameters is any parameter of the Annotate method. The return values are:
  • character width
  • character height
  • ascender
  • descender
  • text width
  • text height
  • maximum horizontal advance


Call QueryColor() with no parameters to return a list of known colors names or specify one or more color names to get these attributes: red, green, blue, and opacity value.

    @colors = $image->QueryColor();
($red, $green, $blue, $opacity) = $image->QueryColor('cyan');
($red, $green, $blue, $opacity) = $image->QueryColor('#716bae');


QueryColorname() accepts a color value and returns its respective name or hex value;
      $name = $image->QueryColorname('rgba(80,60,0,0)');


Call QueryFont() with no parameters to return a list of known fonts or specify one or more font names to get these attributes: font name, description, family, style, stretch, weight, encoding, foundry, format, metrics, and glyphs values.
    @fonts = $image->QueryFont();
$weight = ($image->QueryFont('Helvetica'))[5];


Call QueryFormat() with no parameters to return a list of known image formats or specify one or more format names to get these attributes: adjoin, blob support, raw, decoder, encoder, description, and module.
    @formats = $image->QueryFormat();
    ($adjoin, $blob_support, $raw, $decoder, $encoder,
      $description, $module) = $image->QueryFormat('gif');


Call MagickToMime() with the image format name to get its MIME type such as image/tiff from tif.
    $mime = $image->MagickToMime('tif');

Use RemoteCommand() to send a command to an already running display or animate application. The only parameter is the name of the image file to display or animate.

Finally, the Transform() method accepts a fully-qualified geometry specification for cropping or resizing one or more images. For example,

    $p = $image->Transform(crop=>'100x100');
You can optionally add Image to any method name above. For example, PingImage() is an alias for method Ping().
Back to Contents

> Handling Errors



All PerlMagick methods return an undefined string context upon success. If any problems occur, the error is returned as a string with an embedded numeric status code. A status code less than 400 is a warning. This means that the operation did not complete but was recoverable to some degree. A numeric code greater or equal to 400 is an error and indicates the operation failed completely. Here is how errors are returned for the different methods:

  • Methods which return a number (e.g. Read(), Write()):
  •     $x = $image->Read(...);
    warn "$x" if "$x"; # print the error message
    $x =~ /(\d+)/;
    print $1; # print the error number
    print 0+$x; # print the number of images read
  • Methods which operate on an image (e.g. Resize(), Crop()):
  •     $x = $image->Crop(...);
    warn "$x" if "$x"; # print the error message
    $x =~ /(\d+)/;
    print $1; # print the error number
  • Methods which return images (Average(), Montage(), Clone()) should be checked for errors this way:
  •     $x = $image->Montage(...);
    warn "$x" if !ref($x); # print the error message
    $x =~ /(\d+)/;
    print $1; # print the error number


Here is an example error message:
    Error 400: Memory allocation failed
Below is a list of error and warning codes:
Error and Warning Codes
Code Mnemonic Description
0 Success method completed without an error or warning
300 ResourceLimitWarning a program resource is exhausted (e.g. not enough memory)
305 TypeWarning A font is unavailable; a substitution may have occurred
310 OptionWarning a command-line option was malformed
315 DelegateWarning an ImageMagick delegate returned a warning
320 MissingDelegateWarning the image type can not be read or written because the appropriate Delegate is missing
325 CorruptImageWarning the image file may be corrupt
330 FileOpenWarning the image file could not be opened
335 BlobWarning a binary large object could not be allocated
340 StreamWarning there was a problem reading or writing from a stream
345 CacheWarning pixels could not be saved to the pixel cache
350 CoderWarning there was a problem with an image coder
355 ModuleWarning there was a problem with an image module
360 DrawWarning a drawing operation failed
365 ImageWarning the operation could not complete due to an incompatible image
380 XServerWarning an X resource is unavailable
385 MonitorWarning there was a problem with prgress monitor
390 RegistryWarning there was a problem getting or setting the registry
395 ConfigureWarning there was a problem getting a configuration file
400 ResourceLimitError a program resource is exhausted (e.g. not enough memory)
405 TypeError A font is unavailable; a substitution may have occurred
410 OptionError a command-line option was malformed
415 DelegateError an ImageMagick delegate returned a warning
420 MissingDelegateError the image type can not be read or written because the appropriate Delegate is missing
425 CorruptImageError the image file may be corrupt
430 FileOpenError the image file could not be opened
435 BlobError a binary large object could not be allocated
440 StreamError there was a problem reading or writing from a stream
445 CacheError pixels could not be saved to the pixel cache
450 CoderError there was a problem with an image coder
455 ModuleError there was a problem with an image module
460 DrawError a drawing operation failed
465 ImageError the operation could not complete due to an incompatible image
480 XServerError an X resource is unavailable
480 MonitorError there was a progress monitor error
490 RegistryError there was a problem getting or setting the registry
495 ConfigureError there was a problem getting a configuration file


The following illustrates how you can use a numeric status code:

    $x = $image->Read('rose.png');
$x =~ /(\d+)/;
die "unable to continue" if ($1 == ResourceLimitError);
Back to Contents


Top of page
"Image manipulation software that works like magick"
./usr/share/doc/imagemagick/www/quantize.html0000644000000000000000000003765510564132606021464 0ustar rootroot00000000000000