7
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ You can use Texture[], will leave example below in a second $\endgroup$ Commented Dec 26, 2015 at 2:58

2 Answers 2

6
$\begingroup$

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:

example 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:

Texture
VertexTextureCoordinates

$\endgroup$
1
  • $\begingroup$ This did the trick, thanks! $\endgroup$
    – ra91
    Commented Dec 26, 2015 at 5:53
6
$\begingroup$

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]

lena

$\endgroup$

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.