Page 1 of 1

Help altering some batch edits to have a Transparency

Posted: 2008-06-29T21:41:18-07:00
by excaluber
Alright my friend has helped me setup 2 seperate dos batch files to use imagemagick to do some conversions for me. Both work great....its setup to work with a black background...which i have and works perfect. However what i'm looking for help with is...altering these so that it's not using black but is transparent...so that they'll have the same look/effect regardless of what my background color is. Is this possible, if so would you mind showing me how?

Heres some examples of what the first script does...and the code that does it...

It takes this....

Image

Image and makes this for me... (to go below it)

So to my understanding it takes the image...flips it...crops it...then places a black gradient over top of it to make it look like its fading out. But what i'd like it to do is have the same size...but instead of placing a black gradient over top of it....turn the the small cropped image into a gradient itself...taht just went from 100% opacity to 0% opacity over the course of the size of it. That way no matter what background i put it over...it holds its look.


@ECHO OFF
:: SET SOME VARS
SET INPUTFILE=%1
SET TMPPATH=%TEMP%\imagicktemp
SET REFLEX=50
set MIRROR=%INPUTFILE:t.jpg=m.jpg%

:: CREATE TEMPORARY DIR
IF NOT EXIST %TMPPATH% mkdir %TMPPATH%

:: FETCH AND SET IMAGE SIZE
FOR /F %%x IN ('identify -format %%w %INPUTFILE%') DO SET IMGWIDTH=%%x
FOR /F %%x IN ('identify -format %%h %INPUTFILE%') DO SET IMGHEIGHT=%%x

Echo Creating file %MIRROR% from %INPUTFILE% (%IMGWIDTH%x%IMGHEIGHT%)

:: CREATING A GAP BETWEEN IMAGE AND REFLECTION
convert -size %IMGWIDTH%x5 xc:black %TMPPATH%\TAG.JPG

:: START ALL THE REAL IMAGEMAGICK :)
:: CREATE THE UPSIDE DOWN IMAGE AND CROP IT TO 120 HIGH AND 0 OFFSET
convert %INPUTFILE% -flip -crop %IMGWIDTH%x%REFLEX%+0+0 %TMPPATH%\FLIP.jpg

:: MAKE A GRADIANT (TRANSPARENT TO BLACK) AND ADD THE GRADIANT TO THE IMAGE
convert ( -size %IMGWIDTH%x%REFLEX% gradient:none-black ) %TMPPATH%\FLIP.JPG +swap -gravity south -composite %TMPPATH%\REFLECTION.JPG

:: JOIN THE TWO IMAGES
convert %TMPPATH%\TAG.JPG %TMPPATH%\REFLECTION.JPG -append %MIRROR



This second one takes a cover image, rotates it, and also places a mirror like reflection below it...(with or without a glow effect...depending on which i comment out).

Starts off looking like this...(but a bigger version..this is just a thumbnail of norm size)

Image

and can make it look like either this....

Image

or this

Image

Again...it works perfect at the moment, because i'm using a black background behind them....but i would like to have it use transparencies instead of black so that i can make it work with any background color or image.

Heres the code for this one...


@ECHO OFF
:: FETCH AND SET IMAGE SIZE
FOR /F %%x IN ('identify -format %%w %1') DO SET IMGWIDTH=%%x
FOR /F %%x IN ('identify -format %%h %1') DO SET IMGHEIGHT=%%x

:: SET SOME VARS
SET INPUTFILE=%1
SET REFLEX=50
SET GLOW=30
SET DIFF=35
SET /A SHIFT=160
SET /A AUX01=%IMGHEIGHT%-%DIFF%
SET /A AUX02=%IMGHEIGHT%*2
SET /A AUX03=%IMGWIDTH%+%SHIFT%
SET /A AUX04=((%IMGHEIGHT%-(2*%DIFF%))*2)+%DIFF%
SET /A AUX05=%IMGWIDTH%+%DIFF%
SET /A AUX06=(%IMGHEIGHT%/4)+%IMGHEIGHT%
SET /A AUX07=%IMGHEIGHT%+%GLOW%
SET /A AUX08=%IMGWIDTH%+%GLOW%
SET /A AUX09=%DIFF%+%GLOW%
SET /A AUX10=%SHIFT%+%GLOW%
SET /A AUX11=%AUX01%+%GLOW%
SET /A AUX12=%AUX02%+%GLOW%
SET /A AUX13=%AUX03%+%GLOW%
SET /A AUX14=%AUX04%+%GLOW%
SET /A AUX15=%AUX05%+%GLOW%
SET /A AUX16=%AUX06%+%GLOW%

:: START ALL THE REAL IMAGEMAGICK :)
:: WITHOUT GLOW EFFECT
::convert %INPUTFILE% -matte +repage -virtual-pixel transparent ( +clone +distort Perspective "0,0,0,0 0,%IMGHEIGHT%,0,%IMGHEIGHT% %IMGWIDTH%,0,%IMGWIDTH%,%DIFF% %IMGWIDTH%,%IMGHEIGHT%,%IMGWIDTH%,%AUX01%" ) ( -size %IMGWIDTH%x%IMGHEIGHT% gradient: -evaluate subtract 75%% -evaluate multiply 2 -flip -clone 0 +swap +matte -compose CopyOpacity -composite +compose +distort Perspective "0,0,%SHIFT%,%AUX02% %IMGWIDTH%,0,%AUX03%,%AUX04% 0,%IMGHEIGHT%,0,%IMGHEIGHT% %IMGWIDTH%,%IMGHEIGHT%,%IMGWIDTH%,%AUX01%" ) -delete 0 -background black -mosaic -crop %AUX05%x%AUX06%+0+0 %INPUTFILE%

::WITH GLOW EFFECT
convert %INPUTFILE% -matte +repage -virtual-pixel transparent ( +clone +distort Perspective "0,0,%GLOW%,%GLOW% 0,%IMGHEIGHT%,%GLOW%,%AUX07% %IMGWIDTH%,0,%IMGWIDTH%,%AUX09% %IMGWIDTH%,%IMGHEIGHT%,%IMGWIDTH%,%AUX11%" ) ( +clone -background blue -shadow %GLOW%x%GLOW%+0+0 -channel A -normalize +channel ) +swap ( -size %IMGWIDTH%x%IMGHEIGHT% gradient: -evaluate subtract 75%% -evaluate multiply 2 -flip -clone 0 +swap +matte -compose CopyOpacity -composite +compose +distort Perspective "0,0,%AUX10%,%AUX12% %IMGWIDTH%,0,%AUX13%,%AUX14% 0,%IMGHEIGHT%,%GLOW%,%AUX07% %IMGWIDTH%,%IMGHEIGHT%,%IMGWIDTH%,%AUX11%" ) -delete 0 -background black -mosaic -crop %AUX05%x%AUX16%+0+0 %INPUTFILE%


I'm brand new to this....as i said my friend help put those together for me...so Thanks for any help you can offer me.

P.S. If it matter heres the batch file that runs it all for me.....but i can use either one of those above 1 file at a time if needed...so they do work independently

CHO OFF
SET POPCORNDIR=E:\popcorn\images
FOR /R %POPCORNDIR% %%x IN (*t.jpg) DO CALL E:\popcorn\images\mirror_index.bat %%x
FOR /R %POPCORNDIR% %%x IN (*f.jpg) DO CALL E:\popcorn\images\mirror_details.bat %%x

Re: Help altering some batch edits to have a Transparency

Posted: 2008-06-29T22:36:02-07:00
by fmw42
You have a bit too much data to understand. But here is how to add a proper transparent gradient alpha channel to an image.

convert yourimage \( -size ${width}x${height} gradient: \) +matte -compose copy_opacity -composite newimage.png

Note that you must use png for the resulting image to support that gradient alpha channel. Also you will need to get your friend to help you convert this Unix command line into your Windows script.

You can also substitute the more current -alpha off for +matte

Re: Help altering some batch edits to have a Transparency

Posted: 2008-06-30T04:19:28-07:00
by excaluber
thanks for trying to help....

So to break it down...using the first script.... perhaps i ditch the last two steps and use your set of commands? so its something like this?

:: START ALL THE REAL IMAGEMAGICK :)
:: CREATE THE UPSIDE DOWN IMAGE AND CROP IT TO 120 HIGH AND 0 OFFSET
convert %INPUTFILE% -flip -crop %IMGWIDTH%x%REFLEX%+0+0 %TMPPATH%\FLIP.png

:: MAKE A GRADIANT (TRANSPARENT TO BLACK) AND ADD THE GRADIANT TO THE IMAGE
convert flip.png \( -size %IMGWIDTH%x%IMGHEIGHT% gradient: \) +matte -compose copy_opacity -composite %MIRROR.png

and that should make the flipped image itself have a transparency?


EDIT: I converted that to dos.... which looks like this...
:: CREATE THE UPSIDE DOWN IMAGE AND CROP IT TO 120 HIGH AND 0 OFFSET
convert %INPUTFILE% -flip -crop %IMGWIDTH%x%REFLEX%+0+0 %TMPPATH%\FLIP.png

:: MAKE A GRADIANT (TRANSPARENT TO BLACK) AND ADD THE GRADIANT TO THE IMAGE
convert %TMPPATH%\FLIP.png -size %IMGWIDTH%x%IMGHEIGHT% gradient: +matte -compose copy_opacity -composite %MIRROR.png

and tried it out but theres no opacity to it.

Re: Help altering some batch edits to have a Transparency

Posted: 2008-06-30T22:38:41-07:00
by fmw42
You must keep the parentheses.

See

http://www.imagemagick.org/Usage/basics/#parenthesis

In Windows, I think you remove the backslashes, though.

See

http://www.imagemagick.org/Usage/api/#windows

Here are two examples:

Original:
Image

convert zelda3.png \( -size 128x128 gradient: \) +matte -compose copy_opacity -composite zelda_alpha1.png

Image


convert zelda3.png \( -size 128x128 gradient: -negate \) +matte -compose copy_opacity -composite zelda_alpha2.png


Image

The transparency "color" depends upon the background over which you place your image with the gradient alpha channel added.

Re: Help altering some batch edits to have a Transparency

Posted: 2008-07-01T20:54:30-07:00
by excaluber
thanks...i think that helped us!

It seems to be perfect!


I'm sure i'll have more questions going forward...but so far thats everything.

Thanks again for the help :D

Re: Help altering some batch edits to have a Transparency

Posted: 2008-10-02T15:43:17-07:00
by worta
Hm. Thats pretty cool.

It works perfectly if the input file is without transparency itself - like

convert logo: \( -size 640x480 gradient: \) +matte -compose copy_opacity -composite out.png

But imagine the input file has already transparency on it. Then the output is simply a gradient... Why?
One could alter the command to

convert transp.png -flatten \( -size 640x480 gradient: \) +matte -compose copy_opacity -composite out.png

to remove the transparency - but what if you want to keep the transparent parts of the source image?
Adding "-background none" in front of the "-flatten" creates a strangely inverted output file (maybe due to the copy_opacity) but is there a working solution?

All the tests where done using ImageMagick 6.4.4-1

Thanks! ;)

Re: Help altering some batch edits to have a Transparency

Posted: 2008-10-02T16:01:59-07:00
by fmw42
worta wrote:Hm. Thats pretty cool.

It works perfectly if the input file is without transparency itself - like

convert logo: \( -size 640x480 gradient: \) +matte -compose copy_opacity -composite out.png

But imagine the input file has already transparency on it. Then the output is simply a gradient... Why?
One could alter the command to

convert transp.png -flatten \( -size 640x480 gradient: \) +matte -compose copy_opacity -composite out.png

to remove the transparency - but what if you want to keep the transparent parts of the source image?
Adding "-background none" in front of the "-flatten" creates a strangely inverted output file (maybe due to the copy_opacity) but is there a working solution?

All the tests where done using ImageMagick 6.4.4-1

Thanks! ;)
Again it is not clear what you want to do. If the input has transparency, then why add the gradient transparency channel. The +matte is supposed to turn off the transparency in the input image (if it exists), so that the gradient can replace it. Please clarify what you want to do when the input image has transparency. What is the result you want?

Re: Help altering some batch edits to have a Transparency

Posted: 2008-10-03T05:06:57-07:00
by worta
The source image has transparency because it has spaces "with no background" where you can see thru.
This is used (for a web page) to make the user see the real page background.

The desired destination should keep those transparent spots and receive a "fade out at the bottom" Effekt which can be created with a gradient (eg. "white-none") and copying only the opacity.

In pseudo code I would write "copy opacity from source gradient pixel, if existing destination pixel opacity is not 0". ;)

Re: Help altering some batch edits to have a Transparency

Posted: 2008-10-03T12:00:03-07:00
by fmw42
worta wrote:The source image has transparency because it has spaces "with no background" where you can see thru.
This is used (for a web page) to make the user see the real page background.

The desired destination should keep those transparent spots and receive a "fade out at the bottom" Effekt which can be created with a gradient (eg. "white-none") and copying only the opacity.

In pseudo code I would write "copy opacity from source gradient pixel, if existing destination pixel opacity is not 0". ;)

Then what I think you need to do is extract the alpha channel from the image, multiply it by the gradient, then replace that product for the original alpha channel in the input image.

Here is an example:

create partially transparent logo: (where white)
convert logo: -transparent white logo_t1.png

combine transparency with gradient:
convert logo_t1.png \
\( logo_t1.png -alpha extract -size 640x480 gradient: -compose multiply -composite \) \
-alpha off -compose copy_opacity -composite logo_t1_grad.png

see the new -alpha extract and other recent changes in -alpha near the bottom of the page at
http://www.imagemagick.org/Usage/basics/

It you do not have a current IM release, then you should be able to replace

logo_t1.png -alpha extract

with

logo_t1.png -channel a -separate -negate

also +matte will do the same as -alpha off

see
http://www.imagemagick.org/script/comma ... .php#alpha

Re: Help altering some batch edits to have a Transparency

Posted: 2008-10-03T18:38:41-07:00
by worta
You're incredible - exactly what I desired to produce.
Works like a charm, Thanks!

Re: Help altering some batch edits to have a Transparency

Posted: 2009-03-13T04:52:48-07:00
by MoBO
Hello,

I tried to use the provided code with the correct parenthesis but my final result is ; a black image !

Strange.
If I use "-append" instead of "-mosaic" I have all my 2 (without GLOW effect) or 3 (with GLOW effect) images.

Were is the mistake within this code (if any can tell)?

Re: Help altering some batch edits to have a Transparency

Posted: 2009-03-13T11:23:19-07:00
by fmw42
MoBO wrote:Hello,

I tried to use the provided code with the correct parenthesis but my final result is ; a black image !

Strange.
If I use "-append" instead of "-mosaic" I have all my 2 (without GLOW effect) or 3 (with GLOW effect) images.

Were is the mistake within this code (if any can tell)?

Please provide your full command line and your images if possible.