# Apply an image as a texture to text

I have the following equation

eq = Sum[x^2, {x, 0, 10}]


and an image image = Import["http://creativity103.com/collections/Graphic/rainbowbars.jpg"].

How might I output a transparent version of the equation above with the image used as a tile for the text.

For example I'm looking for something similar to this gradient image expect for the text above.

Edit: it wasn't very clear in the question, but I am looking to tile the background image if possible.

-
I'd like a new formulation for the question. If you put a transparent text on top of an image, well, then you get the same image. There are several different way to use an image as a tiling, so be more precise. – C. E. Aug 6 '14 at 0:58
@Pickett Included an image. Basically I'm looking to do something similar to a gradient but with an actual image. – William Aug 6 '14 at 1:07
Your added screen shot doesn't show any kind of a background behind the text, so I still don't get what you are looking for. – m_goldberg Aug 6 '14 at 1:09
@m_goldberg I believe it is fixed. – William Aug 6 '14 at 1:15

ImageAdd is your friend:

image = Import["http://creativity103.com/collections/Graphic/rainbowbars.jpg"];

text = HoldForm @ Sum[x^2, {x, 0, 10}];
img2 = Image @ Rasterize @ Style[text, 100, Bold];

rainbow = image ~ImageResize~ ImageDimensions[img2] ~ImageAdd~ img2


With Pickett's extension for outlining:

img3 = ColorNegate[img2] ~Dilation~ 2 // ColorNegate;



-
I've already up-voted this, but it would be even better if the text were outlined as shown in the question. Without a black outline the text is difficult to read for those, like me, who have poor vision, – m_goldberg Aug 6 '14 at 1:52
@m_goldberg Tomorrow – Mr.Wizard Aug 6 '14 at 5:37
Here's one way (image). – C. E. Aug 6 '14 at 10:03
@Pickett Thanks! I'll include it in my answer, with credit of course. – Mr.Wizard Aug 6 '14 at 16:56

Here's a method based on creating a MeshRegion from the text:

text = Style[HoldForm @ Sum[x^2, {x, 0, 10}], 100, Bold];
graphics = First[text ~ExportString~ "PDF" ~ImportString~ "PDF"];
region = DiscretizeGraphics[graphics, MaxCellMeasure -> 5];
image = ExampleData[{"ColorTexture", "Kingwood"}];

RegionPlot[region, Frame -> False, BoundaryStyle -> Black, PlotStyle -> Texture[image]]


Or in 3D...

Plot3D[1, {x, y} ∈ region,
PlotStyle -> Texture[image],
Extrusion -> 10, BoxRatios -> Automatic,
Mesh -> False, Boxed -> False, Axes -> False]


# Tiling

To tile the image you can use TextureCoordinateFunction, e.g:

RegionPlot[region, Frame -> False, BoundaryStyle -> Black,
PlotStyle -> Texture[image],
TextureCoordinateFunction -> ({5 #1, 5 #2} &)]


-
Simon, I have been having trouble converting a PDF to FilledCurve graphics for a while now. Here's what graphics looks like on my machine: i.stack.imgur.com/zCMGX.png. What version and OS are you using? – Michael E2 Aug 6 '14 at 14:40
@MichaelE2, I'm using Windows 7 (64 bit) and version 10. It worked fine with version 9 too. – Simon Woods Aug 6 '14 at 14:45
thanks. It works fine in V9 for me, too. I have only a beta version of V10 -- waiting for my IT department to catch up to the update. I'm hoping the bug will go away in the final release. – Michael E2 Aug 6 '14 at 14:49
@Mr.Wizard, Extrusion is the new Thickness for Plot3D. See this. – Simon Woods Aug 6 '14 at 19:32
@LiamWilliam, to avoid changing the aspect ratio of the texture you will need to use AspectRatio -> Automatic and TextureCoordinateScaling -> False along with a texture coordinate function like (0.08 {#1, ar #2} &) where ar is the aspect ratio of the texture image ar = Divide@@ImageDimensions[image] and the numerical factor is adjusted to give the desired number of tilings. – Simon Woods Aug 7 '14 at 21:28

Using Simon Woods' shadow package this is easy:

text = Style[HoldForm@Sum[x^2, {x, 0, 10}], 100, Bold];
image = ImageResize[Import["http://creativity103.com/collections/Graphic/rainbowbars.jpg"], ImageDimensions@Rasterize@text];


In the example above I stretched the background to fit the image of the equation. If you want to tile the background instead you can create your tiled background of the appropriate size using the following function:

createBackground[size_, pattern_] := Module[{scalex, scaley, vertices},
{scalex, scaley} = size/ImageDimensions[pattern];
vertices = {{0, 0}, {scalex, 0}, {scalex, scaley}, {0, scaley}};
Graphics[{
Texture[pattern],
Polygon[
vertices,
VertexTextureCoordinates -> vertices
]
}, ImageSize -> size, PlotRangePadding -> 0]
]


size is the total size of the background and pattern is the tile image. Typically you would set the size of the background to the size of the bounding box of the text that you're trying fill with the texture.

-
It didn't even occur to me to use shadow :-) – Simon Woods Aug 6 '14 at 14:43