# Filling a rectangle with an image

Say I have some rectangles of different sizes drawn on the screen. How can I fill them all with the same image? Ideally, the image would be resized to completely fill each rectangle. I've been trying to do this by wrapping each rectangle in graphics and adding the image wrapped in an inset in the epilog for each one, something like this

Example:

Show[Graphics[{Rectangle[{0, 0}, {0.5, 0.3}]},
Epilog -> Inset[pic, {0, 0}, {0, 0}]],
Graphics[{Rectangle[{0.5, 0.3}, {0.7, 0.4}]},
Epilog -> Inset[pic, {0, 0}, {0, 0}]]]


but it's not giving me the result I want.

Is there anything like Fill[Rectangle[],pic] that would fill the rectangle with the image pic? I would also be fine with using something other than rectangles. Maybe there's something like a frame I can work with instead and just draw the image into those?

• You can use Texture[], will leave example below in a second – e.doroskevic Dec 26 '15 at 2:58

Code:

(*Sample data*)
data = Table[
ColorData["Rainbow", t] /. RGBColor -> List, {t, 0, 1, 1/100}];

(*Visualize*)
Graphics[{Texture[data],
Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}},
VertexTextureCoordinates -> {{0}, {0}, {1}, {1}}]}]


Output: Note:

In the above picture data could be any picture you wish to use as a Texture[]. Please see references below to find more useful information.

Reference:

• This did the trick, thanks! – ra91 Dec 26 '15 at 5:53

Here is a way to make a Lena-Sierpinski triangle. I'm not very experienced with Mathematica's image processing tools, so it's probably far from optimal, but it might give you a starting point for developing better code.

lena = ExampleData[{"TestImage", "Lena"}];

tripleImage[img_Image] :=
Module[{scaled},
scaled =
ImageTransformation[img, ScalingTransform[{2, 2}],
Background -> Transparent, Masking -> All];
ImageCompose[
ImageCompose[
scaled,
ImageTransformation[scaled, TranslationTransform[{-.5, 0}],
Background -> Transparent, Masking -> All]],
ImageTransformation[scaled, TranslationTransform[{-.25, -.5}],
Background -> Transparent, Masking -> All]]]

Nest[tripleImage, lena, 4] 