images

We are going to start talking about how computer represent images, and the features that are important when we use them in a webpage.

Class Preparation

1. Watch at home the following video from DIYWebmastery (7-1/2 min):

Image Formats: Understanding images

Compression

The video above is really good and summarizes the important points: for a webpage, we care about the size of the image – which affects the time it takes to load the webpage, and whether the image format supports transparency or not.

server sending an image to a browser

The size of the original images straight from a digital camera, a.k.a. raw images, is very large, so we usually compress them, i.e., we change their representation so they take less space. An image with c columns and r rows has c x r pixels (a dot of color) so, if each pixel is stored in 1 byte, then an image takes about c x r bytes.

When a browser asks a server for an image that is part of a webpage, the server transmits the compressed version of the image; our browser receives the compressed version, and reverses the process, i.e., it decompresses the image, changing the representation back to get that of the original raw image, which it then displays on the screen. The compression process is done so often that in reality, we seldom store or transmit raw images anymore, dealing only with compressed images; the raw images are restored for displaying them, though. Transmitting a raw image would require transmitting all its pixels as bytes (a byte is a combination of 8 zeros and ones), so an image of N pixels would require us to transmit N bytes; compression rates vary with the picture but a typical value is about 1/5th of the size, i.e., 1-2 bits/pixel instead of 8 bits/pixel. So we can compress a typical 100 kbyte image to about 20 kbytes, where a kilobyte means about 1,000 bytes.

Image formats

An image format is a specific way to represent the image. The three most common images formats used in the internet are GIF, JPG and PNG. One of the ways in which they are different from each other is whether they are ‘lossless’ or ‘lossy’.

‘Lossless’ compression techniques can recover exactly the image that was compressed, with no loss of image information, while ‘lossy’ compression takes advantage of the limitations of the human eye to push compression further, at the expense of not recovering the original image but one that, to our eyes, is indistinguishable from the original. Examples of lossless compression image formats are GIF (Graphics Interchange Format), and PNG (Portable Network Graphics), while the JPEG (Joint Photographic Experts Group) is an example of lossy compression.

Since lossless compression preserves the original image intact, it is often used to compress simple images in which we could detect defects easily, e.g., logos, cartoons, simple drawings, etc. Lossy compression is used to compress images in which it would be more difficult for us to detect defects, like photographs.


Good candidate for lossless compression, e.g., GIF or PNG


Good candidate for lossy compression, e.g., JPG or PNG


Transparency

An image has transparency when we can set some of their pixels as ‘see-through’. We say that these ‘transparent’ pixels belong to the ‘alpha’ channel. Let’s see what a big difference being transparent makes with our logo: the image on the left does not have transparency, while that on the right does.

left: JPG; right: PNG, or GIF

Transparency is useful in many situations, specially in games where we want spacecrafts and people to move over complex backgrounds, and blend with them seamlessly. Image transparency is not a setting in CSS but a property of the format of the image. Well… GIFs and PNGs support transparency but JPGs do not. PNGs and GIFs do not have to have an alpha channel, but they can if we need to, while JPGs just can’t have an alpha channel. The price to pay for having an alpha channel is a slight increase in the size of the image; after all, the alpha channel adds to the normal three RGB channels, so the size of a transparent image is always larger than that of its corresponding non-transparent version.

PNG: The winning format

At this moment, the format that appears to have gained the favor of the internet developers is PNG. PNG is a good compromise of size and features. It is lossless so it works well with both drawings and photographs, and supports transparency, which JPEG cannot do. A new format, called Jpeg-2000, has transparency but it didn’t become popular so few applications support it. In summary:

Format Drawings Photographs Transparency Popular
GIF Yes No Yes Yes
JPEG Yes Yes No Yes
Jpeg-2000 Yes Yes Yes No
PNG Yes Yes Yes Yes

So, in the end, the internet is now dominated by PNGs and JPGs, but JPGs are slowly losing their importance as PNGs becomes more and more popular. Basically, PNGs has all the advantages of GIFs and JPEGs, while the price to pay is only a small increase in the image size.

Image resizing in CSS

Adjusting the size of an image in CSS is easy, simply setting the element’s width and height properties. If we do not specify the width or height of an image, the browser draws the original image, in this case with 400 columns and 266 rows:

We could force the browser to scale the image into a given number of columns and rows, even if the result is not proportional to the original image:

Still, the best way to scale an image is to make a copy of the image with the size that we want and then load the rescaled image; telling the browser the size of the rescaled image also helps it to separate the right amount of memory for the image even before it is retrieved from the server. For example, ‘dog-300×200.png’ is a scaled version of the original ‘dog.png’ with 300 columns and 200 rows; let’s give all that information to the browser:

The following is the script with our example:

that we can see in action here.

Now let’s continue with one of the main ingredients of style: color.