Page 1 of 1

Blending image followed by setting background

Posted: 2010-03-01T10:11:54-07:00
by andrey
I have images with alpha channel (PNG and TGA) that I want to transform in following way: all pixels that are not fully transparent (alpha > 0) must be blended with given color, after that all pixels that are fully transparent (alpha == 0) must be set to another color and set fully opaque.

I made following batch file to transform single file:

Code: Select all

@echo off

SET SELF=%0
shift
SET INPUT_FILE=%0
shift
SET RENDER_ON=%0
shift
SET OUTPUT=%0
shift

if "%OUTPUT%"=="" (
	echo Usage: %SELF% ^<file with transparency^> ^<color to render on^> ^<output file^> [^<transparent color^>]
	echo color may be one of:
	echo 	- color name: blue, red, magenta, ...
	echo 	- web color format: "#ff00ff", "#7f7f7f", ...
	echo 	- rgb color: "rgb(255, 0, 255)", "rgb(96, 96, 96)", ...

	exit 1
)

if "%0"=="" (
	SET COLOR_KEY=magenta
) else (
	SET COLOR_KEY=%0
)

convert "(" "%INPUT_FILE%" "(" "%INPUT_FILE%" -fill "%RENDER_ON%" -draw "rectangle 0,0 2048,2048" ")" -compose dst-over -composite -transparent "%RENDER_ON%" -alpha off ")" "(" "%INPUT_FILE%" -alpha extract -level 0,1 -alpha off ")" -compose copy-opacity -composite -background %COLOR_KEY% -alpha background -alpha off %OUTPUT%
It does it's job, but I don't like it.
First, as you can see, i'm trying to draw 0,0 2048,2048 rectangle to be used as background for alpha blending, so if I happen to process image larger than 2048 in any direction, it will produce broken image. Is there a way to fill whole image regardless of it's size?
Second, I don't really like that "load image again then fill it with target color" part. Is there away to simply generate rectangle filled with some color having same size as previously loaded image?

Also, I suppose there is simpler way to do what I want?

Re: Blending image followed by setting background

Posted: 2010-03-01T14:02:47-07:00
by fmw42
Is there a way to fill whole image regardless of it's size?
convert image -fill red -colorize 100 redimage.png

see http://www.imagemagick.org/script/comma ... p#colorize

Re: Blending image followed by setting background

Posted: 2010-03-01T15:20:59-07:00
by snibgo
Another useful switch is +clone or -clone, so convert doesn't have to read the same file multiple times.

Re: Blending image followed by setting background

Posted: 2010-03-02T03:04:14-07:00
by andrey
Thanks, I've come to this command line:

Code: Select all

convert "%INPUT_FILE%" "(" +clone -fill "%RENDER_ON%" -colorize 100 ")" -compose dst-over -composite -alpha off "(" -clone 0 -alpha extract -level 0,1 -alpha off ")" -compose copy-opacity -composite -background "%COLOR_KEY%" -alpha background -alpha off "%OUTPUT%"
which at least works correctly :)