[Image-SIG] Question about Thumbnail-Mode.

Fredrik Lundh fredrik at pythonware.com
Thu Mar 9 14:48:22 CET 2006

Dieter Krachtus wrote:

> I am in science and want to understand certain things PIL does better...
> One can get jpg-thumbnails of images with PIL. This happens pretty fast
> - so PIL cannot read the complete file, but use some trick. I guess it
> just reads in a 2^x (i.e. 1/2, 1/4, 1/8, 1/16, ...) of the actual
> image-data. Just to understand how this is done, is my qustion:
> 1. Does PIL look into the "header" of the jpg file and finds out from
> where to where the actual image data goes and how the dimension
> (width/height in pixel) of the image is ?


> 2. Then whats next? Does PIL read only e.g. every 16th byte, or how is
> this done - if you do this - is the resulting stream of bytes still
> interpretable as a jpg ?

it's a lot tricker than that: the JPEG data stream represents image data not as
a bunch of separate pixels, but as a set of frequency coefficients, which, when
combined, can be used to reconstruct the original image (in pretty much the
same way as you can generate a square wave by summing enough sine waves;
see http://en.wikipedia.org/wiki/Square_wave).

the frequency transform is made on 8x8 tiles, so you get a distinct set of
coefficients for each 8x8 box in the source image.

(to get lossy compression, JPEG simply uses fewer bits to store the coefficients
than they really need; e.g. instead of storing a full range of, say, 0-1000 for a
high-frequency coefficient, the file may only use 0, 300, 600, and 900.  this
lets you store the coefficient in 2 bits instead of 10.)

to quickly generate thumbnails from JPEG images from the coefficients, PIL
simply ignores some of the coefficients.  to generate an 1/8 image, PIL uses
the "DC component" from the frequency transform directly (which corre-
sponds to the average value for the entire 8x8 box).  for 1/2 and 1/4 images,
PIL only enough frequency components to generate a 4x4 or 2x2 tile.

there's a lot more to JPEG than I've described here; if you want to learn more
about JPEG, the wikipedia entry is quite good:


hope this helps!


More information about the Image-SIG mailing list