4

I have been using the fadings tikz library for colouring images (png and pdf, mostly) in LaTeX with the code below. The only two libraries I am using here are tikz and graphicx.

% Importing the image
\begin{tikzfadingfrompicture}[name=theimage,inner sep=0]
    \node [fill={transparent!0}]
    {\includegraphics{nameoftheimage}}; 
\end{tikzfadingfrompicture}

% Printing "theimage"
\begin{tikzpicture}[inner sep=0]
% Colouring
\path[scope fading=theimage,fit fading=false];
\node[fill=magenta, minimum width=thewidth, minimum height=theheight]{}; 
\end{tikzpicture}

When I do this, I obtain the following image:

Processed image

That is already great. The problem is that the source file has to be:

Source image

instead of:

Original image

This forces me to invert the colouring of all the images I want to use. I've googled the tikzfadingfrompicture function, but I found nothing useful. (Nor in the tikz documentation.) Is there an easy way to manipulate the code above so that it is not necessary to invert the colours of the original picture? Thanks in advance for any answer.

6

As you note yourself, your method "inverts the picture", i.e. does the step you do by hand. You could just store this result in a \savebox, the inverted version of gives the desired result.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\newsavebox{\cheat}
\begin{document}
\begin{tikzfadingfrompicture}[name=theimage,inner sep=0]
    \node[fill=transparent!0]
    {\includegraphics[scale=0.3]{example-image-a}}; 
\end{tikzfadingfrompicture}

\savebox\cheat{\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{\includegraphics[scale=0.3]{example-image-a}}};
\fill[path fading=theimage,fit fading=false] (img.south west) rectangle (img.north east);
\end{tikzpicture}}
\begin{tikzfadingfrompicture}[name=inverted,inner sep=0]
    \node[fill=transparent!0]
    {\usebox\cheat}; 
\end{tikzfadingfrompicture}

\begin{tabular}{ccc}
\tikz[baseline={(pic.base)}]{\node(pic){\includegraphics[scale=0.3]{example-image-a}};}
& \tikz[baseline={(pic.base)}]{\node(pic){\usebox\cheat};} &
\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{\usebox\cheat}};
\fill[magenta,path fading=inverted,fit fading=false] (img.south west) rectangle (img.north east);
\end{tikzpicture}
\\
original & inverted & inverted twice
\end{tabular}

\end{document}

enter image description here

And you can make this a macro so that you do not have to redo these steps.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\newsavebox{\cheat}
\newcommand{\FadeShade}[2][]{\begin{tikzfadingfrompicture}[name=theimage,inner sep=0]
    \node[fill=transparent!0]
    {#2}; 
\end{tikzfadingfrompicture}%
\savebox\cheat{\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{#2}};
\fill[path fading=theimage,fit fading=false] (img.south west) rectangle (img.north east);
\end{tikzpicture}}%
\begin{tikzfadingfrompicture}[name=inverted,inner sep=0]
    \node[fill=transparent!0]
    {\usebox\cheat}; 
\end{tikzfadingfrompicture}
\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{\usebox\cheat}};
\fill[#1,path fading=inverted,fit fading=false] (img.south west) rectangle (img.north east);
\end{tikzpicture}}
\begin{document}
\FadeShade[magenta]{\includegraphics[scale=0.3]{example-image-a}}
~
\FadeShade[blue]{\includegraphics[scale=0.6]{example-image-duck}}
\end{document}

enter image description here

  • Thanks. I am studying your answer. – lfba Aug 25 at 5:09
  • Thanks a lot, your answer pointed me into the right direction. I was intending to use this method for creating a macro. When I finish it I am going to share it. (I will also give you the proper credits.) Thanks. – lfba Aug 25 at 5:54
  • @hpekristiansen I have just given a flag for your comment. What are you doing in this friendly forum? – Black Mild Aug 25 at 10:50
  • @Black Mild: I do not understand what you want or what you are talking about. I deleted my comment – hpekristiansen Aug 25 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.