39.12. jpeg — Read and write JPEG files — Python 2.7.18 documentation

This document is for an old version of Python that is no longer supported. You should upgrade and read the Python documentation for the current stable release.

# 39.12. `jpeg` — Read and write JPEG files

Deprecated since version 2.6: The `jpeg` module has been removed in Python 3.

The module `jpeg` provides access to the jpeg compressor and decompressor written by the Independent JPEG Group (IJG). JPEG is a standard for compressing pictures; it is defined in ISO 10918. For details on JPEG or the Independent JPEG Group software refer to the JPEG standard or the documentation provided with the software.

A portable interface to JPEG image files is available with the Python Imaging Library (PIL) by Fredrik Lundh. Information on PIL is available at http://www.pythonware.com/products/pil/.

The `jpeg` module defines an exception and some functions.

exception `jpeg.` `error`

  Exception raised by `compress()` and `decompress()` in case of errors.

`jpeg.` `compress`(data, w, h, b)

  Treat data as a pixmap of width w and height h, with b bytes per pixel. The data is in SGI GL order, so the first pixel is in the lower-left corner. This means that `gl.lrectread()` return data can immediately be passed to `compress()`. Currently only 1 byte and 4 byte pixels are allowed, the former being treated as greyscale and the latter as RGB color. `compress()` returns a string that contains the compressed picture, in JFIF format.

`jpeg.` `decompress`(data)

  Data is a string containing a picture in JFIF format. It returns a tuple `(data, width, height, bytesperpixel)`. Again, the data is suitable to pass to `gl.lrectwrite()`.

`jpeg.` `setoption`(name, value)

  Set various options. Subsequent `compress()` and `decompress()` calls will use these options. The following options are available:

  Option | Effect
  --- | ---
  `'forcegray'` | Force output to be grayscale, even if input is RGB.
  `'quality'` | Set the quality of the compressed image to a value between `0` and `100` (default is `75`). This only affects compression.
  `'optimize'` | Perform Huffman table optimization. Takes longer, but results in smaller compressed image. This only affects compression.
  `'smooth'` | Perform inter-block smoothing on uncompressed image. Only useful for low- quality images. This only affects decompression.

See also

JPEG Still Image Data Compression Standard

  The canonical reference for the JPEG image format, by Pennebaker and Mitchell.

Information Technology - Digital Compression and Coding of Continuous-tone Still Images - Requirements and Guidelines

  The ISO standard for JPEG is also published as ITU T.81. This is available online in PDF form.