Package: simplejpeg
This project is in no way affiliated with thelibjpeg-turboproject.simplejpegsimplejpeg 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:Pillow isveryslow compared to OpenCV.Pillow only accepts streams as input. Images in memory
have to be wrapped inBytesIOor similar.
This adds to the slowness.OpenCV is gigantic,
only accepts Numpy arrays as input,
and returns images as BGR instead of RGB.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:Speed.Read and write directly from/to memory.Advanced features of the underlying library.InstallationOn Linux (x86/x64), Windows (x86/x64), or MacOS (10.9+, x64)
you can simplypip install simplejpeg.
Updatepipif it wants to build from source anyway.On other platforms you can try to install from source.
Make sure your system is setup to build CPython extensions
and installcmake >= 2.8.12.
Then runpip install simplejpegto install from source.You can also runpython setup.py bdist_wheeletc. as usual.UsageThis 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 thebuffer protocol,
likearray,bytes,bytearray,memoryview, etc.decode_jpeg_headerdecode_jpeg_header(
    data: Any,
    min_height: SupportsInt=0,
    min_width: SupportsInt=0,
    min_factor: SupportsFloat=1,
    strict: bool=True,
) -> (SupportsInt, SupportsInt, Text, Text)Decode only the header of a JPEG image given as JPEG (JFIF) data from memory.
Accepts any input that supports thebuffer 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 ignoredmin_width:
minimum width in pixels of the decoded image;
values <= 0 are ignoredmin_factor:
minimum downsampling factor when decoding to smaller size;
factors smaller than 2 may take longer to decodestrict:
if True, raise ValueError for recoverable errors;
default Truereturns:(height: int, width: int, colorspace: str, color subsampling: str)decode_jpegdef 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,
    strict: bool=True,
) -> np.ndarrayDecode a JPEG image given as JPEG (JFIF) data from memory.
Accepts any input that supports thebuffer 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 spacefastdct:
if True, use fastest DCT method;
speeds up decoding by 4-5% for a minor loss in qualityfastupsample:
if True, use fastest color upsampling method;
speeds up decoding by 4-5% for a minor loss in qualitymin_height:
minimum height in pixels of the decoded image;
values <= 0 are ignoredparam min_width:
minimum width in pixels of the decoded image;
values <= 0 are ignoredparam min_factor:
minimum downsampling factor when decoding to smaller size;
factors smaller than 2 may take longer to decodebuffer:
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 sizestrict:
if True, raise ValueError for recoverable errors;
default Truereturns: image asnumpy.ndarrayencode_jpegdef encode_jpeg(
        image: numpy.ndarray,
        quality: SupportsInt=85,
        colorspace: Text='RGB',
        colorsubsampling: Text='444',
        fastdct: Any=True,
) -> bytesEncode an image given as numpy array to JPEG (JFIF) string.
Returns JPEG (JFIF) data.image:
uncompressed image as uint8 arrayquality:
JPEG quantization factor;
0-100, higher equals better qualitycolorspace:
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 differencereturns:bytesobject of encoded image as JPEG (JFIF) datais_jpegdef is_jpeg(data: SupportsBytes)Check whether a bytes object (or similar) contains JPEG (JFIF) data.data: JPEG (JFIF) datareturns: True if JPEG