17
$\begingroup$

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.

enter image description here

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – C. E.
    Commented Aug 6, 2014 at 0:58
  • $\begingroup$ @Pickett Included an image. Basically I'm looking to do something similar to a gradient but with an actual image. $\endgroup$
    – William
    Commented Aug 6, 2014 at 1:07
  • $\begingroup$ 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. $\endgroup$
    – m_goldberg
    Commented Aug 6, 2014 at 1:09
  • $\begingroup$ @m_goldberg I believe it is fixed. $\endgroup$
    – William
    Commented Aug 6, 2014 at 1:15

3 Answers 3

10
$\begingroup$

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

enter image description here


With Pickett's extension for outlining:

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

rainbow ~ImageSubtract~ img2 ~ImageAdd~ img3

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ 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, $\endgroup$
    – m_goldberg
    Commented Aug 6, 2014 at 1:52
  • $\begingroup$ @m_goldberg Tomorrow $\endgroup$
    – Mr.Wizard
    Commented Aug 6, 2014 at 5:37
  • 3
    $\begingroup$ Here's one way (image). $\endgroup$
    – C. E.
    Commented Aug 6, 2014 at 10:03
  • 1
    $\begingroup$ @Pickett Thanks! I'll include it in my answer, with credit of course. $\endgroup$
    – Mr.Wizard
    Commented Aug 6, 2014 at 16:56
22
$\begingroup$

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]]

enter image description here

Or in 3D...

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

enter image description here

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} &)]

enter image description here

$\endgroup$
12
  • $\begingroup$ 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.sstatic.net/zCMGX.png. What version and OS are you using? $\endgroup$
    – Michael E2
    Commented Aug 6, 2014 at 14:40
  • $\begingroup$ @MichaelE2, I'm using Windows 7 (64 bit) and version 10. It worked fine with version 9 too. $\endgroup$ Commented Aug 6, 2014 at 14:45
  • $\begingroup$ 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. $\endgroup$
    – Michael E2
    Commented Aug 6, 2014 at 14:49
  • 2
    $\begingroup$ @Mr.Wizard, Extrusion is the new Thickness for Plot3D. See this. $\endgroup$ Commented Aug 6, 2014 at 19:32
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Aug 7, 2014 at 21:28
7
$\begingroup$

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];
shadow[image, text]

Example

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.

$\endgroup$
1
  • 3
    $\begingroup$ It didn't even occur to me to use shadow :-) $\endgroup$ Commented Aug 6, 2014 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.