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]


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.


    \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}}
    \pgfimage[#1, mask=themask]{#2.jpg}


\includemaskedgraphics[width=6cm]{image}% don't use extension


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





\pgfimage[width=6cm, mask=testmask]{image.jpg}


File sizes:

PNG           690 KB
JPEG (image)   87 KB
JPEG (mask)    32 KB
PDF document  131 KB
  • 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 '14 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 '14 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 '14 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).

  • 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 '14 at 1:25

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.