[Tex/LaTex] TikZ: scope fading doesn’t work with \includegraphics

tikz-pgf

In this example, the included PNG (the large rectangle in the middle) should fade from left an right due to the scope fading on a previous path. Why doesn't it?

Edit: I genuinely need a fade as I am trying to simulate a crossfade by layering a partially transparent image onto another image. Using \pgfdeclaremask doesn't really work as the source textures are being cropped to the left and right (and the exact dimensions involved may change), so the mask size can't easily be determined. How can I obtain a crossfade?

enter image description here

MWE:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{shadows}

\tikzset{path image/.style 2 args={
path picture={
\node at (path picture bounding box.center) {
\pgfimage[height=#2]{#1}
};}}}

\begin{document}
\begin{tikzpicture}
    \draw[path image={texture_red.png}{90mm}] (3mm,3mm) rectangle (63mm-3mm,88mm-3mm);
    \draw[black,scope fading=east] (0,0) rectangle (63mm,88mm); 
    \draw[path image={texture_black.png}{90mm}] (3mm,3mm) rectangle (63mm-3mm,88mm-3mm);
\end{tikzpicture}
\end{document}

texture_black.png

[]

texture_red.png

[http://i.stack.imgur.com/MidAg.png]

Best Answer

There are a couple of of problems here.

In the MWE the scope fading in the second \draw command will have no effect on nodes in any subsequent path. To have an effect the scope fading should be applied to a scope or the node itself (as the examples in the manual show).

However, it seems that it is necessary to "flatten" the texture_black.png in order to get fadings to work with the image. This can easily be done using gimp and will remove any existing alpha channel (which is what I presume is causing the problem).

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
\node [left,  scope fading=east, label=90:Original] 
  {\pgfimage[height=2in]{texture_black.png}};
\node [right, scope fading=east, label=90:Flattened] 
  {\pgfimage[height=2in]{texture_black_flattened.png}};
\end{tikzpicture}
\end{document}

enter image description here

So, returning to the OP's problem, the scope fading needs to be applied to the node containing the image and the image needs to be flattened.

\documentclass[tikz, border=5]{standalone}
\usepackage{tikz}

\usetikzlibrary{fadings}

\tikzset{path image/.style args={#1#2#3}{
  path picture={
    \node [#3]at (path picture bounding box.center) {
     \pgfimage[height=#2]{#1}
   };
  }
}}

\begin{document}
\begin{tikzpicture}
  \draw [path image={texture_red.png}{90mm}{}] 
    (3mm,3mm) rectangle (63mm-3mm,88mm-3mm);
  \draw [black] (0,0) rectangle (63mm,88mm); 
  \draw [path image={texture_black_flattened.png}{90mm}{scope fading=east}] 
    (3mm,3mm) rectangle (63mm-3mm,88mm-3mm);
\end{tikzpicture}
\end{document}

enter image description here

To compile the above examples, the images from the original post are required, and the following image, saved as texture_black_flattened.png:

enter image description here