Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
Maybe you can try to shrink the resulting PDF, e.g. by using Adobe Acrobat or pdfsizeopt. –  Philipp Feb 14 at 3:04
1  
What about vectorizing/tracing each graphic with Inkscape or similar? inkscape.org/doc/tracing/tutorial-tracing.html –  Paul Gessler Feb 14 at 3:25
    
@PaulGessler, yes, I do that when possible. Unfortunately sometimes the results are poor or it is impossible to expect a decent result. Here it is an example of an image that is raster, transparency (around) makes sense and it cannot be traced: camdart.biz/wp-content/uploads/2013/08/Car-Engine-1.jpg –  alfC Feb 14 at 3:32
2  
I would suggest a compromise: produce a 40MB version with uncompressed images when making a poster-sized print, and produce an 8MB version for email distribution. If you remove background gradients, I'm not sure why you can't fill the image backgrounds with the exact same colour as the poster background. –  Moriarty Feb 14 at 4:41
2  

2 Answers 2

up vote 11 down vote accepted

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
share|improve this answer
    
funny enough the mask itself, because of its nature is much better compressed to the point that PNG (mask) 4KB. And the final PDF is even smaller PDF document 100 KB. The command is convert image.png -alpha extract -colorspace GRAY -negate mask.png and the latex line is \pgfdeclaremask{testmask}{mask.png} –  alfC Mar 29 at 1:21
    
Also, I found this command that work for most images (with white "simple connected" background. convert image.jpg -alpha set -channel RGBA -fuzz 8% -fill none -floodfill +0+0 white -channel RGBA -separate +channel -evaluate-sequence add -threshold 0 -colorspace GRAY -negate mask.png. This way one doesn't need to manually crop the image or pass through an unnecessary intermediate .png. (The limitation is that original white holes won't show up transparent --like in the example--, but for 90% of the images this is enough.) Formoreseehere: imagemagick.org/Usage/masking/#floodfill –  alfC Mar 29 at 1:57
    
Using PNG mask (like my comment above) can produce/expose this bug in Acrobat tex.stackexchange.com/questions/46237/… –  alfC Mar 29 at 2:54

Use gimp to color (fill) the background of the photos the same as the background of the poster. Should not change the size of the picture considerably and will look transparent (no white block).

share|improve this answer
3  
Matching colors can be a challenge. Besides the poster or slide backgrounds can contain gradients, or other background images or textures. –  alfC Feb 14 at 1:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.