some updates to the webpage
[PyX.git] / examples / bitmap / jpeg.txt
blobeb14485c44ccdb28be2ae3dc98b3735ef82c3b1a
1 Insert JPEG images without and with recompression
3 With the `bitmap.jpegimage` class, PyX allows you to directly insert a
4 JPEG image into a canvas. It extracts the compressed JPEG data and makes
5 the data available to a PyX bitmap without recompression (i.e. without loss
6 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. If you forget to do so, PyX will fail and report about
11 this mistake. (The original source of the problem is that PyX tries to
12 compress all images using the gzip method by default and you need to
13 turn off this feature to prevent the data from being double compressed.)
15 Since we have some image data in this example, let us also discuss how to use
16 the [Python Imaging Libary PIL http://www.pythonware.com/products/pil/] to load
17 the data and write it to the file. The straightforward solution would be to
18 replace the creation of the `bitmap.jpegimage` instance by:
20     import Image
21     i = image.open("jpeg.jpg")
23 While this works perfectly, it will result in a totally uncompressed
24 image. The size of the EPS file will for example become almost 1.1MB.
26 In a next step, you may turn on the default gzip-based compression by
27 omitting the 'compressmode=None' (or by setting `compressmode="Flate"`
28 explicitely). This will reduce the file size down to about 727KB (EPS)
29 which is still much larger than the version in the above example which
30 used the JPEG compression. Note that this does not mean that the gzip
31 based compression method is bad in general - it is just bad compared to
32 the JPEG method for certain kind of image data like photos.
34 Finally, since the image instance is a PIL instance, we can also use
35 `compressmode="DCT"`, which turns on the lossy JPEG compression method.
36 This will restore the file size to similar values as we got previously by using
37 the `bitmap.jpegimage` instance. The file size and quality of the loosy
38 compression can be adjusted by the `dctquality`, `dctoptimize`, and
39 `dctprogression` parameters of the `bitmap` constructor. Nevertheless, it is
40 important to note that you will always get some additional artifacts due
41 to the recompression by the lossy JPEG compression method. It is also
42 important to note that the original version shown in this example (using
43 `bitmap.jpegimage`) does not use the PIL at all.