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,
)

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=True,
    fastupsample: Any=True,
    min_height: SupportsInt=0,
    min_width: SupportsInt=0,
    min_factor: SupportsFloat=1,
)

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; usually no observable difference

  • fastupsample: if True, use fastest color upsampling method; usually no observable difference

  • 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

  • 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,
)

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

  • image: uncompressed image as numpy 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.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.2.6.tar.gz (1.3 MB view details)

Uploaded Source

Built Distributions

simplejpeg-1.2.6-cp37-cp37m-manylinux1_x86_64.whl (273.0 kB view details)

Uploaded CPython 3.7m

simplejpeg-1.2.6-cp37-cp37m-manylinux1_i686.whl (267.5 kB view details)

Uploaded CPython 3.7m

simplejpeg-1.2.6-cp36-cp36m-manylinux1_x86_64.whl (273.1 kB view details)

Uploaded CPython 3.6m

simplejpeg-1.2.6-cp36-cp36m-manylinux1_i686.whl (267.7 kB view details)

Uploaded CPython 3.6m

simplejpeg-1.2.6-cp35-cp35m-manylinux1_x86_64.whl (271.6 kB view details)

Uploaded CPython 3.5m

simplejpeg-1.2.6-cp35-cp35m-manylinux1_i686.whl (266.4 kB view details)

Uploaded CPython 3.5m

File details

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

File metadata

  • Download URL: simplejpeg-1.2.6.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.2.6.tar.gz
Algorithm Hash digest
SHA256 a6a84a469706b28ad8d5cb3f2d09ec8c8c7363173bcff8270a08a511c78a5752
MD5 767a20c0c1f207323f08d8266dcef339
BLAKE2b-256 275732a198dc0f8b60b42910a9a22491c6855d8a66ca5fbec2d292cdc6213260

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.2.6-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 273.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.2.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 75eb97d90903452b4f038efd79dff2815ad94f991858bc36e285a84aa54bcb42
MD5 a2277d81168ce0af4a702916b93c5a8d
BLAKE2b-256 efd7a293c93a8d0adf5a741dd3212f88d8496de6b502fdf21d7638486b8ebf87

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.2.6-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 267.5 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.2.6-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 41a1be847ddd0c9fff2bed76ccf627fdcade8c35c4db7aa162ca39ded7e30a63
MD5 139a4ad5a2c07d397cc56b5540d6fd6d
BLAKE2b-256 9f3e0d55abc4195daf6a9eda365512e2825572d4267ffa9bc882985709521fe6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.2.6-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 273.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.2.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0b80a59045d77e43593896cf5eaff4b3a7a2cb4307ed8b7653b152ea8d6b6023
MD5 1ad6c2739946ab39dfb27ec06cd5806d
BLAKE2b-256 8874abb6a045891738384b545419a962a264091f222ed2fed0048b99d5253fd9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.2.6-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 267.7 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.2.6-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 f2570dbed6ea7343cee27388830a2ca4a5950cd85752eb6235d8e77354b1f6a6
MD5 515c5284d2385664d53afbdea9cb2563
BLAKE2b-256 28c5372ae6272be4543600bd143ec89b7357854d024734f5c84be3e488e52d60

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.2.6-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 271.6 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.2.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 34ee5a2028904652d8a724b64a7c5294970c423855e03a74971b245e510f052c
MD5 179cb963443562fdea10f6b0c6d1b3c9
BLAKE2b-256 1eb52bd253e0c28e3b4994c0678e04e2d9adac7c8472e3b1a0e4dc69e7e017bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplejpeg-1.2.6-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 266.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.2.6-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 f8642d3a1777904f143455b2af213c632cb617a4264e060869ef18daa008eac7
MD5 23dd8aa5e8330bb405522a1f9d070db9
BLAKE2b-256 9361553186e4050ce39896ac9853772baff3667bcd0247b1c8641e3a60df052b

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