7

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?

xzczd
  • 65,995
  • 9
  • 163
  • 468
ra91
  • 307
  • 1
  • 5

2 Answers2

6

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

e.doroskevic
  • 5,959
  • 1
  • 13
  • 32
6

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257