LaTeX 中的图像合成/混合模式(乘法)?

LaTeX 中的图像合成/混合模式(乘法)?

在 Photoshop(以及许多其他计算机图形应用程序)中,有多种方法可以将图像/图层与其底层背景合成。这些方法有时称为混合模式。

一种特殊的混合模式是正片叠底,它具有方便的功能,可以从黑线插图中消除白色背景,同时正确处理抗锯齿边缘(见下面的示例)。

有没有办法在 LaTeX 中做到这一点?我有大量带有白色背景的图像,需要将它们包含在两个具有不同颜色背景的文档中。我正在寻找一种快速从图像中删除白色背景的方法,而无需在 Photoshop 中手动为每张图像添加 alpha 蒙版。

正常混合模式: 在此处输入图片描述

正片叠底混合模式: 在此处输入图片描述

抗锯齿边缘的特写,正确羽化: 在此处输入图片描述

答案1

你可以通过以下方式实现tikz 混合模式或者,如注释中所述,使用批处理模式下的 imagemagic 等工具(可能将白色背景变成透明背景,以便可以将其插入到任何位置):

在此处输入图片描述

\documentclass[]{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[blend group=multiply]
  \node{\includegraphics[width=8cm]{example-image-a}};
  \node{\includegraphics[width=5cm]{example.png}};
\end{tikzpicture}

\end{document}

编辑

您还可以使用blend modeInstead ofblend group使其与文档的其余部分交互,但文档指出某些渲染器无法使其正常工作。至少在 Okular 中它似乎有效。如果您想要一个独立于后端的解决方案,您还可以使用path pictureorder 将背景复制到新节点:

在此处输入图片描述

\documentclass[]{article}

\usepackage{eso-pic,graphicx}

\usepackage{tikz}

\AddToHook{shipout/background}{%
  A
  % \begin{tikzpicture}[remember picture, overlay]
  %   \node[anchor=center] at (current page.center) {
  %     \includegraphics[width=\paperwidth,height=\paperheight]{#1}
  %   };
  % \end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
  \node[anchor=center] at (current page.center) {
    \includegraphics[width=\paperwidth,height=\paperheight]{example-image}
  };
\end{tikzpicture}


Hey I am some text, see how the image blends? Note however that according to the Tikz documentation, some renderer might fail to render this properly:

\vspace{-2cm}

\begin{tikzpicture}[blend mode=multiply]
  % \node{\includegraphics[width=8cm]{example-image-a}};
  \node{\includegraphics[width=5cm]{example.png}};
\end{tikzpicture}

More safe (renderer independent), but will not work nicely with the background:

Hey I am some text, see how the image blends? Note however that according to the Tikz documentation, it

\vspace{-1cm}
\begin{tikzpicture}[blend group=multiply]
  % \node{\includegraphics[width=8cm]{example-image-a}};
  \node{\includegraphics[width=5cm]{example.png}};
\end{tikzpicture}


With a background picture:

\begin{tikzpicture}[blend group=multiply]
  \node{\includegraphics[width=8cm]{example-image-a}};
  \node{\includegraphics[width=5cm]{example.png}};
\end{tikzpicture}

With path picture (renderer independent, but a bit more annoying to use, possibly a bit less reliable (we are drawing the background twice in the internal node), and it would not overlay with text that is outside of the picture):
\tikzset{
  %% WARNING: make sure to use "remember picture"
  absolute fill with image/.style={
    path picture={
      \node[anchor=center] at (current page.center) {
        \includegraphics[width=\paperwidth,height=\paperheight]{#1}};
    }
  },
}

\begin{tikzpicture}[remember picture]
  \begin{scope}[transparency group]
    \node[absolute fill with image=example-image]{\phantom{\includegraphics[width=5cm]{example.png}}};
    \begin{scope}[blend mode=multiply]
      \node{\includegraphics[width=5cm]{example.png}};
    \end{scope}
  \end{scope}
\end{tikzpicture}


\end{document}

相关内容