[Oiio-dev] OIIO tips 20 Mar 2017: Composite text with drop shadow
Larry Gritz
lg at larrygritz.com
Mon Mar 20 23:00:24 PDT 2017
Let's see if this comes through the mail list with images intact. If not, I'll make it into a wiki page or something.
---
Let's say you have a text image, white on black, and you want to composite it over an existing background image. Let's quickly generate one to have it as an example:
$ oiiotool -create 128x96 3 -text:x=20:y=50:size=32:color=1,0,0 "Hello" -d uint8 -o text.tif
And let's make an RGBA file for our background using something from our testsuite:
$ cp tahoe-tiny.tif ./bg.tif
Now, we could naively composite it:
$ oiiotool text.tif bg.tif --over -o comp.tif
oiiotool ERROR: over : images must have alpha channels
Oops. Well, adding alpha to the background would be easy,
oiiotool ... bg.tif -ch R,G,B,A=1.0 ...
But what about the foreground?
Well, if we really were just using oiiotool to make the text in the first place, we could have given it an alpha channel:
$ oiiotool -create 128x96 4 -text:x=20:y=50:size=32:color=1,0,0,1 "Hello" -d uint8 -o textrgba.tif
But let's suppose for a minute that we didn't have that luxury, we have an RGBA image of text and that's that. We can construct an alpha channel from the luminance, then thresholding it, like this, step by step (first, an invalid command line that explains, then a fully valid command line):
oiiotool text.tif # read input
--dup # duplicate it on the stack
--chsum:weight=.2126,.7152,.0722 # compute luminance as single channel
--mulc 20 # multiply it...
--clamp:min=0:max=1 # ...and clamp to threshold
--chappend # mash the original RGB with the luminance-computed alpha
--chnames R,G,B,A # make sure that new channel has the right name
bg.tif # read the background
--ch R,G,B,A=1.0 # add an alpha channel to the backgound (opaque)
--over # composite
-o comp1.tif # output
$ oiiotool text.tif --dup --chsum:weight=.2126,.7152,.0722 --mulc 20 --clamp:min=0:max=1 --chappend -chnames R,G,B,A bg.tif -ch R,G,B,A=1.0 --over -o comp1.tif
The purpose of the --mulc and --clamp is so that a dimmer text color (less than luminance 1.0) won't make a semi-transparent alpha and show the background color through the text.
This is fairly nice, but a little naive about the text readability. What we really want is a bit of a blurred drop shadow, black rim around the letters, for better visibility against light backgrounds. So let's add a blur to the alpha we are generating, thus making the alpha image extend past the edges of the letters themselves to suppress part of the background:
$ oiiotool text.tif --dup --chsum:weight=.2126,.7152,.0722 --blur 5x5 --mulc 20 --clamp:min=0:max=1 --chappend -chnames R,G,B,A bg.tif -ch R,G,B,A=1.0 --over -o comp2.tif
Voilà! Much nicer! Adjust the blur size and mulc threshold to suit your taste.
--
Larry Gritz
lg at larrygritz.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openimageio.org/pipermail/oiio-dev-openimageio.org/attachments/20170320/22bd3ff1/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: text.png
Type: image/png
Size: 1176 bytes
Desc: not available
URL: <http://lists.openimageio.org/pipermail/oiio-dev-openimageio.org/attachments/20170320/22bd3ff1/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bg.png
Type: image/png
Size: 24554 bytes
Desc: not available
URL: <http://lists.openimageio.org/pipermail/oiio-dev-openimageio.org/attachments/20170320/22bd3ff1/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: comp1.png
Type: image/png
Size: 27816 bytes
Desc: not available
URL: <http://lists.openimageio.org/pipermail/oiio-dev-openimageio.org/attachments/20170320/22bd3ff1/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: comp2.png
Type: image/png
Size: 27306 bytes
Desc: not available
URL: <http://lists.openimageio.org/pipermail/oiio-dev-openimageio.org/attachments/20170320/22bd3ff1/attachment-0007.png>
More information about the Oiio-dev
mailing list