ImageMagick command without intermediary file fails

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
TheMightyPope
Posts: 3
Joined: 2015-05-25T11:26:35-07:00
Authentication code: 6789

ImageMagick command without intermediary file fails

Post by TheMightyPope »

I'm working on the example from ImageMagick called " Gel" Effects http://www.imagemagick.org/Usage/advanced/#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):

Code: Select all

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:

Code: Select all

( +clone -background navy -shadow 80x4+4+4 ) +swap -background none -flatten  
This step causes 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:

Code: Select all

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:

Code: Select all

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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick command without intermediary file fails

Post by snibgo »

I haven't checked this, but I think "-flatten" uses the "-compose" setting. So, when it is one big command, you need to reset to the default setting: "-compose Over".

This looks like a Windows BAT file. Don't forget to double % signs.
snibgo's IM pages: im.snibgo.com
TheMightyPope
Posts: 3
Joined: 2015-05-25T11:26:35-07:00
Authentication code: 6789

Re: ImageMagick command without intermediary file fails

Post by TheMightyPope »

Hi Snibgo..

That actually worked, I added it like this.

Code: Select all

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  -compose Over

-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
Can you explain a bit more WHY it worked?

My understanding of the entire command was that first a 'canvas' is created with the basic rectangle. Then a clone is added with -composite, which I thought would 'merge' the two images. Another clone with -composite is added.. At this time, I thought there would be literally just one image on the stack, and if I save the image at this time, it looks fine.

By adding, and fixing this, as you suggest, what is actually happening that makes it work?

Thank you very much for your help, I really appreciate it.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick command without intermediary file fails

Post by snibgo »

I didn't even look at the whole command. I just saw the last part contained "-layers", and the immediately preceding "-compose" wasn't "Over".
TheMightyPope wrote:My understanding of the entire command was that first a 'canvas' is created with the basic rectangle. Then a clone is added with -composite, which I thought would 'merge' the two images.
Yes.

It creates a transparent image, then draws a shape on it. Then "+clone" copies that image, so we now have two images in the list. "(" means we do things to just that image, up to "-roll". Then we make a clone of that image, so now we have three images. Before the ")", we "-composite", so this combines the last two images into one, leaving us with just two. The next "-composite" combines the two images into one.

Towards the end, "( +clone -background navy -shadow 80x4+4+4 )" makes a copy of the only image in the list and changes it into a shadow, so now we have two images. "+swap" swaps the order of these so the shadow will be behind the button. "-flatten" is very similar to "-composite": it combines the two images into one.

Does that explain it? You can insert "+write abc01.png" etc anywhere you want to see intermediate results.

As a matter of style, I would put "-compose Over" immediately before "-flatten", instead of where you have it.
snibgo's IM pages: im.snibgo.com
TheMightyPope
Posts: 3
Joined: 2015-05-25T11:26:35-07:00
Authentication code: 6789

Re: ImageMagick command without intermediary file fails

Post by TheMightyPope »

OK, I moved the -compose over as you suggested, and that still works, so that's good.

I think I get it. So using '-flatten' is would have used the last -compose setting, which was "Multiply"... Causing the image to be flattened wrong. By inserting the -Compose Over, flatten will use "Over" instead of Multiply, and because this relates to the -flatten command, it makes sense to have it just before the -flatten at the end.

That makes sense... I actually thought the -compose Over would perform a command on the spot, and didn't realise it's a setting for subsequent commands (I'm new to this). This also explains why saving the image, then applying the last (clone) works, because -flatten would have been 'reset' to it's default setting, which I assume is "-composite Over"...

Thank you very much for your patience explaining it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick command without intermediary file fails

Post by fmw42 »

snibgo wrote:As a matter of style, I would put "-compose Over" immediately before "-flatten", instead of where you have it.
From my experience, IM does not always reset the -compose method in a command line of parenthesis processing that does various types of compose. So often one needs to manually reset it with -compose over before -compose <math type methods> or -compose copy_opacity, and especially before -flatten.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick command without intermediary file fails

Post by snibgo »

TheMightyPope wrote:... I think I get it. ...
That's it.

"-compose" is a setting for "-composite", "-flatten" and "-layers flatten". The default setting is "Over".
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ImageMagick command without intermediary file fails

Post by anthony »

-compose is also used for many compound operators like -frame and -border too :-)

-flatten is just a legacy short hand for -layers flatten they are really the same thing.

When dealing with shadows I often like to use -layers merge +repage as it ensures the shadow is never slipped regardless of its offset or bluriness.

See Generating Shadows
http://www.imagemagick.org/Usage/blur/#shadow
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply