Fade text using an image without affecting background image

graphicstikz-fadingstikz-pgf

I have a background image on top of which I need to show some text. I want to fade the text using an image without affecting the background image. As I understand it, this should be possible given what Section 115.3 "Specifying a Fading" in the TikZ & PGF Manual states:

You create a normal picture, which may even contain text, image, and
shadings. Then, you create a fading based on this picture. For this, the luminosity of each pixel of the picture is analyzed (the brighter the pixel, the higher the luminosity – a black pixel has luminosity 0, a white pixel has luminosity 1, a gray pixel has some intermediate value as does a red pixel). Then, when the fading is used, the luminosity of the pixel determines the opacity of the fading at that position. Positions in the fading where the picture was black will be completely transparent, positions where the picture was white will be completely opaque. Positions that have not been painted at all in the picture are always completely transparent.

Here's an MWE that does not yet do what I want:

\documentclass{article}

\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{tikz}

\begin{document}

\pgfdeclarefading{myfading}{fadpix-jpg-rsz} 

\begin{tikzpicture}[remember picture, overlay]
  \node[inner sep=0pt] at (current page.center) {%
    \includegraphics[scale=0.5]{Utah_Teapot_mr_maya.jpg}
  };%
  \begin{scope}[shift={(current page.center)}]
    \pgfsetfading{myfading}{\pgftransformshift{\pgfpoint{1cm}{1cm}}} % causes text to disappear
    \node[white,text width=11.0cm] at (0.0cm,0.0cm) {\lipsum[1-2]};    
  \end{scope}  
\end{tikzpicture}
\end{document}

I'm using the Utah teapot as a sample background image and lipsum to generate some sample text. The picture fadpix-jpg-rsz looks as follows:

enter image description here

To be clear: I want the text to be faded using this picture, but the background picture (the teapot) to be unaffected. At present, the line with \pgfsetfading causes the entire text to disappear. When I comment it out, the text is visible.

What am I doing wrong? Can what I want even be done?

(Side note: I used the article instead of the standalone class because the latter kept giving me empty two-page documents with a very long second page. When I used the preview option, the document caused Skim to crash and MacOS Preview to show empty files.)

Best Answer

Well at first you actually need to use your fading picture and not only set the text fadpix-jpg-rsz.

At second it looks as if one can't shift a fading by a coordinate (at least not with your pgf command, perhaps it works with the tikz keys), it sticked in the left upper corner. But if I use the shipout/background hook to move the picture into the center it works:

\documentclass{article}

\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{tikz}
\pgfdeclarefading{myfading}{\includegraphics[width=11cm]{fadpix-jpg-rsz.jpg}}  

\begin{document}
\mbox{} 

\AddToHook{shipout/background} 
 {
   \put(0.5\paperwidth,-0.5\paperheight)
   {% 
    \begin{tikzpicture}[remember picture,overlay]
     \node[inner sep=0pt] {%
     \includegraphics[width=11cm]{Utah_Teapot_mr_maya.jpg}
     %\includegraphics[width=11cm]{fadpix-jpg-rsz.jpg} %for test
      };%
    \begin{scope}[scope fading=myfading]
     \pgfsetfading{myfading}{} % 
     \node[text=white,text width=11.0cm] 
     {\lipsum[1-3]}; 
    \end{scope}  
  \end{tikzpicture}
 }}

\end{document}

enter image description here