This question already has an answer here:

Is there a way to use black-and-white images in a PDF as transparency masks for vector content? I know that I can use


for hard (1-bit) or soft transparency masks, but only with raster images maskfile and imagefile of the same dimension.

If I understand correctly, that is only a requirement of TikZ/PGF, not PDF. At least I'm sure that PDF allows bitmap stencils of different pixel size over other bitmap images (that's what Xerox copiers do for scans to PDF).

However, is there a way to do the equivalent of

  \fill[mask=maskname,color=somecolor] (0,0) rectangle (100,100);

to have the opaque parts of the mask drawn in somecolor, or even a way to mask arbitrary content with an alpha channel taken from a grayscale picture?

In case it matters, I'm currently using LuaLaTeX, but that's not a strict requirement.

marked as duplicate by Paul Gaborit tikz-pgf Mar 1 '16 at 13:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Did you read Coloring a black and white PDF graphic? – Paul Gaborit Mar 1 '16 at 10:49
  • Indeed, the approach described there works. Thanks! In short for further reference: \begin{tikzfadingfrompicture}[name=mask] \node[fill=transparent!0, draw=none, inner sep=0pt] {\pgfimage{img}}\end{tikzfadingfrompicture} ... \path[scope fading=mask, fit fading=false, <fading transform={...}>]; \fill[color=<color>] <coords> rectangle <coords>; – akobel Mar 1 '16 at 12:57
  • Addendum: be careful with that; it turned out that a page of such a PDF file did not print on a Xerox printer which AFAICS handles the PDF internally. So if in doubt and you are preparing for press, stick to traditional graphics without fancy stuff, and rather sacrifice file size... – akobel Apr 19 '16 at 10:41