Andy
Andy

Reputation: 902

ImageMagick command without intermediary file fails

I'm working on the example from ImageMagick called "Gel" Effects on the ImageMagick Example pages.

Instead of executing the ImageMagick script in many steps with intermediary images, I want to do it in ONE step with no intermediary files.

My code looks like this (95% same as the example):

convert.exe -size 150x64 xc:none  -fill green 
-draw "roundrectangle 10,10 140,54 8,8"  

( +clone -alpha extract -blur 0x12 -shade 110x0 
   -normalize -sigmoidal-contrast 16,60% -evaluate multiply .5 
   -roll +4+8 +clone -compose Screen -composite ) -compose In  -composite 

( +clone -alpha extract  -blur 0x2 -shade 0x90 -normalize -blur 0x2  
    +level 60,100%  -alpha On ) -compose Multiply -composite    

-font Verdana-Bold  -pointsize 20  -fill white  -stroke black -gravity Center  
    -annotate 0 "Download" -trim -repage 0x0+7+7   

( +clone -background navy -shadow 80x4+4+4 ) +swap -background none -flatten  
ButtonTest.png"

These steps are copies of the steps in the example.

The problem is the last clone step:

( +clone -background navy -shadow 80x4+4+4 ) +swap -background none -flatten  

cause the otherwise very nice image to become dark. I.e. no dropshadow is added. Up until that step, the one command works very nicely.

Furthermore. IF I save the image before the last step, like this:

convert.exe -size 150x64 xc:none  -fill green 
-draw "roundrectangle 10,10 140,54 8,8"  

( +clone -alpha extract -blur 0x12 -shade 110x0 
   -normalize -sigmoidal-contrast 16,60% -evaluate multiply .5 
   -roll +4+8 +clone -compose Screen -composite ) -compose In  -composite 

( +clone -alpha extract  -blur 0x2 -shade 0x90 -normalize -blur 0x2  
    +level 60,100%  -alpha On ) -compose Multiply -composite    

-font Verdana-Bold  -pointsize 20  -fill white  -stroke black -gravity Center  
    -annotate 0 "Download" -trim -repage 0x0+7+7   
tempbutton.png

and then perform the last clone on the tempbutton, like this:

convert.exe tempbutton.png
( +clone -background navy -shadow 80x4+4+4 ) +swap -background none -flatten
downloadbutton.png

then it works.

How can I avoid to save the image as that last step, and make this one big command?

Upvotes: 1

Views: 125

Answers (1)

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90335

Your link to the Example page does not lead me to what you possibly used as your template (stating it's "95% same as the example").

However, after some reformatting for better readability, for removing a typo and for adaption to my own environments (which needs the " (...) " parts escaped with back slashes like this: " \( ... \) ", I inserted an additional -compose src-over just before the final -flatten operator:

convert -size 150x64 xc:none -fill green -draw "roundrectangle 10,10 140,54 8,8" \
                                                                                 \
  \( +clone -alpha extract -blur 0x12 -shade 110x0                               \
     -normalize -sigmoidal-contrast 16,60% -evaluate multiply .5                 \
     -roll +4+8 +clone -compose Screen -composite                                \
  \)                                                                             \
                                                                                 \
  -compose In  -composite                                                        \
                                                                                 \
  \( +clone -alpha extract  -blur 0x2 -shade 0x90 -normalize -blur 0x2           \
     +level 60,100%  -alpha On                                                   \
  \)                                                                             \
                                                                                 \
  -compose Multiply -composite                                                   \
  -font Verdana-Bold  -pointsize 20  -fill white  -stroke black -gravity Center  \
  -annotate 0 "Download" -trim -repage 0x0+7+7                                   \
                                                                                 \
  \( +clone -background navy -shadow 80x4+4+4                                    \
  \)                                                                             \                                                                                      \
  +swap -background none -compose src-over -flatten                              \
    "ButtonTest.png"

The reason for that is this:

  1. -compose <some-composite-operator> is an Image Setting.

  2. Its default value is src-over.

  3. The -flatten operator you use just before writing the final result image is just a shortcut for a -layers flatten operation.

  4. The -layers flatten operation works like this:

    "Create a canvas the size of the first images virtual canvas using the current -background color, and -compose each image in turn onto that canvas. Images falling outside that canvas is clipped. Final image will have a zero virtual canvas offset." see IM docu

  5. Image Settings like -compose ... persist until the complete command has ended, or until they are explicitly changed by another -compose ... setting later on the command line.

  6. Your previously supplied -compose Multiply setting is still active while you do the final -flatten (a.k.a. -layers flatten). That gives the output you see...

However, when you run your second, separate command to create your button, you start afresh:

  1. The previous -compose Multiply setting is no longer known.
  2. An explicit -compose <something> setting is not given for the -flatten operation.
  3. Hence, the implicit default -compose src-over is used for -flatten.

So to integrate that setting into the one, single command for ImageMagick, you have to reset the previous -compose multiply by running -compose src-over -flatten before writing the output.

Here is a comparison "before" (your command's output, left) and "after" (my command's output, right) the modifications I proposed:

ButtonTest.png, first attempt by OP     ButtonTest.png, with modified command


You should study my previous answer on the topic of "The Architecture of the ImageMagick Command Line" here:

It explains the difference in the following categories of ImageMagick command line params. To understand these differences is essential if you want to construct complex IM commands with multiple operations happening:

  1. Image Settings
  2. Image Operators
  3. Image Sequence Operators
  4. Image Stack Operators

Upvotes: 2

Related Questions