reencoding->recompression
[PyX/mjg.git] / examples / bitmap / jpeg.txt
blob5680151b72fb66b5e60e485124def87107b435be
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 Of course you could than turn on the default gzip based compression by
28 omitting the 'compressmode=None'. This will reduce the filesize down
29 to about 727KB. Still much, much larger than the version we get by the
30 jpeg compression.
32 Since the image instance is a PIL instance we can also use
33 `compressmode="DCT"`, which turns on the loosy jpeg compression
34 method. This will restore a file size similar to what we get by using
35 the `jpegimage` instance. The file size and quality of the loosy
36 compression can be adjusted by the `dctquality`, `dctoptimize`, and
37 `dctprogression` parameters of the `bitmap` constructor. But it is
38 important to note that you will always get some additional artifacts
39 due to the recompression by the loosy jpeg compression method. It is
40 also important to note that the original version shown in this example
41 (using `jpegimage`) does not use the PIL at all.