TikZ - how to pass an image by a filter

The approach used in the OP adds opacity to two separate objects (JPEG image, node background fill) and subsequently overlays them. But, adding opacity removes contrast from the original JPEG, blurring the texture it provides. As a result, one only gets a somewhat pale composite picture.

The canonical way, however, is to define a transparency mask (fading) from the JPEG image that can later be used to mask other graphical objects. Like shadings, pgf/TikZ fadings are scaled before being used (see PGF manual, section 114.3). Therefore, in order to use the full JPEG image when applied as a mask, we scale it to 50bp x 50bp size with a 25bp margin added upon mask creation.

\documentclass{report}

\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{document}

\begin{tikzfadingfrompicture}[name=mask image]
  \node [inner sep=25bp] {\includegraphics[width=50bp,height=50bp]{DvhfC}};
\end{tikzfadingfrompicture}%
% Now we use the mask in another picture:
\begin{tikzpicture}
  \fill [blue,path fading=mask image] (0,0) rectangle (1,1);
  \fill [red,path fading=mask image] (1,1) rectangle (4,4);
  \fill [green,path fading=mask image] (2,0) circle [radius=1cm];
\end{tikzpicture}

\end{document}

Note that brighter areas of the mask image add less, while darker areas add more transparency to the object being masked:

enter image description here


You can put your image and then shade something over it (TikZ manual 15.7, "shading a path").

\documentclass{report}

\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
    \node[opacity=0.5, inner sep=0pt](A) at (0, 0) {\includegraphics[scale=0.5]{DvhfC.jpg}};
    \shade [left color=blue, right color=white, shading angle=135, opacity=0.5]
           (A.south west) rectangle (A.north east);
\end{tikzpicture}
\end{document}

enter image description here

Be aware that this kind of transparency tricks can trigger PDF viewer "features" sometimes, so check if it works correctly for you (the grab is from okular).

Tags:

Tikz Pgf