tikz-pgf highlighting – How to Highlight Part of an Image

highlightingtikz-pgf

I want to highlight part of an image in beamer by drawing a box around it and setting the rest of the image to a dim setting. I do not know how to decrease the brightness of the rest of the picture. Below is my partial attempt, which almost does the job.

But I am unable to decrease the brightness of the remaining picture outside the small red rectangle at the left bottom. Any help would be greatly appreciated.

\documentclass{beamer}
\usetheme{Copenhagen}
\usecolortheme{whale}
\useinnertheme{rounded}

\usepackage{tikz}

\newcounter{boxes}

\newcommand\ordbox[2]{%
  \stepcounter{boxes}
  \tikz[remember picture,overlay]{
  \node[rectangle,rounded corners,line width=2pt,draw=red,fill=pink,text height=10pt,text depth=3pt,align=left,draw opacity = 0.75, fill opacity=.75,text opacity=1] (box-\theboxes) at #2 {#1};}
}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay] \node (#1) {};}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[scale=0.2]{../images/H1D_level_5.pdf}};
\end{tikzpicture}
\end{frame}
\begin{frame}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[scale=0.2]{../images/H1D_level_5.pdf}};
\draw[red,ultra thick] (0,0) rectangle (0.5,0.5);
\end{tikzpicture}
\end{frame}

\end{document}

The code above produces the outputs below.
enter image description here
enter image description here

Best Answer

If you name the node in the second slide you can use a fill=<color> with fill opacity=<num>. By naming the node we can easily access the coordinates of the vertices so that we can traverse the region we want to fill:

\draw[red,ultra thick] (0,0) rectangle (0.5,0.5);
\fill [draw=none, fill=white, fill opacity=0.3] 
    (0,0.5) -- 
    (A.north west) -- (A.north east) -- (A.south east) -- 
    (0.5,0) -- (0.5,0.5) -- cycle;

Using a modified version showing those two images side by side to see the effect:

enter image description here

Notes:

  • To get the images to align I shifted the drawing of the box by 0.5\pgflinewidth. So if you notice that your beamer image shifts from slide to slide, you will need to apply this shift as well.
  • Even with this adjustment, the second image appears to be off slightly and I don't know why. So perhaps another tweak is required.

Code:

\RequirePackage[demo]{graphicx}

\documentclass{beamer}

\usetheme{Copenhagen}
\usecolortheme{whale}
\useinnertheme{rounded}

\usepackage{tikz}

\newcounter{boxes}

\newcommand\ordbox[2]{%
  \stepcounter{boxes}
  \tikz[remember picture,overlay]{
  \node[rectangle,rounded corners,line width=2pt,draw=red,fill=pink,text height=10pt,text depth=3pt,align=left,draw opacity = 0.75, fill opacity=.75,text opacity=1] (box-\theboxes) at #2 {#1};}
}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay] \node (#1) {};}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[scale=0.2]{../images/H1D_level_5.pdf}};
\end{tikzpicture}
\end{frame}
\begin{frame}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (A) at (0,0) {\includegraphics[scale=0.2]{../images/H1D_level_5.pdf}};
\draw[red,ultra thick] (0,0) rectangle (0.5,0.5);
\fill [draw=none, fill=white, fill opacity=0.3] (0,0.5) -- (A.north west) -- (A.north east) -- (A.south east) -- (0.5,0) -- (0.5,0.5) -- cycle;
\end{tikzpicture}
\end{frame}

\end{document}

Code:

Modified to generate the image above.

\documentclass[border=2pt]{standalone}

\usepackage{tikz}

\newcommand\tikzmark[1]{\tikz[remember picture,overlay] \node (#1) {};}
  
 \newcommand*{\Size}{2.5}%

\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[scale=0.1]{./images/EiffelWide.jpg}};
\end{tikzpicture}
%
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (A) at (0,0) {\includegraphics[scale=0.1]{./images/EiffelWide.jpg}};
    
    \draw[red,ultra thick] (0.5\pgflinewidth,0.5\pgflinewidth) rectangle (\Size,\Size);
    
    \fill [draw=none, fill=white, fill opacity=0.4] 
        (\pgflinewidth,\Size) -- 
        (A.north west) -- (A.north east) -- (A.south east) -- 
        (\Size,\pgflinewidth) -- (\Size,\Size) -- cycle;
\end{tikzpicture}
\end{document}