是否可以将图片裁剪成矩形以外的其他形状?

是否可以将图片裁剪成矩形以外的其他形状?

我想将一些图片添加到我注释的现有图片中。为此,我在 tikz 中使用以下命令执行此操作

\node[draw=black,anchor=south west] at (0,0.4){\includegraphics{ex.png}};

请注意,我添加黑色矩形只是为了清晰起见,以便大致了解图形的极限。这在我的最终作品中被删除了。现在,正如你所看到的,这个图看起来很糟糕在此处输入图片描述

我想知道是否可以裁剪这些虚线周围的图形,以便能够看到图片的其余部分。

这是我用来生成该图像的代码:

\documentclass{standalone}
\usepackage{graphicx} %Manage images of different formats
\usepackage{tikz}   % drawing

\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=0.9\textwidth]{NuclideMap2.png}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
    \draw[->] (0.014,0) -- (1,0);
    \draw (1,0) node[anchor= west]{$N$};
    \draw[->] (0,0.02) -- (0,1);
    \draw (0,1) node[anchor=north west]{$Z$};
    \draw[dashed] (0.014,0.02) -- (0.6388,0.95) node[anchor=north west]{$N=Z$}; 
    \node[draw=black,anchor=south west] at (0.01,0.4){\includegraphics[width=0.35\textwidth]{ex.png}};
    \draw[thick,dashed] (0.2,0.35) -- (0.45,0.85);
    \draw[thick,dashed] (0.005,0.45) -- (0.08,0.98);
    \end{scope}
    \end{tikzpicture}
\end{document}

答案1

这里有一个剪辑的例子,可以帮助您理解 dexteritas 答案中的想法(甚至可以不用它?;)

输出

在此处输入图片描述

对比

在此处输入图片描述

代码

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}

\strut\vspace{10cm}

\begin{tikzpicture}[transform canvas={scale=10}]
  \node[blue,anchor=base west,inner sep=1pt]{B};
  %\node[red,anchor=base,scale=2]{A}; % this would hide part of the B
  \begin{scope}
    \clip (0,-1ex) rectangle (-1em,5ex); %try \clip[draw] to see the clipped region
    \node[red,anchor=base,scale=2]{A} ;
  \end{scope}
\end{tikzpicture}
\end{document}

让我们来申请你的案例。

输出

在此处输入图片描述

代码

\documentclass[tikz]{standalone}
\usepackage{graphicx} %Manage images of different formats

\begin{document}

\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=0.9\textwidth]{example-image-a}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \draw[->] (0.014,0) -- (1,0);
        \draw (1,0) node[anchor= west]{$N$};
        \draw[->] (0,0.02) -- (0,1);
        \draw (0,1) node[anchor=north west]{$Z$};
        \draw[dashed] (0.014,0.02) -- (0.6388,0.95) node[anchor=north west]{$N=Z$}; 
        \begin{scope}
          \clip (0.2,0.35) -- (0.45,0.85) -- (0.08,0.98) -- (0.005,0.45) -- cycle; % closed path to clip
          \node[anchor=south west] at (0.01,0.4){\includegraphics[width=0.35\textwidth]{example-image-b}};
        \end{scope}
    \end{scope}
\end{tikzpicture}

\end{document}

答案2

使用这个回答如何通过贝塞尔路径剪辑图像?(链接至杰克在评论中)并插入示例图像,您会得到以下代码。

\documentclass{standalone}
\usepackage{graphicx} %Manage images of different formats
\usepackage{tikz}   % drawing

\newif\ifdeveloppath
\tikzset{/tikz/develop clipping path/.is if=developpath,
  /tikz/develop clipping path=true}

\newcommand{\clippicture}[2]{
    \begin{tikzpicture}
        % Include the image to determine the size and set up the relative coordinate system. Enclose the \includegraphics in \phantom{} once the clipping path has been set up
        \ifdeveloppath
            \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics#1};
        \else
            \node[anchor=south west,inner sep=0] (image) at (0,0) {\phantom{\includegraphics#1}};
        \fi
        \pgfresetboundingbox
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            % Draw grid while developing clipping path
            \ifdeveloppath
                \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
                \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
                \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }
                \draw[red, ultra thick] #2 -- cycle;
            \else
                % Use the path to clip, include the image
                \path[clip] #2 -- cycle;
                \node[anchor=south west,inner sep=0pt] {\includegraphics#1};
            \fi
        \end{scope}
    \end{tikzpicture}
}

\begin{document}

\tikzset{develop clipping path=false}

\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=0.9\textwidth]{example-image-a}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \draw[->] (0.014,0) -- (1,0);
        \draw (1,0) node[anchor= west]{$N$};
        \draw[->] (0,0.02) -- (0,1);
        \draw (0,1) node[anchor=north west]{$Z$};
        \draw[dashed] (0.014,0.02) -- (0.6388,0.95) node[anchor=north west]{$N=Z$}; 
        \node[anchor=south west] at (0.01,0.4){\clippicture{[width=0.35\textwidth]{example-image-b}}{(0.55,0) -- (1,0.9) -- (1,1) -- (0.1,1) -- (0,0.3) -- (0,0)}}; % path to clip
        \draw[thick,dashed] (0.2,0.35) -- (0.45,0.85);
        \draw[thick,dashed] (0.005,0.45) -- (0.08,0.98);
    \end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容