# TikZ: scope fading doesn't work with \includegraphics

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?

MWE:

\documentclass{article}
\usepackage{tikz}

\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[path image={texture_black.png}{90mm}] (3mm,3mm) rectangle (63mm-3mm,88mm-3mm);
\end{tikzpicture}
\end{document}


texture_black.png

texture_red.png

• I don't really understand the question. Why do you think that the included image should be affected? – cfr Jun 15 '14 at 23:15
• @cfr: I guess a misunderstanding as to how tikz works. A better question might then be how to achieve the effect with the image fade. – Mohan Jun 15 '14 at 23:19
• OK. I think you may want to use \pgfdeclareimage... etc. instead of \includegraphics... so that you can use masking. This is around 1066 of the manual. Maybe there's another way but I thought that opacity etc. could only be applied normally to stuff tikz renders rather than including. (You can put something over the image but it doesn't affect the content of the included image.) So this maybe is a case where the pgf image inclusion is needed rather than ordinary graphics inclusion. – cfr Jun 15 '14 at 23:23
• @cfr: Unfortunately switching to pgfdeclareimage doesn't seem to fix it. Shall I post a revised MWE? – Mohan Jun 15 '14 at 23:45
• FWIW, 87.4 says: >You can only mask images the are in a “pixel format.” These are .jpg and .png.< So it should be possible. But I don't understand the pgf internals... – Mohan Jun 15 '14 at 23:54

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}
\begin{document}
\begin{tikzpicture}
{\pgfimage[height=2in]{texture_black.png}};
{\pgfimage[height=2in]{texture_black_flattened.png}};
\end{tikzpicture}
\end{document}


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}

\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);

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