Composing with transparency

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
incubusaurus

Composing with transparency

Post by incubusaurus »

Hi,

I am trying to recreate an effect that I have produced in Gimp. I'd like to use ImageMagick to do it, as it is an effect that I need to apply to a large number of images.

I have three images: top.png, middle.png, bottom.png. Each is on its own layer in Gimp, with the following settings:

top (70% opacity, overlay mode)
middle (40% opacity, hard light mode)
bottom (100% opacity, normal mode)

I have managed to achieve this effect with ImageMagick by calling convert twice, as follows:

Code: Select all

convert \
  bottom.png \
  \( \
    middle.png \
    -alpha set -channel A -fx '0.4' \
    -compose hardlight \
  \) \
  -composite composite.png

convert \
  composite.png \
  \( \
    top.png \
    -alpha set -channel A -fx '0.7' \
    -compose overlay \
  \) \
  -composite composite.png
That works perfectly, but I am struggling to combine it into a single statement. I have tried many variations of parenthesis, cloning layers and deleting layers, but have not quite got it right. Please could someone offer some guidance? I'm sure I'm missing something obvious, but can't quite figure it out.


Regards,

Mike
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Composing with transparency

Post by Bonzo »

A link to some sample images would help.
incubusaurus

Re: Composing with transparency

Post by incubusaurus »

Hi,

Okay, here are some sample images, and a further explanation of the process:

Top:
Image

Middle:
Image

Bottom:
Image

These two separate commands produce the required effect:

Code: Select all

convert \
  bottom.png \
  \( \
    middle.png \
    -alpha set -channel A -fx '0.4' \
    -compose hardlight \
  \) \
  -composite composite.png

convert \
  composite.png \
  \( \
    top.png \
    -alpha set -channel A -fx '0.7' \
    -compose overlay \
  \) \
  -composite composite.png
The result is this composite image:
Image

Sampling the pixels of the composite shows that it has a colour of #E01F00:

Code: Select all

convert composite.png -crop 1x1+50+50 txt:-
# ImageMagick pixel enumeration: 1,1,255,rgb
0,0: (224, 31,  0)  #E01F00  rgb(224,31,0)
I'd like to be able to combine the two convert commands into a single command, but I keep ending up with the wrong result. Here's one of many failed attempts:

Code: Select all

convert \
  bottom.png \
  \( \
    middle.png \
    -alpha set -channel A -fx '0.4' \
    -compose hardlight \
  \) \
  \( \
    top.png \
    -alpha set -channel A -fx '0.7' \
    -compose overlay \
  \) \
  -composite composite.png

convert composite.png -crop 1x1+50+50 txt:-
# ImageMagick pixel enumeration: 1,1,255,rgb
0,0: (255,  0,  0)  #FF0000  red
Here, the resulting image is plain red (#FF0000), so I've ended up with just the bottom layer showing through.


Regards,

Mike
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Composing with transparency

Post by Bonzo »

This is the code I used ( php on a windows PC ):

Code: Select all

<?php

$cmd = "bottom.png ( middle.png -alpha set -channel A -fx \"0.4\" ) -compose hardlight -composite ". 
" ( top.png -alpha set -channel A -fx \"0.7\" ) -compose overlay -composite ";

$array=array(); 
echo "<pre>";
exec( "convert $cmd composite.png 2>&1", $array);  
echo "<br>".print_r($array)."<br>";  
echo "</pre>"; 

  system(" identify -verbose composite.png ");
  ?>

  <img src="composite.png">
How you want it:

Code: Select all

convert bottom.png \( middle.png -alpha set -channel A -fx "0.4"  \) -compose hardlight -composite \( top.png -alpha set -channel A -fx "0.7" \) -compose overlay -composite composite.png 
Last edited by Bonzo on 2010-11-20T08:03:22-07:00, edited 1 time in total.
incubusaurus

Re: Composing with transparency

Post by incubusaurus »

:-) Genius!

Thanks Bonzo, that's perfect. It was the additional '-composite' that I had been missing. I'm sure I tried adding one in there before, but got an error message about a missing file.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Composing with transparency

Post by Bonzo »

I am glad its working - basicaly its just your code just put onto one line!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composing with transparency

Post by fmw42 »

try this


convert bottom.png \
\( middle.png -alpha set -channel A -fx '0.4' \) \
-compose hardlight -composite \
\( top.png -alpha set -channel A -fx '0.7' \) \
-compose overlay -composite result.png

and this will be faster as it avoids -fx


convert bottom.png \
\( middle.png -alpha set -channel A -evaluate set 40% \) \
-compose hardlight -composite \
\( top.png -alpha set -channel A -evaluate set 70% \) \
-compose overlay -composite result.png
Post Reply