Design in Data Figures: Rasterized versus Vectorized
Introduction
Have you ever zoomed in on an image and it was so pixelated you couldn’t read or understand anything? Have you ever tried to compile a LaTeX document and it took forever or time out? Have you ever had an email get stuck behind a filter because your figures or images were too big in terms of file size?
If any of that sounds familiar, you might be using the wrong kind of images or figures. There are two general categories, vectorized and rasterized images, and everyone should know and use both, but at different times. I want to share a few thoughts here on the differences between vectorized and rasterized figures with the hope that we can all explain, present, and communicate through figures a little better in the future.
The proceeding section introduces rasterized or bitmap images. If you are well acquainted with this category, you can skim or skip it, but to some readers, it may be new. In the following section, I introduce vectorized images and start to make comparisons between the categories. I then end with some pros and cons between these two types of images and figures and conclude with some recommendations as to when to use one or the other.
Rasterized Images
In a nutshell, a rasterized image or figure is where every pixel will be assigned a value between 0 and 1 (or between 0 and 255 or some other integer) that represents a particular intensity of a particular color. Thus, a pixel with a value of 0 could mean “no intensity’ of some color, say white, which means there isn’t any white in the pixel and the pixel would appear black. On the other hand, a pixel with a value of 1 could mean “full intensity of the color” (say white) and the pixel would indeed appear white. Finally, a pixel with an intensity value of 0.5 would be gray, half-way between black and white, or 0.75 which looks like a very light gray, or 0.1 which is a very dark gray, and so on.
Therefore, a black and white picture which is 100x100 pixels (or 10000 pixels total) needs 10 thousand values between 0 and 1 to be stored in the file for correct representation. But since the computer stores numbers in binary, it has to convert these decimal numbers, like 0.5 or 0.75 or 0.125 into 8-bit or 16-bit binary numbers. If in 8-bit, the intensity values will span the range from 0000000 (black) to 11111111 (white), to represent the decimal range of 0 to 1. Thus a mid-gray tone might be 0111111, about halfway between 0 and 1. If you know how to convert from binary to decimal, then you’ll realized that these values are actually representing the range of 0 to 255, i.e. 0 is all off (black), and 255 is all on (white). This means the decimal range of 0 to 1 is broken up into 256 different levels of available intensity and so your black and white picture can have 256 difference shades of gray. Some programs bypass the decimal representation and require the files or user to use integer values between 0 and 255 inclusive to represent the intensity (see Figure 1 below).
Extending this concept, a 16-bit number will have more gradations and can represent finer shades of gray, but it comes at a computational cost. For a 16-bit schema, the values would have to be between 0 and 65535, inclusive, with 65536 levels. You might have an image that uses more or less bits but the principle is the same.
Of course, not all images are black and white. A color figure or image will have often have three “channels”: a red channel, a green channel, and a blue channel. Red, green, and blue are called the additive primary colors and these three channels or colors add up to make a third color that is ultimately used to represent the pixel’s color. Thus, in a 100 x 100 pixel color image, 30 thousand intensity values are needed, or 10 thousand for each channel. Let’s say a pixel has the RGB values of 1 (or 255) , 1 (or 255), and 0 for the red (R), green(G), and blue(B) intensity values, respectively (see the top row, fourth column in figure 2). That means there is full red and green intensity assigned to the pixel but the blue intensity value is zero or off. With additive primary colors, this combination adds up to a yellow (a full yellow). Remember, these colors are additive so red and green make yellow (i.e. the opposite of blue). Since each channel might use 8 bits, the full color image might be labeled as 24-bit, or 3x8 bits, since 24 bits are dedicated to every pixel. This schema will allow one of 2^24 different colors to be assigned to every pixel - that’s over 16 million colors!
More directly, these channels are often represented with matrices composed of the intensity values for each color as presented in Figure 2. One matrix for red, one for green, and one for blue. Sometimes a fourth matrix, or the alpha channel, is used as well, which can represent how transparent or opaque the pixel appears. But that’s for another day.
So, why is this technique called a rasterized image? A raster is a grid-like pattern, similar to how many projectors, monitors, and TVs work by updating each pixel, one at a time, left to right, line by line down the image or screen to complete the gird. Zoom in on any rasterized image sufficiently and you’ll see the grid or raster of pixels, with each pixel taking on one, and only one, color, which is the combination of three 8-bit intensity values from the three channels.
The term bitmap is often used to describe a rasterized image and is also a specific file format you likely have seen (i.e. BMP), but all it means is that the image file holds information about each pixel individually, usually in a matrix or some sort of structure that can designate the intensity for all the channels and for each pixel. JPG (JPEG) and PNG files use the most common rasterized file formats, however, a PNG file can potentially decrease the file size by creatively assigning the same color to a bunch of pixels that are beside each other, reducing the unnecessary repeated information. Since photos rarely have large contiguous sections with the exact same color or shade, the JPEG format is often preferable but are still sometimes compressed like PNGs, especially in photographs with controlled backgrounds with the same color as in a studio with a green screen.
With 30 thousand values needed for a 100x100 pixel color image with a bit depth of 24 (i.e. 8-bits for each channel) the file would require 240,000 bits, or 30,000 bytes, or 29 kB. This can be further reduced through compression techniques (like the PNG one introduced above) but that’s also another topic for another day.
Vectorized Images
Now, what about vectorized images? Vectorized images contain information about the contents in the image through graphic commands or lines of code. It’s a completely different way of storing and generating figures. For example, if there is a blue circle in the middle of a white square (but still 100 x 100 pixels) you might see in the file the simple following command:
<path d="M50 50"/>
<circle cx="50" cy="50" r="50" fill="blue"/>
Essentially, those commands are telling the computer to move(M) over 50 units, and place a blue circle at the position of (50,50) with a radius of 50. The vector graphic image will end up like this:
In contrast, a blue circle (on a white background) for a rasterized image would require 30 thousand 8-bit numbers. Below (in Figure 4) is a small subset of the full matrix for just the Red channel near the top and slightly to the left of the middle. You can see the white section where the red should be all on (large or near 255), the blue circle would be off for the red channel (small or near 0), and the border of the circle is somewhere in between, due to some smoothing of the sharp edge.
A user or program would have to change or set thousands of individual entries within these three matrices or channels to make the image. That might take a longer time than simply running one or two lines of code. However, this rasterized image will end up like this:
There isn’t much difference between those (i.e., betwen figures 3 and 5) - at least not at the resolutions you are probably viewing this article - but if we zoomed in many times, we probably could see some difference. Note the differences once we scale up the two strategies in the following figures showing just a part of the blue circle:
On the left is the rasterized version and on the right is the vectorized. The lower clarity from the rasterized version might not be an issue though. Perhaps if you are viewing this on a device with low resolution, the differences are inconsequential. But in many circumstances the pixelation of an image can be, at worst, detrimental to the viewers understanding and at best, annoying. Text on images is particularly sensitive to this pixelation. In contrast, the vector graphic version (on the right) will still look good at any size and resolution because the programmatic commands or statements are independent of the image’s dimensions.
The term vectorized is used because of the simple geometric scaling of vectors and their constitutive elements if you multiply them by a scalar or constant. Essentially, everything scales up uniformly and clearly when a vector graphic is enlarged. If a rasterized image is scaled, say by 2, the same pixel might be repeated twice thereby making a particular feature have a boxy look (as seen above). Photoshop and other software can apply smoothing and interpolation functions to reduce this effect but at the end of the day, software can’t create data that wasn’t there to begin with.
Pros and Cons of the two types
The obvious advantage with vectorized images and figures is that by using commands instead of large matrices of numbers they have the potential to decrease file size and therefore working, sending, and compiling them in documents is easier and faster. But only the potential. In a vectorized image with say 100,000 data points, for example, there must be a lot of lines of code or statements in the vector graphics file (i.e. all those point locations and radius commands). With so many statements required for a very full or busy vectored graphic the rasterized version can sometimes end up smaller! For example, the following figures were created and saved with vectorized and rasterized methods (i.e. SVG and PNG respectively) to compare files sizes.
The left figure has 100,000 points and as a vector graphic is 3,421 KB but as a rasterized image it is only 254 KB. The right data figure has 1000 points, and as a rasterized image it is about the same size as the left at 250 KB - even with only one-hundredth of the data of the other. But with 1000 data points, the vectorized image’s size is relatively small at only 84 KB, which is indeed much closer to the 100 times fewer data. This shows that depending on the contents, either option might result in a smaller file.
The reverse phenomenon is observed when someone tries to vectorize a photo. Since every pixel in the photograph will need a distinct command or line of code, the file size can become very large, very quickly. In those cases, it’s often better to keep the raster graphic strategy instead of many lines of code or commands that tell the renderer to make a square at every pixel location with a specific color. This is sometimes what happens when a photo is vectorized and then embedded improperly into a bigger document. A lot of time and bandwidth can be used up downloading a massive PDF with these types of images.
As mentioned, a big advantage of using vectorized images is with scaling. Indubitably, we have all zoomed in on a figure hoping to read some text that wasn’t clear enough before. If the image, including the text, is vectorized everything will scale up accordingly and continue to be crystal clear. On the other hand, if the figure is rasterized, zooming in on text will cause it to be pixelated and although it will be larger, it won’t be more readable (See the example below in Figure 8)
Another advantage is the ease with which one can edit one feature (not one pixel) of the vectorized image. For example, the legend’s font size or style can be changed very easily in the code directly or with a vector graphics editing software like Adobe Illustrator or Inkscape. Changing the color of one data point or one trend line is also quite easy even if they are partially hidden or occluded below other elements. In contrast, a best fit line in a rasterized image (like something in Figure 8) might ultimately use hundreds of pixels but in different sections throughout the figure. These lines might also have edges that look smooth but selecting and editing them all at the same time will be very difficult, because the line’s extent or edge is blurred. In those circumstances, people usually regenerate the original image from scratch which might save time over pixel by pixel editing.
Interestingly, this weakness of rasterized images is also an advantage in some scenarios. Raster graphic editors, like GIMP or Adobe Photoshop, can manipulate and edit photos very powerfully, even down to one pixel by itself when needed. It’s almost impossible to edit one pixel in a vector graphic. With rasterized images, one can adjust contrast, saturation levels, apply filters, masks, and a host of other adjustments to the picture or parts of the picture, by applying a variety of transformations on the RGB channels or matrices directly. This process is often called Photoshopping, named after the most popular editing tool, because the pixels themselves are fixed up or shopped directly, as opposed to the feature or lines of code within a vectorized image.
Recommendations
My recommendation is to err on the side of using vectorized images when they contain data. Rarely does one have more data points than pixels in a figure and often a large portion of the figure is white (or the selected background color) which can be defined in one line of code and not by a repeated and extensive set of RGB values. Many other media and scenarios can benefit from using vectored graphics as well, including animations in presentations, infographics, logos, and anything else that might be reproduced on a variety of locations, sizes, and platforms. This is often what we should use for PowerPoint presentations because we may not know beforehand the resolution of the projector or computer ultimately used for the presentation.
On the other hand, when working with photos or a very large amount of data in a data figure, use rasterized images (even in presentations). The file size will be smaller (or at least will be limited), but know that the resolution should be set as high as feasible so that if anyone zooms in on the figure, they’ll still be able to read it or understand what it’s showing. Thankfully, photographs and other bitmap images are often not used to show text and data. Still, a higher resolution will increase the file size so make sure you experiment early and have a balance between the file size and resolution so that someone isn’t waiting forever for your one image (or many large images) to load.