Sunday, February 24, 2013

How to choose the correct format for your academic figure

If you're using jpeg for all of your graphics, you probably want to read this. If you are using BMP for all your graphics you either really know what you're doing or you MUST read this.

I often get asked about figure generation and converting figures from one format to another, usually for use in a presentation or academic paper. Of course, a lot of this is a matter of taste, but sometimes I see what people do and it makes me want to start a huge lecture about the ubiquitous abuse of jpegs and the distinction between vector graphics and bitmap graphics. But I'm usually a generous person, and often I just give them a few quick hints. So here it is, for your pleasure, the basics of graphics formats and their intended uses. Caveat: I am by no means an expert at this stuff. Most of what I know is from the wikipedia articles.

Vector or Bitmap?

Before I go into the nitty-gritty, there's a few things we need to discuss. The main one is the distinction between vector graphics and bitmap graphics. These are the two main paradigms of image storage and understanding the strengths and limitations of both is crucial.

Instead of a long diatribe, however, I will try and present this in as few images and words as possible. The image below, which I pulled from Wikipedia, summarizes this section:

To put it in words, there are two main ways of representing images inside a computer: raster (or bitmap) images are stored as arrays of numbers, where each number represents the color and transparency of one tiny block of the image, a pixel; a vector image is instead stored as a collection of lines and circles and other similar geometric primitives, generally known as paths. In a bitmap image two points cannot be closest than one pixel apart, which is a function of the resolution of your image. So, when you zoom in, eventually you hit that pixel limit and your image starts looking like an old Atari game. In a vector image the position of each end of a line (or primitive node) is stored as a high precision number, which means that if the distance between two points is greater than 1e-12 you will eventually be able to tell them apart by zooming in enough (although I would recommend spending your time on more valuable endeavors, like writing blog posts for the benefit of the technologically hungry).

If the above didn't make sense, or it did but the implications are not clear, or if you simply don't care about the details, here is what it boils down to: use raster formats for things you do not know the geometry of, like scanned in pictures of your dog or screenshots of your sweet desktop arrangement; use vector graphics for images where geometry is known or obvious, like computer generated graphs, schematics, or cartoons.

Common image formats and what to use when

The above stuff is all well and good, but it doesn't really tell you why using jpegs indiscriminately is bad, and that's what you're here for, right? Since nothing is better explained than by example, I'll give a few scenarios and my personal preference (and reasons) for doing the voodoo that I do.

Pictures of my dog

Ok, I don't actually have a dog, but if I were to have one I would name it Jake and take a million photos of him, and stick that stuff in GIMP in order to add party hats to its cute head. If you have an amateur camera like mine, it will likely not give you much of an option as to the type of image it saves. It will probably be JPEG, and with good reason: JPEG is a compression algorithm for raster images that is specifically designed to work well on standard photographic material. For example, JPEG does really well (high compression ratio with very small loss in perceived quality) for pictures of sand, or dogs, i.e. where there is already a lot of information we perceive as nothing more than just noise. It does and adequate job for pictures of faces, but it does a not so good job for certain Joan Miro paintings, where it often fails to preserve edge sharpness. Unfortunately your crappy camera does not give you an option, so this is the best you're going to get, so just turn the compression quality to High and live with it. Or else buy a decent camera that allows you to store images in raw format (large files, no compression) or tiff (lossless compression) but at that point you're turning into a professional photographer and you're only reading my post for the jokes.

Screenshots and scanned images

Because JPEG compression does not do well with edges, and because computer generated images, like screenshots, often involve sharp edges, some clever people got together and invented a different compression algorithm: PNG (portable network graphics). This is in many ways similar to JPEG, but is in no way a replacement. Rather, it complements it. While JPEG is good at encoding originally noisy raster images and bad at edges, PNG is good at edges but bad at preserving the noisiness of sand on a beach. That is why, when you take a screenshot, save it as a PNG. And, most importantly, don't save it as a JPEG and then change it to a PNG: every time you re-encode you lose quality and suffer from the downsides of your algorithm. I made the images below to show the differences between the two formats. First up is a picture of tux, the beloved linux penguin:

I encoded the image as both PNG and JPEG to have the same file size. They look pretty similar, but if you zoom in you start to notice nasty artifacts in the JPEG version. Here are the original files for comparison: tux.jpg and tux.png. On the other hand, this next image shows the downside of PNG.

Both images look (at least to me) to be the same quality, but the size of each file is very different. And that's with a low level of JPEG compression. I could have dialed the compression up quite a bit with very little changes to the observed quality. Again, original images are gravel.jpg and gravel.png.

Other uses for PNGs are creating a raster image of an icon or cartoon or sketch. All the twitter icons in all your apps: they are likely pngs. Most webcomics publish their art as PNG. If you are going to scan a sweet doodle you made, or save that drawing you made using your wacom tablet, PNG is probably your best choice. You get the gist, I hope.

One small note here: if you're scanning documents that are mostly text, and your scanning software is not something last modified in the previous century, you likely have the option to use OCR (Optical Character Recognition) while scanning, and obtain a PDF. If you have this option, I highly recommend you use it. It will try and do character recognition for your document, which will result in your being able to select text from your scanned document and copy-pasta it anywhere you want it. Not to mention that, unless you mess with the options, the original image of the scanned document will still be what is visible, so regardless of the quality of OCR, your document is still be readable by eye.

Graphs and Sketches

And here we are at my favorite section, and that is creating vector graphics from scratch. This is your best option whenever you create digital images outside of the restrictions in the previous sections. So, when do you create images on a computer? A lot of people I know use MATLAB to generate plots of their data. Others use R or python or whathave you. Sure, you can save your graphs as a high resolution JPEG and lug around 50 files one megabyte each, digital storage is cheap and getting cheaper, right? But what if you really want to zoom in on one of those plots later on? What if you want to change some text on it, but you lost all the data and regenerating the plot is out of the question? You use JPEG and you're hosed. That's why you use a vector format instead: SVG, PDF, EPS. The advantages are plenty: your file will be smaller in size and also editable outside of the software that created it; not to mention you can zoom in up to infinity. You can use Adobe Illustrator (costs $) or Inkscape (open source) to edit any of the text or even change the colors of your curves or thickness of your axes. Everything is easy! Now that we're at vector graphics software, let's say you want to make a diagram, or sketch of a mechanism. Are you going to use Photoshop? Boo! Photoshop (and its free cousin, gimp) are great programs but they are made for editing photos and other image manipulation tasks (as their names imply). Sure, they both have *some* vector graphics capabilities built in, but if you're going to make a clean sketch it's like using a bread knife to mow the lawn: it will work, but do you really want to do it when there's a brand new lawn mower in the garage? So go ahead and download Inkscape or ipe (they are free) and play around for a while. Save your stuff as SVG if you're going to post it on the web, or PDF if you're going to use it in your dissertation ($\LaTeX$ ftw).

When Vector Graphics Goes Bad

I have encountered only one significant case where all the above information would have had me using vector format for an image but instead the correct choice was to encode it as PNG: a vector image with a very large number of primitives. Imagine you are doing a scatter plot of something like ten thousand data points, and you save it as PDF. There will be points all over each other, you won't be able to actually see more than a few hundred of them, but yet the image will be a few megabytes in size and it will take seconds to load (I know, I could just become a patient person, but I'd rather spend my time figuring things out rather than waiting on needless computation). So, all of a sudden, the love affair with vector format has come to bite you in the proverbial ass. Maybe your paper is even too big to submit to the conference! You have a few options, depending on how crafty you want to be. First, just give up, save that image as PNG, go home and have a cold one. If you want to be fancy, you can do the following. Save two images, one just the axes and the text, saved in SVG format. The other, just the troublesome datapoints, in PNG format. You can then go into your favorite vector image program and merge them together, save as a PDF. PDFs are awesome because they allow you to mix vector and raster images. You will still be able to select the text, modify later and everything else, except for the data points. But your image will be small and manageable again.


There is one format suitable for every purpose. When in doubt, read the wikipedia entry for it. You will learn a ton. Then use that information to your advantage to make your stuff look consistently good, have consistently small file sizes (and load times), and finally help yourself out by keeping as much of an image editable as you can. I could not count the times my adviser wanted a slightly larger font on my figure and because of poor planning it meant I had to re-run an overnight simulation to do it. I've been there and done that, and you don't have to repeat my mistakes!

1 comment:

  1. cool article. i was thinking of trying some vectors at one point but then got lazy / busy.

    greeting from the old country ;)
    hope you're well

    alex boldea