[Tex/LaTex] Overpic a semitransparent rectangle on graphics

beameroverpic

I know with overpic you can put text on top of an image, as explained in this question..

I need to add text to the image but I need it placed on top of a white semitransparent rectangle so that it can be seen. The idea is explained in the following picture taken from the link above (think of the solid white rectangle as semitransparent, excuse my poor editing skills).

enter image description here

I tried overlaying text inside a transparent colored box as

\begin{overpic}[width=\linewidth]{image.png}
    \put (0, 2.3){ %
        \hspace{-0.1pt}\hspace{-5pt}%
        \pgfsetfillopacity{0.5}%
        \colorbox{white}{%
            \parbox{\linewidth}{\pgfsetfillopacity{1}\hspace{25pt}\color{black} image text  \hspace{10pt}} }}
\end{overpic}       

But the white box surrounding the text surpasses the bonds of the image and I am left with parts of white rectangles on the background (my background is grey). Which is weird cuz both the image, and colorbox are set to linewidth, if I make the width of the colorbox smaller then it's off centered (using \centering doesn't do anything).
enter image description here

There has to be a way to place a semitransparent rectangle within the confinements of the image but I don't know how. Thank you for any help you might have!


MWE:

\documentclass[10pt]{beamer}
\usetheme[progressbar=frametitle]{metropolis}
\usepackage[percent]{overpic}

\begin{document}
\begin{frame}
\hspace{-15pt}
\begin{minipage}{0.251\linewidth}
\vspace{10pt}
\begin{overpic}[width=\linewidth]{example-image-a}
    \put (0, 2.3){ %
        \hspace{-0.1pt}\hspace{-5pt}%
        \pgfsetfillopacity{0.5}%
        \colorbox{white}{%
            \parbox{\linewidth}{\pgfsetfillopacity{1}\hspace{25pt}\color{black} text \hspace{10pt}} }}

    \put (-1.95, 15.5){%
        \hspace{-0.1pt}\hspace{-2.2pt}%
        \pgfsetfillopacity{0.5}%
        \colorbox{white}{%
            \parbox{\linewidth}{\pgfsetfillopacity{1} \color{black}\hspace{1pt} image text } }}
\end{overpic}
\end{minipage}
\end{frame}

\end{document}

Best Answer

overpic seems to use tikz, so advertizing tikz here may be in order. The question how to add something on top of a pic has this nice answer, which may be relevant if you want to see the grid. Otherwise just do

\documentclass[10pt]{beamer}
\usetheme[progressbar=frametitle]{metropolis}
\usepackage{tikz}

\begin{document}
\begin{frame}
\hspace{-15pt}
\begin{minipage}{0.251\linewidth}
\vspace{10pt}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0) 
{\includegraphics[width=\linewidth]{example-image-a}};
\node[fill=white,fill opacity=0.5,anchor=south,text width=\linewidth-2pt,
inner xsep=1pt,inner ysep=3pt,outer sep=0pt,align=center,text opacity=1] at (image.south){text\\ more text};
% from https://tex.stackexchange.com/a/9562/121799
%     \begin{scope}[x={(image.south east)},y={(image.north west)}]
%         \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}; }
%     \end{scope}
\end{tikzpicture}
\end{minipage}
\end{frame}
\end{document}

enter image description here

Another way of making text more visible is to use the contour package.

\documentclass[10pt]{beamer}
\usetheme[progressbar=frametitle]{metropolis}
\usepackage{tikz}
\usepackage{contour}
\contourlength{1pt}
\begin{document}
\begin{frame}
\hspace{-15pt}
\begin{minipage}{0.251\linewidth}
\vspace{10pt}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0) 
{\includegraphics[width=\linewidth]{example-image-a}};
\node[%fill=white,fill opacity=0.5,
anchor=south,text width=\linewidth-2pt,
inner xsep=1pt,inner ysep=3pt,outer sep=0pt,align=center,text opacity=1] at 
(image.south){\contour{white}{text}\\ \contour{white}{more text}};
\end{tikzpicture}
\end{minipage}
\end{frame}
\end{document}

enter image description here

Related Question