0

Tikz allows us to use a blending mode, to turn the first image into the second:

enter image description here

This is great, but now I'd like to do the same, but while being able to choose the color of the incrusted image, for instance white or blue instead of black. Even better would be the ability to make the gray image act like a transparency mask for any arbitrary picture: this would allow me to use the grey image as clip path for another picture.

MWE:

Image amande_noir.png: enter image description here

\documentclass{article}

\usepackage{tikz}
\begin{document}

Starting point:

\begin{tikzpicture}
  \fill[red] (-2cm,-2cm) rectangle (2cm, 2cm);
  \node{\includegraphics[width=4cm]{amande_noir.png}};
\end{tikzpicture}

Blending mode multiply:

\begin{tikzpicture}
  \begin{scope}[transparency group]
    \begin{scope}[blend mode=multiply]
      \fill[red] (-2cm,-2cm) rectangle (2cm, 2cm);
      \node{\includegraphics[width=4cm]{amande_noir.png}};
    \end{scope}
  \end{scope}
\end{tikzpicture}

\end{document}

1 Answer 1

1

I finally found the solution using fadings, notably the great tikzfadingfrompicture that is well documented and is basically exactly an equivalent of transparency masks. I needed also to invert the color of the picture (as in fading, black is transparent and white is not), for this I used decodearray={1 0 1 0 1 0} (works for RGB picture, for CMYK and grey scale you might need to change these numbers).

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{fadings}
\begin{document}

\noindent Starting point:

\noindent\begin{tikzpicture}
  \fill[red] (-2cm,-2cm) rectangle (2cm, 2cm);
  \node[overlay]{\includegraphics[width=4cm]{images/amande_noir.png}};
\end{tikzpicture}

Blending mode multiply:

\noindent\begin{tikzpicture}
  \begin{scope}[transparency group]
    \begin{scope}[blend mode=multiply]
      \fill[red] (-2cm,-2cm) rectangle (2cm, 2cm);
      \node[overlay]{\includegraphics[width=4cm]{images/amande_noir.png}};
    \end{scope}
  \end{scope}
\end{tikzpicture}

I want the same in a different color (say blue instead of black):

\begin{tikzfadingfrompicture}[name=myblackalmond]
  % decodearray: for each component RGB we provide un tuple (a, b) where 0 is mapped to a and 1 is mapped to b.
  % so decodearray={1 0 1 0 1 0} inverts the picture so that black is white and white is black
  \node{\includegraphics[width=4cm,decodearray={1 0 1 0 1 0}]{images/amande_noir.png}};
\end{tikzfadingfrompicture}

\noindent\begin{tikzpicture}
  \fill[red] (-2cm,-2cm) rectangle (2cm, 2cm);
  \fill[path fading=myblackalmond, fit fading=false, blue,overlay] (-3cm, -3cm) rectangle (3cm, 3cm); 
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .