I want to draw a circle with center {x,y} in an image. I use the following code:

img = Image[Graphics[{
   {Red, Thick, Circle[{100, 200}, 20]},
   {Opacity[0, Black],Rectangle[{0, 0}, {500, 500}]}
  ImageSize -> {500, 500}],ColorSpace -> "RGB"]

What I don't understand is that if I print the Image to the screen the position of the circle is not as expected. As you can see {100,200} is not the center of the circle.

enter image description here

How could I print a circle with the right coordinates?

  • $\begingroup$ Can you clarify what is that you want to plot? You have 2 circles in your image, but only one in the function. also, What do you mean by plot to screen? My WM plots in the correct coordinates with reference to the Rectangle[] $\endgroup$ – Jose Enrique Calderon Feb 6 '19 at 15:00
  • 1
    $\begingroup$ Interesting, it seems to be because of the Image[] command, I get the same result. If I instead put it as an epilog in a plot, it is correct. Try e.g. Plot[2x,{x,0,600},ImageSize->1000,GridLines->{{100},{200}},Epilog->{{Red, Thick, Circle[{100, 200}, 20]}, {Opacity[0.1, Black],Rectangle[{0, 0}, {500, 500}]}}] $\endgroup$ – a20 Feb 6 '19 at 15:10
  • $\begingroup$ @JoseECalderon the extra red circle comes from the coordinates tool, it is just a marker where you clicked with the mouse in order to save the coordinates. The issue is that the coordinates tool does not give the expected coordinates from how the image was defined. $\endgroup$ – a20 Feb 6 '19 at 15:11
  • $\begingroup$ @JoseECalderon As bjorn mentioned the small circle origniates from the Coordinates Tool. I just wanted to show that the coordinates of the center are not {100,200} $\endgroup$ – RMMA Feb 6 '19 at 15:20
  • $\begingroup$ @bjorn Thx for your effort. For me it would be important that the center is at the right pixel. I want to overlay the circle with another image which includes some points I want to mark automatically $\endgroup$ – RMMA Feb 6 '19 at 15:21

Your issue is that Mathematica by default creates a white margin around graphics objects (which you couldn't see because you had the Opacity of the black square set to 0). To remove it, you need to use the command 'PlotRangePadding->0'

img = Image[Graphics[{
  {Red, Thick, Circle[{100, 200}, 20]},
  {Opacity[0.1, Black], Rectangle[{0, 0}, {500, 500}]}
 , PlotRangePadding -> 0
], ColorSpace -> "RGB", ImageSize -> {500, 500}]

enter image description here

  • $\begingroup$ thank you. The ImageSize including the margin is 500x500. So Mathematica does some resizing on the Rectangle[]. If I Import an Image there is no margin. That's why the overlaying didn't work as expected. $\endgroup$ – RMMA Feb 6 '19 at 16: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.