[Oiio-dev] OIIO tips 20 Mar 2017: Composite text with drop shadow
Larry Gritz
lg at larrygritz.com
Mon Mar 20 23:08:16 PDT 2017
Hey, it worked!!
As an aside, the commands I explained all assume a linear color space and workflow. In order to generate the PNGs to embed in the email for the pretty pictures and have it look correct in your browser or email client, for each of these commands, I replaced
... -o out.tif
with
... --colorconvert linear sRGB -o out.png
But otherwise, those really were the exact commands I used to make the examples.
> On Mar 20, 2017, at 11:00 PM, Larry Gritz <lg at larrygritz.com> wrote:
>
> 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
>
> <text.png>
>
> And let's make an RGBA file for our background using something from our testsuite:
>
> $ cp tahoe-tiny.tif ./bg.tif
>
> <bg.png>
>
> 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.
>
> <comp1.png>
>
> 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
>
> <comp2.png>
>
> Voilà! Much nicer! Adjust the blur size and mulc threshold to suit your taste.
>
>
>
> --
> Larry Gritz
> lg at larrygritz.com <mailto:lg at larrygritz.com>
>
>
> _______________________________________________
> Oiio-dev mailing list
> Oiio-dev at lists.openimageio.org
> http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org
--
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/2c9026f1/attachment.htm>
More information about the Oiio-dev
mailing list