# How to get Tikz to get the color of an image pixel

I am drawing on top of an image that has been included with \includegraphics using TikZ. I would like to be able to control the color of a line, text, node etc based on the color of the pixels of the included image underneath.

Looking at the TikZ spy library, it seems that accessing the individual pixels of an included image is possible. For example, see this question/answer.

My end goal is to be able to draw a line on top of an image, which changes color with respect to the pixel color of the image. Perhaps it is necessary to use the PGF library for this?

• Welcome! Please add a minimal example showing what you've tried/want to do, so people have something to work from. However, I think that what you want to do is not possible. TikZ doesn't need to know what colour the pixels are in the answers you linked. I don't think there is any way that TikZ could know this. \includegraphics is relying on the backend driver (engine dependent). You might be able to use PGF's own image-inclusion stuff. This is not recommended (by TikZ/PGF) unless you really need it, but there are some things it can do which graphicx does not support. – cfr Feb 24 '17 at 2:40
• In principle it would be possible using lualatex but not something I'd try doing. Perhaps easier with pythontex. – JPi Feb 24 '17 at 2:56

<hand waving> You can colour a pixel according to the colour of a pixel in an image included with \includegraphics. At least, you can do so indirectly. It does not, however, follow that PGF/TikZ has access to information about the colours in the images. I suspect it does not. Rather, I would guess that it tells the backend driver how to colour the pixel based on the colour of the pixel in the image. Or something like that ... </hand waving>

For example, here are four renderings of the standard tiger image. These were rendered using pdfTeX and viewed in Okular on GNU/Linux. They may well look different if the code is compiled with other engines and/or viewed in other PDF viewers and/or on other systems.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}[blend mode=difference]
\node {\includegraphics{tiger}};
\shade [bottom color=blue, top color=green, middle color=magenta] (current bounding box.north west) rectangle (current bounding box.south east);
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\begin{scope}[blend mode=screen]
\node {\includegraphics{tiger}};
\shade [bottom color=blue, top color=green, middle color=magenta] (current bounding box.north west) rectangle (current bounding box.south east);
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\begin{scope}[blend mode=exclusion]
\node {\includegraphics{tiger}};
\shade [bottom color=blue, top color=green, middle color=magenta] (current bounding box.north west) rectangle (current bounding box.south east);
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\begin{scope}[blend mode=color dodge]
\node {\includegraphics{tiger}};
\shade [bottom color=blue, top color=green, middle color=magenta] (current bounding box.north west) rectangle (current bounding box.south east);
\end{scope}
\end{tikzpicture}
\end{document}