7

I have several greyscale-pictures that I would like to get colorized within pdflatex (so it will be automatically colorized to a given color, from black-to-white lets say black-to-red).

Is there a way to manipulate external graphics in pdflatex directly?

If this is not possible what way would you recommend to do this for a hundreds of pictures on a linux system? (maybe imagemagick?)

Example:

Example Image

Should be something like:

enter image description here

  • 2
    What kind of pictures are they? Vector or raster? Do they have transparent backgrounds? Could you edit your question to include a sample image? – Jake Jun 2 '13 at 21:21
  • 2
    JPEG like, no transparency, raster, picture added – MrD Jun 2 '13 at 22:13
4

As far as I know, you can't alter raster images in LaTeX itself. You can, however, call ImageMagic, like you suggested, to transform the image just before it's included. Here's a macro that allows you to write

\redify{image.jpg}

to convert and include the image (shown here in comparison to the unaltered image which is included using \includegraphics{image.jpg}:

ImageMagick is called every time the \redify macro is called, regardless of whether the image has already been converted. If you have many and very large images, you might want to compile the document once with the conversion enabled, and comment out the conversion line from the macro on subsequent runs:

\documentclass{article}
\usepackage{graphicx}
\newcommand{\redify}[2][]{%
\immediate\write18{convert #2 -fill red -tint 40 blurred#2}%
\includegraphics[#1]{blurred#2}%
}

\begin{document}
\includegraphics{image.jpg}\redify{image.jpg}
\end{document}
  • 1
    Thanks indeed. Actually I had an old function called executeifnewer that uses the \pdffilemoddate routine. This performs very well, as it checks whether the monochrome picture is newer than the redified picture is. If so, it will call the conversion, and if not, it will just include the picture. – MrD Jun 3 '13 at 8:37
  • @DL6ER: Ah, that sounds really useful! Could you maybe post (and accept) an answer showing the final macro you ended up using? That would help others who come across a similar issue in the future. – Jake Jun 3 '13 at 15:16
  • I will do this soon. – MrD Jun 5 '13 at 14:36
3

This answer is an improvement of Jakes previous answer, please read his answer if you're not satisfied with mine.

Using ImageMagick is probably the only way of manipulating raster pictures the way I need.

enter image description here

This output is produced by putting

\includegraphics{image.jpg}\redify{image.jpg}

But as Jake mentioned there could be a speed problem:

ImageMagick is called every time the \redify macro is called, regardless of whether the image has already been converted. If you have many and very large images, you might want to compile the document once with the conversion enabled, and comment out the conversion line from the macro on subsequent runs.

Therefore I added the executeifnewer-routine that will check if the source image is newer than the redified one. If so, it will call the conversion, and if not, it will just include the picture. This might save you much time and can easily ported to other (not only conversation) applications.

Here is the code:

\documentclass{article}
\usepackage{graphicx}
\newcommand{\executeifnewer}[3]{%
  \ifnum\pdfstrcmp{\pdffilemoddate{#1}}{\pdffilemoddate{#2}}>0%
  {\immediate\write18{#3}}%
  \fi}

\newcommand{\redify}[2][]{%
  \executeifnewer{#2}{blurred#2}{convert #2 -fill red -tint 40 blurred#2}%
  \includegraphics[#1]{blurred#2}%
}

\begin{document}
\includegraphics{image.jpg}\redify{image.jpg}
\end{document}

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.