A Python wrapper of libjpeg-turbo for decoding and encoding JPEG image.
Project description
PyTurboJPEG
A Python wrapper of libjpeg-turbo for decoding and encoding JPEG image.
Prerequisites
Example
import cv2
from turbojpeg import TurboJPEG, TJPF_GRAY, TJSAMP_GRAY, TJFLAG_PROGRESSIVE
# specifying library path explicitly
# jpeg = TurboJPEG(r'D:\turbojpeg.dll')
# jpeg = TurboJPEG('/usr/lib64/libturbojpeg.so')
# jpeg = TurboJPEG('/usr/local/lib/libturbojpeg.dylib')
# using default library installation
jpeg = TurboJPEG()
# decoding input.jpg to BGR array
in_file = open('input.jpg', 'rb')
bgr_array = jpeg.decode(in_file.read())
in_file.close()
cv2.imshow('bgr_array', bgr_array)
cv2.waitKey(0)
# direct rescaling 1/2 while decoding input.jpg to BGR array
in_file = open('input.jpg', 'rb')
bgr_array_half = jpeg.decode(in_file.read(), scaling_factor=(1, 2))
in_file.close()
cv2.imshow('bgr_array_half', bgr_array_half)
cv2.waitKey(0)
# decoding JPEG image properties
in_file = open('input.jpg', 'rb')
(width, height, jpeg_subsample, jpeg_colorspace) = jpeg.decode_header(in_file.read())
in_file.close()
# encoding BGR array to output.jpg with default settings.
out_file = open('output.jpg', 'wb')
out_file.write(jpeg.encode(bgr_array))
out_file.close()
# encoding BGR array to output.jpg with TJSAMP_GRAY subsample.
out_file = open('output_gray.jpg', 'wb')
out_file.write(jpeg.encode(bgr_array, jpeg_subsample=TJSAMP_GRAY))
out_file.close()
# encoding BGR array to output.jpg with quality level 50.
out_file = open('output_quality_50.jpg', 'wb')
out_file.write(jpeg.encode(bgr_array, quality=50))
out_file.close()
# encoding BGR array to output.jpg with quality level 100 and progressive entropy coding.
out_file = open('output_quality_100_progressive.jpg', 'wb')
out_file.write(jpeg.encode(bgr_array, quality=100, flags=TJFLAG_PROGRESSIVE))
out_file.close()
# decoding input.jpg to grayscale array
in_file = open('input.jpg', 'rb')
gray_array = jpeg.decode(in_file.read(), pixel_format=TJPF_GRAY)
in_file.close()
cv2.imshow('gray_array', gray_array)
cv2.waitKey(0)
# scale with quality but leaves out the color conversion step
in_file = open('input.jpg', 'rb')
out_file = open('scaled_output.jpg', 'wb')
out_file.write(jpeg.scale_with_quality(in_file.read(), scaling_factor=(1, 4), quality=70))
out_file.close()
in_file.close()
# lossless crop image
out_file = open('lossless_cropped_output.jpg', 'wb')
out_file.write(jpeg.crop(open('input.jpg', 'rb').read(), 8, 8, 320, 240))
out_file.close()
Installation
macOS
- brew install jpeg-turbo
- pip install -U git+git://github.com/lilohuang/PyTurboJPEG.git
Windows
- Download libjpeg-turbo official installer
- pip install -U git+git://github.com/lilohuang/PyTurboJPEG.git
Linux
-
RHEL/CentOS/Fedora
- Download libjpeg-turbo.repo to /etc/yum.repos.d/
- sudo yum install libjpeg-turbo-official
- pip install -U git+git://github.com/lilohuang/PyTurboJPEG.git
-
Ubuntu
- sudo apt-get update
- sudo apt-get install libturbojpeg
- pip install -U git+git://github.com/lilohuang/PyTurboJPEG.git
Benchmark
macOS
- macOS Sierra 10.12.6
- Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
- opencv-python 3.4.0.12 (pre-built)
- turbo-jpeg 1.5.3 (pre-built)
Function | Wall-clock time |
---|---|
cv2.imdecode() | 0.528 sec |
TurboJPEG.decode() | 0.191 sec |
cv2.imencode() | 0.875 sec |
TurboJPEG.encode() | 0.176 sec |
Windows
- Windows 7 Ultimate 64-bit
- Intel(R) Xeon(R) E3-1276 v3 CPU @ 3.60 GHz
- opencv-python 3.4.0.12 (pre-built)
- turbo-jpeg 1.5.3 (pre-built)
Function | Wall-clock time |
---|---|
cv2.imdecode() | 0.358 sec |
TurboJPEG.decode() | 0.135 sec |
cv2.imencode() | 0.581 sec |
TurboJPEG.encode() | 0.140 sec |
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
PyTurboJPEG-1.4.1.tar.gz
(6.8 kB
view details)
File details
Details for the file PyTurboJPEG-1.4.1.tar.gz
.
File metadata
- Download URL: PyTurboJPEG-1.4.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09688a93331281e566569b4d313e1d1a058ca32ccae1a2473847a10e4ca2f2a7 |
|
MD5 | df42ecc72debd4aef20537fd8d8cd512 |
|
BLAKE2b-256 | 9df56106c673096b1bc1af716bbc9b17e542c77ccad5fafd150afb91ff18a6e8 |