[Oiio-dev] OIIO tips 20 Mar 2017: Composite text with drop shadow
Heng Zhou
hzhou3 at clemson.edu
Tue Mar 21 04:40:29 PDT 2017
Hi, Gritz:
Thank you for the interesting post. I happen to be working on a project in
which I need to composite a text onto a background image. But both the text
and background image are generated by C++ code, so I hope I can write a few
lines of code to do the same job as what oiiotool does in your post. I have
no idea how. Could you (or any other nice people) please share a C++
snippet with me? I don't need a shadowed text; just displaying it on the
image I generated would suffice. Thanks a lot!
Best Regards
Heng Zhou
On Tue, Mar 21, 2017 at 2:00 AM, 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
>
>
> 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
>
>
>
> _______________________________________________
> Oiio-dev mailing list
> Oiio-dev at lists.openimageio.org
> https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.openimageio.org_listinfo.cgi_oiio-2Ddev-2Dopenimageio.org&d=DwIFaQ&c=Ngd-ta5yRYsqeUsEDgxhcqsYYY1Xs5ogLxWPA_2Wlc4&r=yN_mtgnvh2k3WD8QIZam9tbZUPHJMoZc_18KfiSrxx0&m=euk9A4Ibxbdf6xWtliyUXHguHMD7O_gAzN3HZ6km25E&s=I64wq-2NHLavZXU1sdfiiXSdK59wX0HuUtIxnG4WhD0&e=
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openimageio.org/pipermail/oiio-dev-openimageio.org/attachments/20170321/4f3bf13b/attachment-0001.htm>
-------------- 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/20170321/4f3bf13b/attachment-0004.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/20170321/4f3bf13b/attachment-0005.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/20170321/4f3bf13b/attachment-0006.png>
-------------- 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/20170321/4f3bf13b/attachment-0007.png>
More information about the Oiio-dev
mailing list