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:
Pillow is very slow compared to OpenCV.
Pillow only accepts streams as input. Images in memory have to be wrapped in BytesIO or 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.
Installation
On Linux (x86/x64), Windows (x86/x64), or MacOS (10.9+, x64) you can simply pip install simplejpeg. Update pip if 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 install cmake >= 2.8.12. Then run pip install simplejpeg to install from source.
You can also run python setup.py bdist_wheel etc. as usual.
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, 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 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
strict: if True, raise ValueError for recoverable errors; default True
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, strict: bool=True, ) -> 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
strict: if True, raise ValueError for recoverable errors; default True
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for simplejpeg-1.7.4-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ac0f09f80ee9b9221f6b4be93d07aa174e43f67e30e723ed38a4733859f05d5 |
|
MD5 | 78baaf41cce0a8cb559047383b738f80 |
|
BLAKE2b-256 | acf4d94a58ea4fba117b04aec06f69b34bffcbb54268862bf3f20e4d657010c3 |
Hashes for simplejpeg-1.7.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac39df39a3420b42047849767a4a5836fe86e0c9193855afc9024c4f924cfbf5 |
|
MD5 | a334954e5bd76643cbd7348018773eec |
|
BLAKE2b-256 | 275df188619205cca3410659be819d5132a41505a2b4f4ec242d2aaeb3d87c2b |
Hashes for simplejpeg-1.7.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5edc15e5b41c799d0b92bd0d4d3f75b941e7c40e624555a4201a1094a8419801 |
|
MD5 | 8b5af0313b033343814c10effabdd019 |
|
BLAKE2b-256 | e6af900aeb8dc84b33472c23b20cb0d0881da54e397211cf8547c206f36d5a01 |
Hashes for simplejpeg-1.7.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5091730c9324a2a0c4b6f73282d5502362d4fa8b2c43f903b4b7898836fb64f |
|
MD5 | 6a55da2fc320be2e878dc89d6ff01a76 |
|
BLAKE2b-256 | efb97a0c6966bf6645f9ed1a54d93e1a424ad5d1a344d82ee2a3ddf3fc8d1321 |
Hashes for simplejpeg-1.7.4-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8354a21576051b408f64039ecb0dbceab5668e16c2694862ae948e4619a5aaca |
|
MD5 | 474b3a41589efe595ad26ae592edfa55 |
|
BLAKE2b-256 | 907946ddebe5fc845dd886334f4ba91c1fd98ea7efb13633f833103561d3cd1c |
Hashes for simplejpeg-1.7.4-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c142bc5d5ef779a2940b7665e94e7398fb16d962b8a8cfad524cfddecf870a0 |
|
MD5 | ebfb1a624c8c2a35ed2ff67af78402a8 |
|
BLAKE2b-256 | 295c9173f400747bc13de928d062c7d13d609536ce6ce4ea13a0750ffe67e398 |
Hashes for simplejpeg-1.7.4-cp311-cp311-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec4aea2abd9de0cc22e51738c29222b6ae2aa0ac2f3c3c09d0a5f4bbb7ffb340 |
|
MD5 | 0bc3c5aad21bece98b512b464311acc4 |
|
BLAKE2b-256 | e33a3140184a68d8ffda5f3cf44f0e55cea874190e49866bf885f2c090e29121 |
Hashes for simplejpeg-1.7.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3af458472684397a180693108fe4e2bbe00f1a1d137fce04a0198828a42824d |
|
MD5 | efb16e11d3f265e783036e6dca0998e4 |
|
BLAKE2b-256 | 9aed744fec5fdc8629cc9caae645097ce54aa10fbadb6a2b50d709abd2e159c4 |
Hashes for simplejpeg-1.7.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55e26606b39766d4f0bb8ca81eba5ac063064ca81d608a1066388b573e2d8354 |
|
MD5 | 5c37ec6f9b59c2394fadea2520d76a54 |
|
BLAKE2b-256 | 39f8ee6f29733d20ade5dc5cb7ed0c1ae19a4f703841a590df8645384b8322ba |
Hashes for simplejpeg-1.7.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90e0d482d26508d1f2183bd3c86c2a04fa5a9038c1eae902a4292124b04a3415 |
|
MD5 | 2fd62c998271715aa3296a3d2d7cec52 |
|
BLAKE2b-256 | 6941d71cf61d5fc5916462e1a113a7514161fd5c5efa900d3519250117e2bcea |
Hashes for simplejpeg-1.7.4-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94fb8620d949a66c5ac015bb0d41ad44bd2bd432e2e3c13d658873291e82b6c8 |
|
MD5 | 406ed24e262a114128b6cb7f52b93773 |
|
BLAKE2b-256 | 34cc0f6142f03348b81e8ffb9b1bdcdb5d06d964f65db8261228c0292f68b318 |
Hashes for simplejpeg-1.7.4-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69835de09e6dfdc6c9cbce1c2055d80a7ff1f6be6c1955645cd2c4ac2e199390 |
|
MD5 | 9c5b63b8f25d694e1292d361ed228b56 |
|
BLAKE2b-256 | d2dfe14b2d118931c787b40727cca5b7b651f3af22f4a6a2f1e2be0926a119e0 |
Hashes for simplejpeg-1.7.4-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39f4d6f750ceff16aefefd5a89533a6e330da3d09ce8d6dcd85cfcc0dbb3bb75 |
|
MD5 | a06bfaa37118c113e2e643b1605cd574 |
|
BLAKE2b-256 | 96212055c8e1fd8e9e34833366c7a31e769f4028053315436497368be84d8565 |
Hashes for simplejpeg-1.7.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 168eb0242315cfcc4758667c2ab09f272130d47c5a0bdeb22f6db3c376cd017a |
|
MD5 | 8712529c97f9d9ae41a6ace35a7eb1f2 |
|
BLAKE2b-256 | c52449fba09a652d736c31fe58cad7bf1f1215468308ba4d8e5757668af14ec9 |
Hashes for simplejpeg-1.7.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3df7ec75e0f4ce38e7e3fcb5f1fb602f4c6fb4ef09f936b00960b833b160e3b9 |
|
MD5 | 64280df9412fbad00b22651806e9c266 |
|
BLAKE2b-256 | f772cf017249280e2e0e3718b6bb8349979d5dc0bd08dc38fb6adeb709de8bdf |
Hashes for simplejpeg-1.7.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2cc123d7f3ea8415204a02902f93164bbce13f79e11386316d1df342aa9c11b6 |
|
MD5 | 05ca1cdbd8f66ab61f6e3c1108a5fe59 |
|
BLAKE2b-256 | 5a9b53c446f412e2d0fa7c60e4d5ec5219cd775f2fc050cb404fbe5974f2df94 |
Hashes for simplejpeg-1.7.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c62498a92e591330a270d440bd0e855846ee0f64445ba6b95b5a990107acc9b5 |
|
MD5 | 0f2fb3b14e723c85eb4c64cb2ba7eb8e |
|
BLAKE2b-256 | 3aa4900df49b7b10b73e4473eb68b5850ccacc335a10b5df81052637a0720001 |
Hashes for simplejpeg-1.7.4-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4043f67ebc4f4ab613756e291380b7e42e08d33288b338a075b2f8e719525390 |
|
MD5 | 27835ab68622a1e6ddbee1861c90bb20 |
|
BLAKE2b-256 | 8acac55263354980b438f9d4482f58ddf88385ff0774f41deceb242afaf95499 |
Hashes for simplejpeg-1.7.4-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b6c0685293bb92434c5d36939160c55a653cac9007993c5c02702f77a6c1904 |
|
MD5 | e5fa7cc87c8173ae079600e67a8feaca |
|
BLAKE2b-256 | 3fc29de788bcee947f0e5276715c00821aaf432ec4f759dcf472eb7cb643306c |
Hashes for simplejpeg-1.7.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e932d24098fdd6abbd2132bb6743b883c08a5c79781e307408e7493ec21f24e |
|
MD5 | a7f5d0ea071bf15a89e8a6e44bba4ccd |
|
BLAKE2b-256 | f32257d78a77f0f65c777d03b95e72a70168036f9946bff5b74a0a26849c047f |
Hashes for simplejpeg-1.7.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 302727b8b02b6c82b094ac8eb6ca8d8ad82182964c94bdffe5774e06ed278b49 |
|
MD5 | bc2e7f3ad68023633f9f8bce49aaa170 |
|
BLAKE2b-256 | b8eec854cc53cfc4d48ad5b4ee81bc1c554b196c17e255f55f6a54748dc3bf7e |
Hashes for simplejpeg-1.7.4-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 758b8c3bdfc61628762b7a76c8e42093777ef7dc5fab1c52e907280f9fa214c3 |
|
MD5 | b1464885ea1cca518169c3766d5f4bab |
|
BLAKE2b-256 | b8fac987963971e956473029305b8da7d8ba9684de890aad4ef33d220150fa57 |
Hashes for simplejpeg-1.7.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d303da1a75912d906dc5af6c0af5176a67775fd7194adf1fcb2b63da547ab66 |
|
MD5 | a6a638e7c1fcf7ee6d65515e11663abf |
|
BLAKE2b-256 | 5fdabd05a902c131af2e39b251909e782d6eba861ad6d27efca33ff16694f152 |