tikz:剪辑带有圆角和边框的图像

tikz:剪辑带有圆角和边框的图像

我想使图像的角变圆并且周围有边框:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows.blur,fadings}

\tikzset{ 
  photo/.style={ inner sep=1pt,clip,rounded corners=0.5cm }
}

\begin{document}

\begin{tikzpicture}
  \node[photo] at (0,0)
    {\includegraphics[width=110mm]{chipping-sparrow-wiki-commons-1900x855.jpg}};
\end{tikzpicture}

\bigskip

\begin{tikzpicture}
  \node[photo,draw=red!80,thick] at (0,0)
    {\includegraphics[width=110mm]{chipping-sparrow-wiki-commons-1900x855.jpg}};
\end{tikzpicture}

\end{document}

第二个 tikzpicture 给出了一个错误: ! Package tikz Error: Extra options not allowed for clipping path command..

结果如下:(图片由 DickDaniels 提供,CC-BY-SA,摘自 https://www.innonmillcreek.com/blog/2017/01/north-carolina-mountain-birds-chipping-sparrow.html)

我想我可以使用\path\node组合,但我不知道图像大小。

在这种情况下,如何获得带边框的剪辑图像?

答案1

裁剪后的图像是一个节点,可以命名为(name=clipped)。稍后可以像这样绘制一个矩形:

\draw (clipped.south west) rectangle (clipped.north east);

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows.blur,fadings}

\tikzset{ 
  photo/.style={inner sep=1pt,clip,rounded corners=0.5cm }
}

\begin{document}    
\begin{tikzpicture}
  \node[photo] at (0,0)
    {\includegraphics[width=110mm]{chipping-sparrow-wiki-commons-1900x855.jpg}};
\end{tikzpicture}

\bigskip

\begin{tikzpicture}
  \node[photo,name=clipped] at (0,0)
    {\includegraphics[width=110mm]{chipping-sparrow-wiki-commons-1900x855.jpg}};
  \draw[red,thick, rounded corners=0.5cm] (clipped.south west) rectangle (clipped.north east);
\end{tikzpicture}  
\end{document}

答案2

您可以使用以下命令获取图像高度:

\pgfdeclareimage[width=110mm]{image1}{/path/your/image}
\settoheight{\imageheight}{\pgfuseimage{image1}}

之后,我添加了一个具有您所需选项的矩形节点:

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{shadows.blur,fadings}

\tikzset{ 
  photo/.style={ inner sep=1pt,clip,rounded corners=0.5cm }
}
\newlength\imageheight

\begin{document}
\pgfdeclareimage[width=110mm]{image1}{bird.jpg}
\settoheight{\imageheight}{\pgfuseimage{image1}}


\begin{tikzpicture}

  \node[photo] at (0,0)
   {\includegraphics[width=110mm]{bird.jpg}};
\end{tikzpicture}

\bigskip

\begin{tikzpicture}
  \node[photo] at (0,0)
    {\includegraphics[width=110mm]{bird.jpg}};
    \node (rect) at (0,0) [draw,thick,minimum width=110mm,minimum height=\imageheight,draw=red!80,thick,rounded corners=0.5cm] {};
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容