add some remarks about the 'bad' flate compression
[PyX/mjg.git] / examples / bitmap / jpeg.txt
blob70863e65a272d5c37910b7e61f5575dba335b480
1 Insert jpeg images without recompression
3 You can insert a jpeg image directly in PyX by the jpegimage
4 class. It extracts the compressed jpeg data and makes the
5 data available to a PyX bitmap without recompression (i.e.
6 no loss of quality). ...
8 ! Note that you need to set `compressmode` to `None` when creating the
9 bitmap instance, since the data provided by the image instance `i` is
10 already compressed. However, PyX will fail and report about this
11 mistake in case you forget disable the compression. (The original
12 source of the problem is that PyX tries to compress all images using
13 the gzip method by default and you need to turn off this feature to
14 prevent the data from being double compressed.)
16 Since we have some image data in this example, let us also discuss how
17 to use the PIL to load such data and write it to the file. The
18 straight forward solution would be to replace the creation of the
19 `bitmap.jpegimage` instance by:
21     import Image
22     i = image.open("jpeg.jpg")
24 While this works perfectly, it'll result in a totally uncompressed
25 image. The size of the EPS file will for example become almost 1.1MB.
27 In a next step you may turn on the default gzip based compression by
28 omitting the 'compressmode=None' (or setting `compressmode="Flate"`
29 explicitely). This will reduce the file size down to about 727KB (EPS)
30 which is still much, much larger than the version we get by the jpeg
31 compression. Note that this does not mean that the gzip based
32 compression method is bad in general - it is just bad compared to the
33 jpeg method for certain kind of image data like photos.
35 Since the image instance is a PIL instance we can also use
36 `compressmode="DCT"`, which turns on the loosy jpeg compression
37 method. This will restore a file size similar to what we get by using
38 the `jpegimage` instance. The file size and quality of the loosy
39 compression can be adjusted by the `dctquality`, `dctoptimize`, and
40 `dctprogression` parameters of the `bitmap` constructor. But it is
41 important to note that you will always get some additional artifacts
42 due to the recompression by the loosy jpeg compression method. It is
43 also important to note that the original version shown in this example
44 (using `jpegimage`) does not use the PIL at all.