Page 1 of 1
Composing with transparency
Posted: 2010-11-20T02:26:28-07:00
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
Re: Composing with transparency
Posted: 2010-11-20T02:39:29-07:00
by Bonzo
A link to some sample images would help.
Re: Composing with transparency
Posted: 2010-11-20T06:57:18-07:00
by incubusaurus
Hi,
Okay, here are some sample images, and a further explanation of the process:
Top:
Middle:
Bottom:
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:
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
Re: Composing with transparency
Posted: 2010-11-20T07:42:26-07:00
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
Re: Composing with transparency
Posted: 2010-11-20T07:58:37-07:00
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.
Re: Composing with transparency
Posted: 2010-11-20T08:04:03-07:00
by Bonzo
I am glad its working - basicaly its just your code just put onto one line!
Re: Composing with transparency
Posted: 2010-11-20T11:52:19-07:00
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