I would like to create a tikzpicture where a rectangular part is imported from a raster image (let's say a PNG), and there are other TikZ nodes on top of it. The tricky part is that I need to position the TikZ nodes with reference to pixels in the raster image.
Based on this example, we can make the space (0,0) -- (1,1) span an image. I thought it would then simply be a matter of scaling:
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{mwe}
\begin{document}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0)
{\includegraphics[width=0.9\textwidth]{example-image.jpg}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\begin{scope}[xscale=1/400, yscale=1/300]
\draw[red,ultra thick,rounded corners] (200, 200) rectangle (300, 250);
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
Two problems with this:
- I'd like to avoid hardcoding the image dimensions
- More pressingly, this fails with the following error message:
! Dimension too large.
<recently read> \pgf@xx
l.14 ...ed,ultra thick,rounded corners] (200, 200)
rectangle (300, 250);

