Place a node at a given pixel value within an image

Place a node at a given pixel value within an image

I have and pdf image that is 4903 × 314 pixels. How can I place a node in the image at (3903, 251)? The image itself will be scaled to fit the page width.

답변1

EDIT: Added second macro for multiple nodes

Description of \PlaceInImageAt: The first arguments in (#1,#2) should be the pixel size of the image, the second (#4,#5) should be the pixel at which you want to place your node. #3 should be the image to use, the optional [#6] are the options you can give to \node and finally #7 are the contents of that node. The size argument (#1,#2) is optional. If it isn't specified, (100,100) is used. You can scale the image to whichever sizes you want (even unproportional) by altering the \includegraphics command in #3.

Description of \MultiPlaceInImageAt: First three arguments are identical. The optional [#4] takes options which should be passed to each node. The last argument is a comma separated list. Each node should be enclosed in braces with the following scheme {<x>,<y>=[<options>]<content>}.

\documentclass[]{article}

\usepackage{tikz,graphicx}
\makeatletter
\newbox\PlaceInImageAtbox
\newcommand\PlaceInImageAt{}% just to check whether it is already defined
\def\PlaceInImageAt
  {%
    \@ifnextchar(
      {\PlaceInImageAt@i}
      {\PlaceInImageAt@i(100,100)}
  }
\def\PlaceInImageAt@i(#1,#2)#3(#4,#5)%
  {%
    \@ifnextchar[
      {\PlaceInImageAt@ii{#1}{#2}{#3}{#4}{#5}}
      {\PlaceInImageAt@ii{#1}{#2}{#3}{#4}{#5}[]}%
  }
\def\PlaceInImageAt@ii#1#2#3#4#5[#6]#7%
  {%
    \setbox\PlaceInImageAtbox\hbox{#3}%
    \begin{tikzpicture}
      \draw[use as bounding box] node at (0,0) {\usebox\PlaceInImageAtbox};
      \pgfmathsetmacro\PlaceInImageAtx{(#4 - #1/2) * (\wd\PlaceInImageAtbox/#1)}
      \pgfmathsetmacro\PlaceInImageAty{(#5 - #2/2) * (\ht\PlaceInImageAtbox/#2)}
      \node[#6] at (\PlaceInImageAtx pt,\PlaceInImageAty pt) {#7};
    \end{tikzpicture}%
  }
\def\MultiPlaceInImageAt
  {%
    \@ifnextchar(
      {\MultiPlaceInImageAt@i}
      {\MultiPlaceInImageAt@i(100,100)}
  }
\def\MultiPlaceInImageAt@i(#1,#2)#3%
  {%
    \@ifnextchar[
      {\MultiPlaceInImageAt@ii{#1}{#2}{#3}}
      {\MultiPlaceInImageAt@ii{#1}{#2}{#3}[]}%
  }
\def\MultiPlaceInImageAt@ii#1#2#3[#4]#5%
  {%
    \setbox\PlaceInImageAtbox\hbox{#3}%
    \begin{tikzpicture}
      \draw[use as bounding box] node at (0,0) {\usebox\PlaceInImageAtbox};
      \@for\arg@MultiPlace:={#5}\do
        {%
          \expandafter
          \MultiPlaceInImageAt@each\arg@MultiPlace\@rgend{#1}{#2}{#4}
        }
    \end{tikzpicture}
  }
\def\MultiPlaceInImageAt@each#1,#2=%
  {%
    \@ifnextchar[
      {\MultiPlaceInImageAt@each@i{#1}{#2}}
      {\MultiPlaceInImageAt@each@i{#1}{#2}[]}%
  }
\def\MultiPlaceInImageAt@each@i#1#2[#3]#4\@rgend#5#6#7%
  {%
    \pgfmathsetmacro\PlaceInImageAtx{(#1 - #5/2) * (\wd\PlaceInImageAtbox/#5)}
    \pgfmathsetmacro\PlaceInImageAty{(#2 - #6/2) * (\ht\PlaceInImageAtbox/#6)}
    \node[#7,#3] at (\PlaceInImageAtx pt, \PlaceInImageAty pt) {#4};
  }
\makeatother

\begin{document}
\PlaceInImageAt(4903,314){\includegraphics{example-image}}
  (3903,251)[draw,circle,red]{foo}

\PlaceInImageAt{\includegraphics[width=5cm,height=2cm]{example-image}}
  (50,50)[draw,circle,red]{foo}

\MultiPlaceInImageAt{\includegraphics[scale=0.5]{example-image-a}}
  [red]% option for each node
  {{20,20=[blue]a},{40,40={b}},{60,60={c}},{80,80={d}}}
\end{document}

enter image description here

답변2

Here is a code that does it if you only scale the graphics to \textwidth (such that the vertical scaling is proportional).

UPDATE: Wrote a little macro for repeated use.

\documentclass{article}
\usepackage{tikz}
\newcommand{\PlaceNode}[5][]{
\pgfmathsetmacro{\myx}{(#2-4903/2)*(\the\textwidth/4903)}
\pgfmathsetmacro{\myy}{(#3-314/2)*(\the\textwidth/4903)}
\node[#1] (#4) at  (\myx pt,\myy pt) {#5};
}
\begin{document}
\begin{tikzpicture}
\node at (0,0){\includegraphics[width=\textwidth]{example-image-a}};

\PlaceNode[draw,circle,red]{3903}{251}{first}{}
\PlaceNode[draw,circle,blue]{3703}{151}{second}{}

\end{tikzpicture}
\end{document}

관련 정보