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.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.0.tar.gz (1.3 MB view details)

Uploaded Source

Built Distributions

simplejpeg-1.3.0-cp37-cp37m-manylinux1_x86_64.whl (277.0 kB view details)

Uploaded CPython 3.7m

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

Uploaded CPython 3.7m

simplejpeg-1.3.0-cp36-cp36m-manylinux1_x86_64.whl (277.4 kB view details)

Uploaded CPython 3.6m

simplejpeg-1.3.0-cp36-cp36m-manylinux1_i686.whl (271.1 kB view details)

Uploaded CPython 3.6m

simplejpeg-1.3.0-cp35-cp35m-manylinux1_x86_64.whl (275.4 kB view details)

Uploaded CPython 3.5m

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

Uploaded CPython 3.5m

File details

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

File metadata

  • Download URL: simplejpeg-1.3.0.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.0.tar.gz
Algorithm Hash digest
SHA256 de91ef8aa57d124505b025de95f5357ab8c5d037298075507955786c47e9ad5f
MD5 a80188a30ed64a42c6d585e16780ee23
BLAKE2b-256 19d296d864954cde3e6d72dcd51f9dbbe413aa50e25a443437fb0a5419d42266

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.3.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 277.0 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.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4dd2ff85937faaa703a424c6454bdaf236c46c555ed1a4370d703f5398f3bca9
MD5 dae25a6a739b46b86a9110b6c79fc13d
BLAKE2b-256 de83a53ba4ddecb02d24e8e8827ab19421934e9b4274dd047b3c1fc2e72ead05

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.3.0-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.0-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 f3d327e5d910fa425b486057c795474e7ed89a9cb08b19416a49a3be5bec4672
MD5 b2ee4232922f32c4a7b8c3700554fb90
BLAKE2b-256 7aa95d293dd874ca6e51bd25652784f44c6fee8452cf782904f35199bf2a3ab5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.3.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 277.4 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.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d363763fbe79e238fe8358d310aee84d42d917697162b49c3affdf5952686543
MD5 27037f14c515a09e77a06306ce6fb8cb
BLAKE2b-256 9f05cb27e521d5300047bbbd7f86ea6b4c1abc4224e4d36d85bb2c60a560002c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.3.0-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 271.1 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.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 9402b7cbc270f4e2085e655d0ad44ffb12eba0b67f3578e521b67d438df86f74
MD5 43ee26711f633653eae8f54220f2b954
BLAKE2b-256 9ec1879dfc0abbbe7d0da89eed97f2293f87b773d7d0385889f2cb5699d9d9f4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.3.0-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 275.4 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.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ae1e37d46e6ceba880746cc84023cc250df9f1069b0b28a0a600ba9ddad35bc8
MD5 4665e2afd4361134eebf01354fa1a723
BLAKE2b-256 706acc146504254dea69041f1d9ba7cb80e9eff6e4e91e35e266dbf8676a0ec7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.3.0-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.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6b7406b24128f4e748d5876380d0ea4034e6ac74161665d82558d58c16d853a7
MD5 0f102c7f7561117d234e1479cdd979ea
BLAKE2b-256 b70ba8ddfa51a9ae5d7a9b57201a9e95b88ce8421f6d0999100cc21f6c547e2c

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