用相对坐标裁剪图像

用相对坐标裁剪图像

我想用相对坐标剪辑图像。

参见以下示例:

\documentclass{standalone}
\usepackage{tikz}
\tikzset{mynode/.style={draw,solid,circle,inner sep=1pt}}

\usepackage{etoolbox}
\newbool{DEBUG}
\booltrue{DEBUG}
%\boolfalse{DEBUG}

\begin{document}
\def\infilename{tiger.pdf}
\begin{tikzpicture}[>=latex]
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=5cm]{\infilename}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \clip (0.4,0.6) rectangle (0.5,0.7);
        \ifbool{DEBUG}{\small
            \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1.001,1.001);
            \foreach \x in {1,...,9} { \node [anchor=north] at (\x/10,0) {\x};}
            \foreach \y in {1,...,9} { \node [anchor=east] at (0,\y/10) {\y};}
        };
    \end{scope}
\end{tikzpicture}

\end{document}

但似乎只剪切了辅助线,而不是原始图片。我喜欢相对坐标,因为它更容易找到我想要的准确位置。

可以修复吗?在此示例中,我只想提取矩形区域 (0.4,0.6) 到 (0.5,0.7)。假设完整图像是 (0,0) 到 (1,1)。

在此处输入图片描述

答案1

问题是,您需要知道图像有多大,然后才能应用相对剪辑。通常的方法是使用保存框或使用 \phantom。

使用保存箱:

\tikzset{mynode/.style={draw,solid,circle,inner sep=1pt}}
\usetikzlibrary{calc}

\usepackage{etoolbox}
\newbool{DEBUG}
\booltrue{DEBUG}
%\boolfalse{DEBUG}

\newsavebox{\tempbox}

\usepackage{mwe}% for example images

\begin{document}
\def\infilename{example-image}%
\savebox{\tempbox}{\includegraphics[width=5cm]{\infilename}}%
\fbox{
\begin{tikzpicture}[>=latex]
  \path (0,0) (\wd\tempbox,\ht\tempbox);% set bounding box
    \begin{scope}[x={\wd\tempbox},y={\ht\tempbox}]
        \clip (0.4,0.6) rectangle (0.5,0.7);
        \node[anchor=south west,inner sep=0] {\usebox\tempbox};
        \ifbool{DEBUG}{\small
            \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1.001,1.001);
            \foreach \x in {1,...,9} { \node [anchor=north] at (\x/10,0) {\x};}
            \foreach \y in {1,...,9} { \node [anchor=east] at (0,\y/10) {\y};}
        };
    \end{scope}
\end{tikzpicture}}

\end{document}

使用 \phantom:

\documentclass{standalone}
\usepackage{tikz}
\tikzset{mynode/.style={draw,solid,circle,inner sep=1pt}}
\usetikzlibrary{calc}

\usepackage{etoolbox}
\newbool{DEBUG}
\booltrue{DEBUG}
%\boolfalse{DEBUG}

\usepackage{mwe}% for example images

\begin{document}
\def\infilename{example-image}
\fbox{
\begin{tikzpicture}[>=latex]
    \node[anchor=south west,inner sep=0] (image) {\phantom{\includegraphics[width=5cm]{\infilename}}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \clip (0.4,0.6) rectangle (0.5,0.7);
        \node[anchor=south west,inner sep=0] {\includegraphics[width=5cm]{\infilename}};
        \ifbool{DEBUG}{\small
            \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1.001,1.001);
            \foreach \x in {1,...,9} { \node [anchor=north] at (\x/10,0) {\x};}
            \foreach \y in {1,...,9} { \node [anchor=east] at (0,\y/10) {\y};}
        };
    \end{scope}
\end{tikzpicture}}

\end{document}

剪裁图像

答案2

我认为将 savebox 和 phantom 结合起来更好。

结果如下:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{etoolbox}

\tikzset{mynode/.style={draw,solid,circle,inner sep=1pt}}

\usepackage{etoolbox}
\newbool{DEBUG}
\booltrue{DEBUG}
%\boolfalse{DEBUG}

\begin{document}
\def\infilename{tiger.pdf}
\newsavebox{\graphbox}
\savebox{\graphbox}{\includegraphics[width=5cm]{\infilename}}
\begin{tikzpicture}[>=latex]
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\phantom{\usebox\graphbox}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \ifbool{DEBUG}{\small
            \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1.001,1.001);
            \foreach \x in {1,...,9} { \node [anchor=north] at (\x/10,0) {\x};}
            \foreach \y in {1,...,9} { \node [anchor=east] at (0,\y/10) {\y};}
        };
        \clip (0.4,0.6) rectangle (0.5,0.7);
        \node[anchor=south west,inner sep=0] {\usebox\graphbox};
    \end{scope}
\end{tikzpicture}

\end{document}

结果图片(附带帮助热线):

在此处输入图片描述

答案3

没有 tikz:

平均能量损失

\documentclass{article}
\usepackage{graphicx,calc}
\def\mygraphic{\includegraphics{tiger}}
\newlength\gh \setlength\gh{\heightof{\mygraphic}}
\newlength\gw \setlength\gw{\widthof{\mygraphic}}
\begin{document}
\includegraphics[scale=0.5,trim= {.4\gw} {.6\gh} {.5\gw} {.3\gh}, clip]{tiger}%
\end{document}

答案4

阅读所有答案并得到以下结果:

  1. 如果打开调试选项,它将打印帮助行。
  2. 如果注释掉调试选项,则只输出剪辑区域。

代码如下:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{calc}

\usepackage{etoolbox}
\newbool{DEBUG}
\booltrue{DEBUG}
%\boolfalse{DEBUG}

\begin{document}
\def\infilename{tiger.pdf}

\newsavebox{\graph}\savebox{\graph}{\includegraphics[]{\infilename}}
\newlength\gh\setlength\gh{\heightof{\usebox\graph}}
\newlength\gw\setlength\gw{\widthof{\usebox\graph}}

\begin{tikzpicture}[>=latex,
  image/.style={anchor=south west,inner sep=0}]
    \ifbool{DEBUG}{
        \node[image] (NI) at (0,0){\phantom{\usebox\graph}};
        \begin{scope}[x={(NI.south east)},y={(NI.north west)}]
            \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1.001,1.001);
            \foreach \x in {1,...,9} { \node [anchor=north] at (\x/10,0) {\x};}
            \foreach \y in {1,...,9} { \node [anchor=east] at (0,\y/10) {\y};}
        \end{scope}
    };
    \clip (0.4\gw,0.6\gh) rectangle (0.5\gw,0.7\gh);
    \node[image] {\usebox\graph};
    %\draw[|<->|,very thick,red!60] 
    %    (0.5\gw,0.5\gh) -- node[pos=0.5, auto] {$600$} ++(0.2\gw,0\gh);
\end{tikzpicture}

\end{document}

调试选项开启时的输出:

在此处输入图片描述

关闭调试选项时的输出:

在此处输入图片描述

相关内容