70
\documentclass[demo]{article}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[circle,draw,inner sep=2cm]{\includegraphics{myimage.jpg}};
\end{tikzpicture}
\end{document}

How do I modify the above code so that the included image is adapted to the circle shape and cropped of the exceeding parts?

I want to specify a shape (a circle) and then include an image respecting the given dimension and cropped.

JennyC
  • 739

4 Answers4

83

You can use path picture key.

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\tikz\node[circle,draw,
           text=white,
           path picture={
               \node at (path picture bounding box.center){
                   \includegraphics[width=3.5cm]{frog}
               };
           }]{I'm watching you!};
\end{document}

enter image description here

Image is writelatex's frog.jpg

percusse
  • 157,807
47

Here is another frog solution which also uses path picture beneath. If you don't mind loading tcolorbox for this purpose, you can use its fill overzoom image option for TikZ. This option takes a picture file name as parameter (here frog.jpg again) and scales this image to fit into (or better: over) the shape:

\documentclass[border=5mm]{standalone}
\usepackage[skins]{tcolorbox}
\begin{document}
\begin{tikzpicture}
  % one picture
  \node[circle,draw,inner sep=2cm,fill overzoom image=frog] (A) {};

  % some more
  \foreach \w in {1,2,...,6}
  {
    \path (A) (\the\numexpr\w*60\relax:\the\numexpr 3+\w/2\relax cm)
      node[circle,draw,inner sep=\the\numexpr\w*2\relax mm,fill overzoom image=frog] (B) {};
    \draw[very thick,red,->] (A)--(B);
  }
\end{tikzpicture}
\end{document}

enter image description here

As a bonus, here are some more frogs put into different shapes (zoomed automatically):

\documentclass[border=5mm]{standalone}
\usepackage[skins]{tcolorbox}
\begin{document}
\begin{tikzpicture}[radius=1cm,delta angle=180]
\path[draw,thick,fill overzoom image=frog]
  (0,0) arc [start angle=-90] -- ++(-2,0) arc [start angle=90] -- cycle;
\path[draw,thick,fill overzoom image=frog]
  (3.5,2) arc [start angle=0] -- ++(0,-2) arc [start angle=180] -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

And, finally, just one frog which fills a path consisting of two separate parts:

\documentclass[border=5mm]{standalone}
\usepackage[skins]{tcolorbox}
\begin{document}
\begin{tikzpicture}[radius=1cm,delta angle=180]
\path[draw,thick,fill overzoom image=frog]
  (0,4) arc [start angle=-90] -- ++(-2,0) arc [start angle=90] -- cycle
  (3.5,6) arc [start angle=0] -- ++(0,-2) arc [start angle=180] -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

18

Since I could not find writelatex's frog.jpg, I'm using the Masked tree frog head from Charlesjsharp on Wikimedia, which is 851x567 px big.

I like to use clip, since one can then clip the image with lots of different shapes. In this case I clip the image with a circle of half the radius of the shorter length of the image, centered onto the middle of the image.

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}  
\PreviewEnvironment{tikzpicture}

\newcommand{\imsize}{\linewidth}
\newlength\imagewidth
\newlength\imagescale

\begin{document}

\renewcommand{\imsize}{0.618\linewidth}
\pgfmathsetlength{\imagewidth}{\textwidth}%
\pgfmathsetlength{\imagescale}{\imagewidth/851}%

\begin{tikzpicture}[x=\imagescale,y=-\imagescale]
    \clip (851/2, 567/2) circle (567/2);
    \node[anchor=north west, inner sep=0pt, outer sep=0pt] at (0,0) {\includegraphics[width=\imagewidth]{Masked_tree_frog_head}};
\end{tikzpicture}

\end{document}

enter image description here

Habi
  • 7,694
  • Is there any way to enable anti-aliasing on this solution? – Unapiedra Jul 29 '14 at 11:56
  • 1
    @Unapiedra: Tha anti-aliasing is most probably an artefact from the conversion from pdf to (tiny) jpg. The original file is cropped by a neat circle, as you can see in this screenshot showing the zoomed in document. – Habi Jul 29 '14 at 13:24
  • You have a lot of seemingly magic numbers. I'd like to see this with more documentation or an explanation of those numbers. – jvriesem Jan 05 '16 at 21:59
  • 1
    @jvriesem The numbers are not magic at all. The image I used is 851x567 px big. I'm centering the circle with a radius of half of the smaller length on the middle of the image (851/2 and 567/2), The \imsize of 0.618 is 1/phi, from the Golden ratio, which would not be necessary for this example where only the image is shown and there's no text to relate the image width to. – Habi Jan 06 '16 at 09:52
11

A bit enhanced circular clipper with PSTricks.

\documentclass[pstricks,border=12pt,dvipsnames]{standalone}
\usepackage{graphicx}
\usepackage{multido}
\newsavebox\IBox
%\savebox\IBox{\includegraphics[scale=3]{example-grid-100x100pt}}
\savebox\IBox{\includegraphics[width=6cm]{golum}}

\def\HColumns{10} %half columns
\def\HRows{10} %half rows

\psset
{
    xunit=0.5\dimexpr\wd\IBox/\HColumns,
    yunit=0.5\dimexpr\ht\IBox/\HRows,
    runit=\psxunit,
}

\begin{document}
\begin{pspicture}[showgrid=false](-\HColumns,-\HRows)(\HColumns,\HRows)
    \psline[linecolor=red,linewidth=12pt](-\HColumns,-\HRows)(\HColumns,\HRows)
    \psclip
    {
        \pscustom[linestyle=none,linewidth=0]
        {
            \code{ /clip /eoclip load def }
            \multido{\n=2+2}{5}{\pscircle(0,0){\n}}
        }
    }
    \rput(0,0){\usebox\IBox}
    \endpsclip
\end{pspicture}
\end{document}

enter image description here