2

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.5\textwidth]{tux.png}}; \draw[red] (img.west) -- (img.east) coordinatepos=0.53 coordinatepos=0.41;

\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.3\textwidth]{tux.png}}; \path[red] (img.west) -- (img.east) coordinatepos=0.53 coordinatepos=0.41;

\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.5\textwidth]{tux.png}}; \draw[red] (img.west) -- (img.east) coordinatepos=0.53 coordinatepos=0.41;

\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.

rpapa
  • 12,350

1 Answers1

3
\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).

SebGlav
  • 19,186
  • I think that the OP wants the number of steps, which is set to 10 in your example, stored in pgf keys. –  Mar 22 '21 at 01:04