IM to GM conversion

  • Bill

    Bill - 2011-09-28

    Heard some good things about GM vs. IM in regards to optimizations.  Trying to convert an IM command to GM, but it seems the composite statement is not available inside convert when using GM.   Maybe this requires 2 steps now, but not having much luck.  Any tips are appreciated.

    IM command:

    convert -respect-parenthesis  \( INPUT_FILE -colorspace gray -type grayscale -normalize \)  \( -clone 0 -colorspace gray -negate -lat 15x15+5% -contrast-stretch 0 \) -compose copy_opacity -composite -fill "white" -opaque none +matte -background white -quality 100 OUTPUT_FILE



  • Bob Friesenhahn

    Bob Friesenhahn - 2011-09-28

    The parenthesis command-line syntax is not available in GM.  The ability to clone images in the command line and use them later is also not (easily) available (there is a way if you really, really, need it).  Composition may be performed via -flatten and the -compose option is available to specify how a given image should be composed/treated during the -flatten operation.  If one uses the -flatten option, then it should be the last processing option prior to saving the result.

    Since GM forked from ImageMagick, ImageMagick has almost entirely replaced its command line syntax (and likely will do so again), yet there is often support for the original syntax available.

  • Bill

    Bill - 2011-09-28

    Thanks for the quick response.  It's ok if it has to be done in multiple steps.  I'd like to give it try it.  My skills are apparently not adequate to figure it out.    I have been trying.  Can you post a solution?  Appreciate it.

  • Bill

    Bill - 2011-09-29

    I can't seem to replicate it properly.

    I think I get the right image from the clone section with this:

    gm convert in.png -negate -lat 15x15+5% -normalize lat.png

    I'm trying to figure out how to merge this with the original.

  • Bob Friesenhahn

    Bob Friesenhahn - 2011-09-29

    Note that it does not make any sense to use -normalize after -lat since -lat produces a bilevel image that -normalize should have no effect on (except to consume more CPU).

    You can use the 'gm composite' command to perform composition.  The syntax is:

    gm composite   change-image  base-image   output-image

    The -compose option specifies the composition operator.  The CopyOpacity operator is probably similar to ImageMagick's 'copy_opacity' operator.

    I suggest using the MIFF file format for temporary files since it is faster than PNG.

    'gm convert' can also do the composition via its -flatten command.  There is also a -mosaic command which is similar to -flatten except that it automatically provides an underlying canvas image of the right size considering the size of all of the images and any provided page offsets.

    gm convert -background black  image.miff  -compose CopyOpacity opacity.miff -mosaic output.png

  • Bill

    Bill - 2011-09-29

    Great tips!

    I'm not sure if I'm doing this right.  This is what I've done:

    gm convert in.png image.miff
    gm convert image.miff -negate -lat 15x15+5% opacity.miff
    gm convert -background black image.miff -compose CopyOpacity  opacity.miff -mosaic output.png

    Output.png looks the same as the opacity image?  Stuck again.

    When using the IM command, it create this opacity image which is bi-level/negative of the original, then uses that to clear the background of the original image.


  • Bill

    Bill - 2011-09-29

    In the IM command, it looks like they normalize the original image, create a negative of the original, use composite to overlay and -fill white to clean the background.

    I'm not having luck getting the same result.

  • Bob Friesenhahn

    Bob Friesenhahn - 2011-09-30

    Where did this IM command come from?  Is there a place on the web where the description of its purpose can be found?   I tried executing it on the classic Postscript golfer image ("") and found that its primary effect on that image was to change large areas of solid black to white, but with a thick dark border around those regions.  Smaller areas of black were not effected.

    My description of the composite command may be wrong.  Sometimes I have to experiment to remind myself of how the syntax works. :-)


Log in to post a comment.