3

I have an image (782x512pixels): enter image description here

I would like to overlay a coordinate system (with a grid) where the bottom left hand corner is (0,0) the bottom right is (782,0) top right (782,512) and the top left hand corner (0, 512).

enter image description here

Ideally the code should be able to create a grid based on any size image (x,y) I place.

The output should be with an appropriate superimposed grid over the image with vertical and horizontal lines).

I tried looking at Drawing a grid on array of images, but didn't understand how it was done, and I also want it to be for one image and not an array.

3kstc
  • 931
  • 6
  • 22

2 Answers2

3

One way is using overpic with the abs option, but probably you may want use percent and forget about the original or final image size in pixels. Once you find the right coordinates for the overlay, set the grid option to false.

mwe

\documentclass[twocolumn]{article}
\usepackage{overpic}
\usepackage[margin=1in]{geometry}
\begin{document}

\section*{Grid in percentage}\bigskip

\begin{overpic}[percent,grid=true,tics=20,scale=.5]{example-image-16x10.png}
\put(20,5){\sffamily\bfseries (that is 400 $\times$ 250 pixels)}
\end{overpic}

\section*{Grid with absolute pixels}\bigskip

\begin{overpic}[abs,grid=true,tics=40,scale=.5]{example-image-16x10.png}
\put(39,10){\sffamily\bfseries (now are 200 $\times$ 125 pixels)}
\end{overpic}

\newpage
\section*{Original image}\bigskip

\includegraphics[scale=.5]{example-image-16x10.png}

($300\times200\,\mathrm{bp} = 400 \times 250\,\mathrm{pixels}$)

\end{document}
Fran
  • 80,769
2

I do not claim that I invented this. Rather, I believe I saw something very similar before, but I really cannot find it. Of course, I will be happy to remove this post if the original will be found. It's all Jake's answer plus a rescaling according to the number of pixels. I add an arrow that points to the coordinate at (300,200) in pixel units. (I named your picture Einstein.jpg.)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) 
    {\includegraphics{Einstein.jpg}};
    \begin{scope}[x={(${(1/782)}*(image.south east)$)},
    y={(${(1/512)}*(image.north west)$)}]
    \coordinate (X) at (300,200);
    \draw[blue,latex-,blue,line width=2pt] (X) -- ++(2cm,1cm);
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

If you want to draw an actual grid, use.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) 
    {\includegraphics{Einstein.jpg}};
    \begin{scope}[x={(${(1/782)}*(image.south east)$)},
    y={(${(1/512)}*(image.north west)$)}]
    \draw (0,0) grid[step=1] (782,512);
    \end{scope}
\end{tikzpicture}
\end{document}