9
$\begingroup$

The image below contains various ( lightblue filled ) circles ( made with the Disk function ), I want to fill them with an external image ( for example of a face ) such that each circle contains the entire image as if it was cut out in circle shape ( the smaller circles cut out from an image where the size is reduced. )

How can this be done in Mathematica?

EDIT: Image below now contains the result after the accepted answer has been implemented.

Target image

$\endgroup$
  • 2
    $\begingroup$ I suggest that including code for your graphic may improve the speed and/or quality of the answers you receive. $\endgroup$ – Mr.Wizard Jul 7 '15 at 7:06
  • 1
    $\begingroup$ Possible duplicates and/or related: (1332), (3723), (7657) $\endgroup$ – Mr.Wizard Jul 7 '15 at 7:07
  • $\begingroup$ @Mr.Wizard An image in Disk[]//Graphics would suffice. Sadly 1332, 3723, 7657 dont solve the problem. Imagine importing the image of a face. I will edit this in the question. I worked with Textures before which works but I would not know how to do this with Circle or Disk. $\endgroup$ – nilo de roock Jul 7 '15 at 8:49
  • $\begingroup$ @Mr.Wizard I thought about overlaying and transparent colors but I am stuck at that route. I feel like I am missing the obvious. $\endgroup$ – nilo de roock Jul 7 '15 at 9:02
9
$\begingroup$

You can use your Disk (or any grayscale image) as an alpha channel:

img = ExampleData[{"TestImage", "F16"}];

disk = Graphics[Disk[]];

diskImg = 
  ColorConvert[Rasterize[disk, ImageSize -> ImageDimensions[img]], 
   "Grayscale"];

circleImg = ColorCombine[{img, ColorNegate@diskImg}, "RGB"]

enter image description here

Then you can use Inset to place it like other graphics primitives:

Graphics[{
  Table[
   Inset[circleImg, {Cos[i*10 \[Degree]], 
      Sin[i*10 \[Degree]]}*50, {256, 256}, 5 + i*1], {i, 10}]}]

enter image description here

$\endgroup$
  • $\begingroup$ Everytime I think that I -somewhat- master Mma programming I am blown out by really great programming and new ideas ( for me ) in the answers I receive on this site. - Thanks. $\endgroup$ – nilo de roock Jul 7 '15 at 15:32
9
$\begingroup$

Disk is less convenient. You can define your own disk as a polygonial approximation (here with a default value of a 50-points polygon):

myDisk[n_: 50] := Module[{ps},
   ps = Table[{Cos[t], Sin[t]}, {t, Range[0, 2 \[Pi], 2 \[Pi]/n]}]; 
   Polygon[ps, VertexTextureCoordinates -> Rescale[ps]]
   ];

Edit -- changed the picture

Then everything works:

jeb=Import["https://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Jebcropped.jpg/187px-Jebcropped.jpg"];
Graphics[{Texture[jeb], myDisk[]}]
Graphics[{Texture[jeb], myDisk[3]}]

enter image description here

$\endgroup$
  • 2
    $\begingroup$ Is it really necessary to use Lenna? We're now in 2015, friend. We don't need chauvinistic pin-up test images to prove a point about Mathematica. Can't you pick something that is welcoming to people of both mainstream genders? $\endgroup$ – Lightness Races with Monica Jul 7 '15 at 12:28
  • 2
    $\begingroup$ @LightnessRacesinOrbit Point well taken. I changed the picture. $\endgroup$ – yohbs Jul 7 '15 at 12:34
  • 3
    $\begingroup$ @LightnessRacesinOrbit you are welcome to comment here $\endgroup$ – yohbs Jul 7 '15 at 13:44
  • 1
    $\begingroup$ You can also use the new-in-10.1 CirclePoints. $\endgroup$ – Szabolcs Jul 7 '15 at 14:22
  • 1
    $\begingroup$ It would have been hilarious if you had used your own gravatar... $\endgroup$ – Jens Jul 7 '15 at 21:14
2
$\begingroup$

This is not really anywhere near a reasonable answer but perhaps it will motivate better answers. I have upvoted the Texture approach by use of polygons.

img = (* pick your desired graphics*)

g = Graphics[{}, ImageSize -> {400, 400}, Background -> Black];
Manipulate[
 Row[{ic = ImageCompose[g, img, Scaled[{x, y}]], 
   ia = ImageAdd[ic, 
     msk = Graphics[Disk[{0, 0}, r], PlotRange -> Table[{-5, 5}, {2}],
        ImageSize -> {400, 400}]], 
   ImageAdd[ic, 
    Rasterize[
     Plot[x, {x, -5, 5}, GridLines -> Automatic, 
      GridLinesStyle -> Red, AspectRatio -> Automatic,
      ImageSize -> {400, 400}, 
      Epilog -> {EdgeForm[{Orange, Thickness[0.02]}], 
        Disk[{0, 0}, r]}]]]}], {x, 0, 1}, {y, 0, 1}, {r, 0.1, 3}]

I just picked a picture of Apollo 11 crew so adapt to whatever image dimensions of graphic and figure that is being overlayed. There are obvious issues with image adding of color images but in the event this can be framed out or as mentioned improved I present.

enter image description here

Again just an idea that I do not have time (or expertise) to refine at present but perhaps OP or others will find helpful.

$\endgroup$
  • $\begingroup$ I like the Apollo 11 angle, I better not explain why. ;-) $\endgroup$ – nilo de roock Jul 7 '15 at 15:26
  • $\begingroup$ Not an easy task which answer to give the accept. I upvoted all answers. $\endgroup$ – nilo de roock Jul 7 '15 at 15:33
  • $\begingroup$ I get the following error message : "The number of channels must be equal or one of them must be a single-channel image and the other a multichannel image." - Fyi, I use Mma 10 on Windows and am using an image from ExampleData in img. $\endgroup$ – nilo de roock Jul 7 '15 at 17:52
  • $\begingroup$ @ndroock1 will look at when I get chance...which image?...may be days away $\endgroup$ – ubpdqn Jul 7 '15 at 17:55
  • $\begingroup$ It is ok. I fixed it otherwise. $\endgroup$ – nilo de roock Jul 7 '15 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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