TransWikia.com

Grid shows next to image, not on actual image, and ImageDimensions are wrong. What can I do?

Mathematica Asked by Marla on June 10, 2021

I am still using Mathematica 10.3 and I have the following problem:

My code is as follows (based on what I found in a related post, see below), but I used another example image. So here is my code:

 img = ExampleData[{"AerialImage", "Pentagon"}];

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

 Overlay[{img, grid}];

The output is completely off — the grid is much larger than the image. I also tried with an actual image I am interested in, and that gave me the same problem. It does work fine with test image "Lena", but not with any other image.

However, if I check ImageDimensions[img] and ImageDimensions[grid], I do get the same output. So it might just be that for some reasons, all other images except for "Lena"give me a wrong output concerning the ImageDimensions, which is why the grid might be messed up.

Can anyone help with this? What can I do?

Thanks in advance!

P.S. My question is related to and my code is based on the question and answers here: Image with grid lines

One Answer

n = 10; m = 10;

img1 = ImageResize[img, {500, 500}];

id1 = ImageDimensions@img1;

gridlinesx1 = Subdivide[1, id1[[1]], n];
gridlinesy1 = Subdivide[1, id1[[2]], m];

prange1 = Thread[{1, id1}]; 
grid1 = Graphics[{}, GridLines -> {gridlinesx1, gridlinesy1}, 
  PlotRange -> prange1, PlotRangePadding -> None, 
  GridLinesStyle -> Directive[Red, Thick], ImageSize -> id1]; 
Overlay[{Show[img1, ImageSize -> id1], grid1}]

enter image description here

Use m = 5 to get

enter image description here

The same approach works for a non-square image:

n = 10; m = 10;
img2 = ImageResize[img, {500, 300}];

id2 = ImageDimensions@img2;

gridlinesx2 = Subdivide[1, id2[[1]], n];
gridlinesy2 = Subdivide[1, id2[[2]], m];

prange2 = Thread[{1, id2}]; 

grid2 = Graphics[{}, GridLines -> {gridlinesx2, gridlinesy2}, 
  PlotRange -> prange2, PlotRangePadding -> None, 
  GridLinesStyle -> Directive[Red, Thick], ImageSize -> id2];

Overlay[{Show[img2, ImageSize -> id2], grid2}]

enter image description here

and with m = 5:

enter image description here

Answered by kglr on June 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP