Skip to main content

A simple package for fast JPEG encoding and decoding.

Project description

This project is in no way affiliated with the libjpeg-turbo project.

simplejpeg

simplejpeg is a simple package based on recent versions of libturbojpeg for fast JPEG encoding and decoding.

Why another library?

Pillow and OpenCV are excellent options for handling JPEG images and a variety of other formats.

If all you want is to read or write a couple of images and don’t worry about the details, this package is not for you.

Keep reading if you care about speed and want more control over how your JPEGs are handled.

These are the reasons why I started making this:

  1. Pillow is very slow compared to OpenCV.

  2. Pillow only accepts streams as input. Images in memory have to be wrapped in BytesIO or similar. This adds to the slowness.

  3. OpenCV is gigantic, only accepts Numpy arrays as input, and returns images as BGR instead of RGB.

  4. Recent versions of libturbojpeg offer impressive speed gains on modern processors. Linux distributions and libraries tend to ship very old versions.

This library is especially for you if you need:

  1. Speed.

  2. Read and write directly from/to memory.

  3. Advanced features of the underlying library.

Usage

This library provides four functions:

decode_jpeg_header, decode_jpeg, encode_jpeg, is_jpeg.

Uncompressed image data is stored as numpy arrays. Decoding functions can accept any Python object that supports the buffer protocol, like array, bytes, bytearray, memoryview, etc.

decode_jpeg_header

decode_jpeg_header(
    data: Any,
    min_height: SupportsInt=0,
    min_width: SupportsInt=0,
    min_factor: SupportsFloat=1,
) -> (SupportsInt, SupportsInt, Text, Text)

Decode only the header of a JPEG image given as JPEG (JFIF) data from memory. Accepts any input that supports the buffer protocol. This is very fast on the order of 100000+ images per second. Returns height and width in pixels of the image when decoded, and colorspace and subsampling as string.

  • data: JPEG data in memory; must support buffer interface (e.g., bytes, memoryview)

  • min_height: minimum height in pixels of the decoded image; values <= 0 are ignored

  • min_width: minimum width in pixels of the decoded image; values <= 0 are ignored

  • min_factor: minimum downsampling factor when decoding to smaller size; factors smaller than 2 may take longer to decode

  • returns: (height: int, width: int, colorspace: str, color subsampling: str)

decode_jpeg

def decode_jpeg(
    data: SupportsBuffer,
    colorspace: Text='RGB',
    fastdct: Any=False,
    fastupsample: Any=False,
    min_height: SupportsInt=0,
    min_width: SupportsInt=0,
    min_factor: SupportsFloat=1,
    buffer: SupportsBuffer=None,
) -> np.ndarray

Decode a JPEG image given as JPEG (JFIF) data from memory. Accepts any input that supports the buffer protocol. Returns the image as numpy array in the requested colorspace.

  • data: JPEG data in memory; must support buffer interface (e.g., bytes, memoryview)

  • colorspace: target colorspace, any of the following: ‘RGB’, ‘BGR’, ‘RGBX’, ‘BGRX’, ‘XBGR’, ‘XRGB’, ‘GRAY’, ‘RGBA’, ‘BGRA’, ‘ABGR’, ‘ARGB’; ‘CMYK’ may only be used for images already in CMYK space

  • fastdct: if True, use fastest DCT method; speeds up decoding by 4-5% for a minor loss in quality

  • fastupsample: if True, use fastest color upsampling method; speeds up decoding by 4-5% for a minor loss in quality

  • min_height: minimum height in pixels of the decoded image; values <= 0 are ignored

  • param min_width: minimum width in pixels of the decoded image; values <= 0 are ignored

  • param min_factor: minimum downsampling factor when decoding to smaller size; factors smaller than 2 may take longer to decode

  • buffer: use given object as output buffer; must support the buffer protocol and be writable, e.g., numpy ndarray or bytearray; use decode_jpeg_header to find out required minimum size

  • returns: image as numpy.ndarray

encode_jpeg

def encode_jpeg(
        image: numpy.ndarray,
        quality: SupportsInt=85,
        colorspace: Text='RGB',
        colorsubsampling: Text='444',
        fastdct: Any=True,
) -> bytes

Encode an image given as numpy array to JPEG (JFIF) string. Returns JPEG (JFIF) data.

  • image: uncompressed image as uint8 array

  • quality: JPEG quantization factor; 0-100, higher equals better quality

  • colorspace: source colorspace; one of ‘RGB’, ‘BGR’, ‘RGBX’, ‘BGRX’, ‘XBGR’, ‘XRGB’, ‘GRAY’, ‘RGBA’, ‘BGRA’, ‘ABGR’, ‘ARGB’, ‘CMYK’

  • colorsubsampling: subsampling factor for color channels; one of ‘444’, ‘422’, ‘420’, ‘440’, ‘411’, ‘Gray’.

  • fastdct: If True, use fastest DCT method; usually no observable difference

  • returns: bytes object of encoded image as JPEG (JFIF) data

is_jpeg

def is_jpeg(data: SupportsBytes)

Check whether a bytes object (or similar) contains JPEG (JFIF) data.

  • data: JPEG (JFIF) data

  • returns: True if JPEG

Changelog

1.3.1

  • decode_jpeg requests writable buffer from buffer argument.

1.3.0

  • decode_jpeg accepts optional buffer argument.

1.2.6

  • Fix memory corruption caused by incorrect GIL handling.

1.2.5

  • Initial public release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simplejpeg-1.3.1.tar.gz (1.3 MB view details)

Uploaded Source

Built Distributions

simplejpeg-1.3.1-cp37-cp37m-manylinux1_x86_64.whl (277.1 kB view details)

Uploaded CPython 3.7m

simplejpeg-1.3.1-cp37-cp37m-manylinux1_i686.whl (270.7 kB view details)

Uploaded CPython 3.7m

simplejpeg-1.3.1-cp36-cp36m-manylinux1_x86_64.whl (277.5 kB view details)

Uploaded CPython 3.6m

simplejpeg-1.3.1-cp36-cp36m-manylinux1_i686.whl (271.2 kB view details)

Uploaded CPython 3.6m

simplejpeg-1.3.1-cp35-cp35m-manylinux1_x86_64.whl (275.5 kB view details)

Uploaded CPython 3.5m

simplejpeg-1.3.1-cp35-cp35m-manylinux1_i686.whl (269.9 kB view details)

Uploaded CPython 3.5m

File details

Details for the file simplejpeg-1.3.1.tar.gz.

File metadata

  • Download URL: simplejpeg-1.3.1.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.0

File hashes

Hashes for simplejpeg-1.3.1.tar.gz
Algorithm Hash digest
SHA256 1092ff4bce603b7f655e3259dcd73a520a20d972c50723c8f33fdc06dc34102a
MD5 077653890e3526adfc4ecb6c2f4529a5
BLAKE2b-256 26bd0080a7b3fb718630caa961104c738268786953c90976a7724d5d4ae5a5f2

See more details on using hashes here.

File details

Details for the file simplejpeg-1.3.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: simplejpeg-1.3.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 277.1 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.0

File hashes

Hashes for simplejpeg-1.3.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b8bb30b8e8f819ec61dab680e288fd51af8debf93fae36fa43d33914c8391249
MD5 1c09b06c7f8ef089535fc06943c517ff
BLAKE2b-256 a9623364bcabd399327323df3d405195d6e3a122a7210ede5fba7d56c7ad911d

See more details on using hashes here.

File details

Details for the file simplejpeg-1.3.1-cp37-cp37m-manylinux1_i686.whl.

File metadata

  • Download URL: simplejpeg-1.3.1-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 270.7 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.0

File hashes

Hashes for simplejpeg-1.3.1-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 11a647ece79eacfceec2aaf547a1331908672f21bf2da60d497cbbf4732861e1
MD5 7659e6e53d9ffd62d6d2658d18e6c0b3
BLAKE2b-256 c11114e4728f8e5e140e8710a929905c389e9e923ce5c4c48b52a4778e10407b

See more details on using hashes here.

File details

Details for the file simplejpeg-1.3.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: simplejpeg-1.3.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 277.5 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.0

File hashes

Hashes for simplejpeg-1.3.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3ae697e56fe07bb503aa3df53201302eed0445ccd1134d98732c9650f4d0ed45
MD5 45e2d32773b3673a590a837804448ffe
BLAKE2b-256 6724a9de1fa5baa2e60505ea9bcf7ff3db4f6dcdc8bf08dc3bd8bef10d6bc42f

See more details on using hashes here.

File details

Details for the file simplejpeg-1.3.1-cp36-cp36m-manylinux1_i686.whl.

File metadata

  • Download URL: simplejpeg-1.3.1-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 271.2 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.0

File hashes

Hashes for simplejpeg-1.3.1-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 a2943894dc56f0ee6308c1701da66825b980761595aa4126f36cde3481269bf3
MD5 a04416339fa53a0f621886f42b1cb427
BLAKE2b-256 f27e406ae050db955703eedad1a1d9d823d71c03caeccbb8208dcdd0a73e02f1

See more details on using hashes here.

File details

Details for the file simplejpeg-1.3.1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: simplejpeg-1.3.1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 275.5 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.0

File hashes

Hashes for simplejpeg-1.3.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d7e92b2ee849f2772c030f7961c0232d66a9bf63031a3029f4e4e90c5376e1af
MD5 99794510148e1c490678b57f373dc083
BLAKE2b-256 aa5388fe5e72d8646c0df11732fd885b2133bc7157a077b8547d24e6a50a5235

See more details on using hashes here.

File details

Details for the file simplejpeg-1.3.1-cp35-cp35m-manylinux1_i686.whl.

File metadata

  • Download URL: simplejpeg-1.3.1-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 269.9 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.0

File hashes

Hashes for simplejpeg-1.3.1-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 168c22e7e33dd5088183ddb6c5110cc61bb5f3bed91d51f0c3ce063dbe8336f6
MD5 cc21dd271af20f0f1876fb6e44aae09f
BLAKE2b-256 6b553da137ba8e7959ca7da943e3173697aa1f42d41a2f3ce34d2ba21aeb7df8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page