17

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

William
  • 7,595
  • 2
  • 22
  • 70
  • 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 06 '14 at 00:58
  • @Pickett Included an image. Basically I'm looking to do something similar to a gradient but with an actual image. – William Aug 06 '14 at 01: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 06 '14 at 01:09
  • @m_goldberg I believe it is fixed. – William Aug 06 '14 at 01:15

3 Answers3

22

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

Simon Woods
  • 84,945
  • 8
  • 175
  • 324
  • 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: http://i.stack.imgur.com/zCMGX.png. What version and OS are you using? – Michael E2 Aug 06 '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 06 '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 06 '14 at 14:49
  • I've got to start using that new-to-me Texture stuff. What's this Extrusion option for Plot3D? – Mr.Wizard Aug 06 '14 at 17:08
  • Any clever approach that makes use of the new Mesh functions gets my vote. – RunnyKine Aug 06 '14 at 17:12
  • 2
    @Mr.Wizard, Extrusion is the new Thickness for Plot3D. See this. – Simon Woods Aug 06 '14 at 19:32
  • I'm not sure if I should open a new question, but ideally I am looking to tile the image. Forgive me but I'm not certain if yours does such because I'm on v8(will be updated momentarily.) – William Aug 06 '14 at 19:42
  • @LiamWilliam, see update. – Simon Woods Aug 06 '14 at 19:59
  • On version 9 but keep receiving RegionPlot::argr: RegionPlot called with 1 argument; 3 arguments are expected. – William Aug 06 '14 at 20:05
  • @LiamWilliam, ah - the MeshRegion stuff is new in version 10. – Simon Woods Aug 06 '14 at 20:10
  • @SimonWoods Upgraded to M10! I'm reading through the new mesh documentation, but I seem to be having issues forcing the tile to work. For example I would like the tiled image not to be stretched in any way, but using your example it appears to be stretched. Is there an easy way to generalize this also to images, for example image = Graphics[{Black,Rectangle[]}]. – William Aug 07 '14 at 20:39
  • 1
    @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 07 '14 at 21:28
10

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
7

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.

C. E.
  • 70,533
  • 6
  • 140
  • 264