1

I have figures which I use in several places, some of them in dark context and some of them light (for example, a my obsidian vault is dark but a PDF of my thesis is light). As such, some of the figures are colored to work in a dark environment, and some in light.

But sometimes I want to take a figure from a dark context and use it in a light context, or vice versa. So far I have created a copy of the figure for the light/dark mode by simply inverting the colors. I use color schemes in which this looks mostly fine.

But manually inverting the colors and saving a copy is exhausting, and inefficient. I want to do this automatically, and possibly without saving a copy of the original figure.

Is there a way to invert the colors of a figure while compiling the output of a latex document?

Thanks a lot!

Edit: I saw this question, but the answers were basically "you can't". Since the answers are unsatisfactory and the question is VERY old, I thought it would be best to open a new one. But if this is not the custom here, please let me know and I'll ask there instead.

Yoav Zack
  • 111

1 Answers1

0

Here's a sagetex solution along the lines of my answer to Partial or entire Image Blurring in TikZ?. Python takes care of the inverting the image during the compilation process. The picture is save and then \sagestr{result1} puts the text \includegraphics[width=2in]{MonaInv.png} into the LaTeX document.

\documentclass{article}
\usepackage{sagetex}
\usepackage{graphicx}
\begin{document}
Try this:\\
\includegraphics[width=2in]{Mona.png}
\begin{sagesilent}
from PIL import Image, ImageChops
img = Image.open('Mona.png')
Inv1 = ImageChops.invert(img)
Inv1.save('MonaInv.png')
result1 = r"\includegraphics[width=2in]{MonaInv.png}"
\end{sagesilent}
\sagestr{result1}
\end{document}

The result in Cocalc is: enter image description here

PIL refers to Python Pillow. The easiest way to experiment this way is with a free Cocalc account.

DJP
  • 12,451