Blending image followed by setting background

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
andrey
Posts: 10
Joined: 2010-03-01T09:39:57-07:00
Authentication code: 8675308

Blending image followed by setting background

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Blending image followed by setting background

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

Re: Blending image followed by setting background

Post by snibgo »

Another useful switch is +clone or -clone, so convert doesn't have to read the same file multiple times.
snibgo's IM pages: im.snibgo.com
andrey
Posts: 10
Joined: 2010-03-01T09:39:57-07:00
Authentication code: 8675308

Re: Blending image followed by setting background

Post 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 :)
Post Reply