69
\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.

1

4 Answers 4

82

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

5
  • 1
    ooh dat frog! :) Jul 28, 2014 at 13:27
  • 13
    I see a latex-memes package incoming.
    – Ingo
    Jul 29, 2014 at 7:02
  • 1
    Perfect choice of example picture and text.
    – Raphael
    Jul 29, 2014 at 15:50
  • This approach requires me to put some text inside the circle to make it visible. How can I get the full size image without any text?
    – nimcap
    Feb 16, 2018 at 7:35
  • 3
    @nimcap set the minimum width, minimum heigth or minimum size keys to enlarge the node without text
    – percusse
    Feb 16, 2018 at 7:37
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

1
  • 1
    The tcolorbox package is amazing. Thanks a lot!
    – Hansimov
    Aug 22, 2018 at 9:35
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

4
  • Is there any way to enable anti-aliasing on this solution?
    – Unapiedra
    Jul 29, 2014 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, 2014 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 5, 2016 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 6, 2016 at 9: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

3
  • 2
    Note that the small circles are not drawn on the top but they clip the image with even-odd rule. Jul 28, 2014 at 17:26
  • 1
    Do not know why, but I imagine that you were not gonna take the tender frog and sample image.
    – Fran
    Jul 29, 2014 at 15:21
  • 2
    @Fran: I took a mutant of the frog. :-) Jul 29, 2014 at 16:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .