I need to cut out an image in the shape of the text in another image. I think it's best shown in images.
This is a photo of a cat:
and this is the text I wish to cut out:
The resulting image would be this:
The text image will always be black with a transparent background, and the resulting cut-out should too have a transparent background. Both input images will also be the same size.
No java here, but the needed image operations are easy to understand. In Mathematica:
Create a new BufferedImage and iterate over all the pixels of word cat and if they are black, copy the cat-image pixels to the new image.
Here is some code: (Final working code, supports anti-alias)
First, make the black portion of the "Cat" image transparent. See here for help with this. Then, composite that image over the picture of your favorite cat (mine is Sheeba).
The nice thing about this is you can make the transparent text image once, save it, and then apply it to all of Sheeba's family and friends!
Use
GlyphVector
. UseFont
classYou can get outline
Shape
from glyph vector by public abstract ShapegetOutline()
Assign the outline
Shape
as a clip to yourGraphics
instance.Draw the image on the graphics.
Only clipped shape will be filled.
You can do it in Java with just a few lines of source code, using Marvin Framework
source code: