[Tex/LaTex] Transparency vs image compression dilemma

file sizegraphicstransparency

I have a bunch of raster images that I want to include in a poster. For aesthetic reasons I want to replace the image background by a transparent one.

So far, I have been grabbing the images I had adding an alpha (transparent) channel manually (for example with GIMP) and saving in PNG format. The images look great when included.

However, each image is about 1MB large. Which is unacceptable since I want to include tens of images. If I save the file in JPG format I get a fifth of that size, about 200kB which is much more acceptable. The problem is that JPG doesn't support transparency and the background looks ugly.

Since JPG and PNG seems to be the only raster formats that I can include (as far as I know), I am in a dilemma, either have transparent backgrounds an generate a file 40M big or have white square backgrounds and have a 8M file.

This is for a poster so I would like to avoid changing the resolution of the images and the loosy compression used of JPG is very decent.

In fact, I couldn't find a lossy format that also support transparency (correction: see footnote), let alone one that can also be included in with any TeX backend. But I am wondering if I am missing something obvious or if there is a TeX-based solution around? (for example, like combining JPG and some mask in an intermediate PDF or something like that)

As an example I leave an image for which 1) it makes sense to have transparency (around) 2) can be loosely compressed (in fact the original is) 3) cannot be traced (vectorized) in any practical way (e.g. I want it to have the photographic feeling and certainly I don't want the vectorization to take more space than the JPG file) [quantitative info of this example picture: the original JPG is 80k, a poor vectorization (Inkscape) of the image is 200k, and PNG of this image is 600k]

engine


Correction: There is actually at least one lossy format that supports transparency, it is called PGF (not the same as pgf/tikz) http://en.wikipedia.org/wiki/Progressive_Graphics_File
But it seems that it is not supported by the TeX/PDF family. This can be very useful for beamer and posters.


EDIT: In order to summarize great @Jake's answer and comments there in, I put here a document with a newcommand defined to do all the steps. Creates the mask if necessary (assumes white-around background and a JPG original). (Needs -shell-escape and ImageMagick convert). Applied to the example image the resulting PDF is 103274 bytes only.

\documentclass{article}
\usepackage{pgf}

\newcommand{\includemaskedgraphics}[2][]{
    \IfFileExists{#2-mask.png}{}{\immediate\write18{convert #2.jpg -alpha set -channel RGBA -fuzz 3\% -fill none -floodfill +0+0 white -channel RGBA -separate +channel -evaluate-sequence add -threshold 0 -colorspace GRAY -negate #2-mask.png}}
    \pgfdeclaremask{themask}{#2-mask.png}
    \pgfimage[#1, mask=themask]{#2.jpg}
}

\begin{document}
\pagecolor{cyan}

\begin{figure}\centering
\includemaskedgraphics[width=6cm]{image}% don't use extension
\caption{Vroom!}
\end{figure}

\end{document}

Best Answer

The PGF package allows you to use a grayscale JPEG as a soft mask for your image.

Suppose you have a PNG with transparency called image.png. Then you can convert the picture to a colour JPEG using the ImageMagick command

convert image.png image.jpg

and extract the transparency mask to a grayscale JPEG using

convert image.png -alpha extract -colorspace GRAY -negate mask.jpg

Then you can include the image in your document using

\pgfdeclaremask{motormask}{mask.jpg}
\pgfimage[mask=motormask]{image.jpg}

\documentclass{article}

\usepackage{pgf}

\begin{document}
\pagecolor{cyan}

\begin{figure}\centering
\pgfdeclaremask{testmask}{mask.jpg}
\pgfimage[width=6cm, mask=testmask]{image.jpg}
\caption{Vroom!}
\end{figure}

\end{document}

File sizes:

PNG           690 KB
JPEG (image)   87 KB
JPEG (mask)    32 KB
PDF document  131 KB