Skip to main content

graphics - Image with grid lines


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?



Answer



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


Comments