CImg Error : 'gm.exe' is not recognized as an internal or external command,

9.6k views Asked by At

I am new to c++ programming , today i was trying to save an image using CImg . CImg is C++ Template Image Processing Library .

The basic code i wrote is(Please forgive any syntax erros , as copied part of my codes) :

#include "CImg.h"// Include CImg library header.
#include <iostream>

using namespace cimg_library;
using namespace std;

const int screen_size = 800;

//-------------------------------------------------------------------------------
//  Main procedure
//-------------------------------------------------------------------------------
int main()
{

CImg<unsigned char> img(screen_size,screen_size,1,3,20);
CImgDisplay disp(img, "CImg Tutorial");   
//Some drawing using img.draw_circle( 10, 10, 60, RED);
img.save("result.jpg"); // save the image    
return 0;   
}

But I cannot run my program as it says :

Invalid Parameter - 100%
'gm.exe' is not recognized as an internal or external command,
operable program or batch file.

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

[CImg] *** CImgIOException *** [instance(800,800,1,3,02150020,non-shared)] CImg<unsigned char>::save_other() : Failed to save file 'result.jpg'. Format is not natively supported, and no external commands succeeded.
terminate called after throwing an instance of 'cimg_library::CImgIOException'
  what():  [instance(800,800,1,3,02150020,non-shared)] CImg<unsigned char>::save_other() : Failed to save file 'result.jpg'. Format is not natively supported, and no external commands succeeded.

Though i can see the image , I cannot save it. After googling a bit i found people saying to install ImageMagick , i have installed it but no help . Some of the Forum says to compile against libjpeg, libpng, libmagick++. But i don't know how to compile against those libraries. I am using Eclipse CDT plugin to write C++ project . Please help me .

3

There are 3 answers

0
Majiick On

I know this question is old, but I kept getting the same error on one project and not on another and this is the only thing on Google.

To get rid of it, you must do 2 things:

  1. Install dynamic ImageMagick libraries for your appropriate OS and architecture(32/64). Link

  2. I was using VisualStudio, and the character set must be set to "Unicode". The error would appear again when I reverted back to Multi-Byte character set. I guess this has something to do with the way CImg handles strings and miscompares them.

1
Maxím G. On

I had the same error, and installing of GraphicsMagick (not ImageMagick) helped me.

I've downloaded and installed GraphicsMagick-1.3.26-Q8-win64-dll.exe from ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/windows/. You may choose another one, if you need:

Note that the QuantumDepth=8 version (Q8) which provides industry standard 24/32 bit pixels consumes half the memory and about 30% less CPU than the QuantumDepth=16 version (Q16) which provides 48/64 bit pixels for high-resolution color. A Q8 version is fine for processing typical photos intended for viewing on a computer screen. If you are dealing with film, scientific, or medical images, use ICC color profiles, or deal with images that have limited contrast, then the Q16 version is recommended.

Important: during installation, don't remove checkbox "Update executable search path", which updates environment variable %PATH%, making gm.exe available from any place.

In my case, it was also required to install Ghostscript - which is highly recommended to install by GraphicsMagick. There is a link to x64 Ghostscript: https://sourceforge.net/projects/ghostscript/files/GPL%20Ghostscript/9.09/gs909w64.exe/download (I've put it here, because links from the GraphicMagick websites leads you to 32-bit only).

After that, it worked fine for me.

1
bvalabas On

For some image formats (as .jpg, .png, .tif and basically all formats that require data compression), CImg will try to use an external tool to save them (such as convert from ImageMagick or gm from GraphicsMagick). If you don't have any installed, then you won't be able to save .jpg files without having to link your code with the libjpeg library, to get a native support for JPEG read/write (then, you'll need to #define cimg_use_jpeg before #include "CImg.h", to tell the library you want to use the libjpeg features).

If you want to keep things simpler, I'd recommend to save your image using another (non-compressed) image format, as .bmp or .ppm. These formats are handled natively by CImg and do not require to link with external libraries.