TransWikia.com

How to define an adaptive grid or coordinate system on an image?

TeX - LaTeX Asked on June 10, 2021

I often need to draw on an image precisely.
For this, I draw two paths on the image and I position the points proportionally on the image.
This solution allows me to change the size of the image without needing to recalculate the points like in the image below.

documentclass[A4]{article}

usepackage{tikz}

usepackage{graphicx}
usetikzlibrary{calc}

begin{document}

begin{tikzpicture}
node(img){includegraphics[width=0.5textwidth]{tux.png}};
draw[red] (img.west) -- (img.east)
coordinate[pos=0.53](ax)
coordinate[pos=0.41](bx);

draw[red] (img.south) -- (img.north)
coordinate[pos=0.74] (ay)
coordinate[pos=0.74] (by);

draw[red,ultra thick] (ax|-ay) circle (0.2cm);

draw[blue,ultra thick] (bx|-by) circle (0.2cm);
end{tikzpicture}
begin{tikzpicture}
node(img){includegraphics[width=0.3textwidth]{tux.png}};
path[red] (img.west) -- (img.east)
coordinate[pos=0.53](ax)
coordinate[pos=0.41](bx);

path[red] (img.south) -- (img.north)
coordinate[pos=0.74] (ay)
coordinate[pos=0.74] (by);

draw[red,ultra thick] (ax|-ay) circle (0.2cm);

draw[blue,ultra thick] (bx|-by) circle (0.2cm);
end{tikzpicture}
begin{tikzpicture}[rotate=30,transform shape]
node(img){includegraphics[width=0.5textwidth]{tux.png}};
draw[red] (img.west) -- (img.east)
coordinate[pos=0.53](ax)
coordinate[pos=0.41](bx);

draw[red] (img.south) -- (img.north)
coordinate[pos=0.74] (ay)
coordinate[pos=0.74] (by);

draw[red,ultra thick] (ax|-ay) circle (0.2cm);

draw[blue,ultra thick] (bx|-by) circle (0.2cm);
end{tikzpicture}
end{document}

enter image description here

what I would like is an command or an environment

begin {imageScope}[xstep = imageWidth / 10, ystep = imageHeight / 10]
...
...
end{imageScope}

which will allow me to position the different points in a relative way directly on the image with a drawing command in the scope like

draw (x1, y1) - (x2, y2);

where (x1, y1) and (x2, y2) are defined in the coordinate system associated with the image.

One Answer

documentclass[tikz,border=3.14mm]{standalone}

newcommand{imgrid}[1]
    {
    path (#1.south west);
    pgfgetlastxy{xmin}{ymin};
    path (#1.north east);
    pgfgetlastxy{xmax}{ymax};
    pgfmathsetmacro{xstp}{(xmax-xmin)/10}
    pgfmathsetmacro{ystp}{(ymax-ymin)/10}
    draw[gray] (xmin,ymin) grid[xstep=xstp pt,ystep=ystp pt] (xmax,ymax);
    }

begin{document}

    begin{tikzpicture}
        node (img) {includegraphics[scale=0.5]{pingoo.jpg}};
        imgrid{img};
    end{tikzpicture}
    
end{document}

grid on image

Of course, as Rmano suggested, you can nest your image into a scope and draw a grid inside this scope. That'll work perfectly. But you asked for a command that does the same, so here it is. Just needed to calculate coordinates of your left lower and your right higher point of the image, then divide the distance in whatever sections you want (here 10).

Correct answer by SebGlav 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