Blending image followed by setting background
Posted: 2010-03-01T10:11:54-07:00
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:
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?
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%
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?