7
$\begingroup$

I have an image of a certain size.

img = Image[source, ImageSize -> escala];

I want to add a grid with a scale (0 to maxX from left to right and 0 to maxY from top to bottom). The problem is that GridLines is an option for Graphics, not for Image.

How can I overlay grid lines on my image?

$\endgroup$

2 Answers 2

10
$\begingroup$
img = ExampleData[{"TestImage", "Lena"}];

grid = Graphics[{}, GridLines -> Automatic, PlotRangePadding -> None, 
  GridLinesStyle -> Directive[Red, Thick], ImageSize -> ImageDimensions@img];

Overlay[{img, grid}]

enter image description here


For arbitrary numbers n+1 and m+1 of equidistant gridlines with automatic dependence on the ImageDimensions of img (which does not have to be a square either):

n = 13;
m = 7;
sub1 = Subdivide[-1, 1, n];
sub2 = Subdivide[-1, 1, m];

grid = Graphics[{}, GridLines -> {sub1, sub2}, 
  PlotRangePadding -> None, GridLinesStyle -> Directive[Red, Thick], 
  ImageSize -> ImageDimensions@img];

Overlay[{img, grid}]

enter image description here

$\endgroup$
6
  • $\begingroup$ Thank you so much! it was very useful and clear. Now i can continue by my own. $\endgroup$ Nov 26, 2016 at 0:28
  • $\begingroup$ I did, but i have a problem. It only createas a 4x4 grid. I tried to add more lines but i couldn't. $\endgroup$ Nov 26, 2016 at 0:41
  • $\begingroup$ I need more "density" of lines and a way to control it. The image have a imagesize=escale. I have to control something like "a line each escale/k" $\endgroup$ Nov 26, 2016 at 0:47
  • $\begingroup$ @ShiroIgnis See the updated answer. $\endgroup$
    – corey979
    Nov 26, 2016 at 1:07
  • $\begingroup$ All right! i tested it and it was exactly as i want. $\endgroup$ Nov 26, 2016 at 1:37
13
+100
$\begingroup$

You can use Show with gridlines specs combined with Method -> {"GridLinesInFront" -> True}:

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

Show[img, GridLines -> {Range[0, #, #/10], Range[0, #2, #2/5]} & @@ 
  ImageDimensions[img], GridLinesStyle -> Directive[Thick, Red], 
 Method -> {"GridLinesInFront" -> True}]

Mathematica graphics

You can also use PlotStyle->Texture[img] with, say, RegionPlot:

RegionPlot[0 <= x <= 1 && 0 <= y <= 1, {x, 0, 1}, {y, 0, 1}, 
 PlotStyle -> Texture[img], PlotRangePadding->0,
 GridLines -> {Range[0, 1, .1], Range[0, 1, .2]}, 
 GridLinesStyle -> Directive[Thick, Red], Frame -> False, 
 Method -> { "GridLinesInFront" -> True}]

Mathematica graphics

Finally, you can use Mesh instead of GridLines:

RegionPlot[0 <= x <= 1 && 0 <= y <= 1, {x, 0, 1}, {y, 0, 1}, 
 PlotStyle -> Texture[img], Mesh -> {9, 4}, 
 MeshStyle -> Directive[Thick, Red], Frame -> False]

Mathematica graphics

$\endgroup$
0

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.