• Stars
    star
    144
  • Rank 254,059 (Top 6 %)
  • Language
    C
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast, (mostly) lossless JPEG transformations with Python

jpegtran-cffi

Build status

jpegtran-cffi is a Python package for fast JPEG transformations. Compared to other, more general purpose image processing libraries like wand-py or PIL/Pillow, transformations are generally more than twice as fast (see Benchmarks). In addition, all operations except for scaling are lossless, since the image is not being re-compressed in the process. This is due to the fact that all transformation operations work directly with the JPEG data.

This is achieved by using multiple C routines from the Enlightenment project's epeg library (for scaling) and the `turbojpeg`_ library from the libjpeg-turbo project (for all other operations). These routines are called from Python through the CFFI module, i.e. no external processes are launched.

The package also includes rudimentary support for getting and setting the EXIF orientation tag, automatically transforming the image according to it and obtaining the JFIF thumbnail image.

jpegtran-cffi was developed as part of a web interface for the spreads project, where a large number of images from digital cameras had to be prepared for display by a Raspberry Pi. With the Pi's rather slow ARMv6 processor, both Wand and PIL were too slow to be usable.

Requirements

  • CPython >=2.7 or >=3.5 or PyPy
  • cffi >= 1.0
  • libturbojpeg with headers

Installation

$ pip install jpegtran-cffi

Note

There is a bug in some Ubuntu versions of the libturbojpeg package that prevents the package from installing correctly. If you get the error relocation R_X86_64_32 against .data can not be used..., please perform the following command:

sudo ln -s /usr/lib/x86_64-linux-gnu/libturbojpeg.so.0.1.0 /usr/lib/x86_64-linux-gnu/libturbojpeg.so

Usage

from jpegtran import JPEGImage

img = JPEGImage('image.jpg')

# JPEGImage can also be initialized from a bytestring
blob = requests.get("http://example.com/image.jpg").content
from_blob = JPEGImage(blob=blob)

# Reading various image parameters
print img.width, img.height  # "640 480"
print img.exif_orientation  # "1" (= "normal")

# If present, the JFIF thumbnail can be obtained as a bytestring
thumb = img.exif_thumbnail

# Transforming the image
img.downscale(320, 240).save('scaled.jpg')
img.rotate(90).save('rotated.jpg')
img.crop(0, 0, 100, 100).save('cropped.jpg')

# Transformations can be chained
data = (img.downscale(320, 240)
            .rotate(90)
            .flip('horizontal')
            .as_blob())

# jpegtran can transform the image automatically according to the EXIF
# orientation tag
photo = JPEGImage(blob=requests.get("http://example.com/photo.jpg").content)
print photo.exif_orientation  # "6" (= 270°)
print photo.width, photo.height # "4320 3240"
corrected = photo.exif_autotransform()
print corrected.exif_orientation  # "1" (= "normal")
print corrected.width, corrected.height  # "3240 4320"

For more details, refer to the API Reference.

Benchmarks

All operations were done on a 3.4GHz i7-3770 with 16GiB of RAM and a 7200rpm HDD with the following 2560x1920 8bit RGB JPEG:

http://upload.wikimedia.org/wikipedia/commons/8/82/Mandel_zoom_05_tail_part.jpg

https://jpegtran-cffi.readthedocs.io/en/latest/_images/benchmark.png

Both wand-py and PIL were run with the fastest scaling algorithm available, for wand-py this meant using Image.sample instead of Image.resize and for PIL the nearest-neighbour filter was used for the Image.resize call.

License

The MIT License (MIT)

Copyright (c) 2014 Johannes Baiter <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

More Repositories

1

zotero-cli

Command-line interface for Zotero
Python
246
star
2

pyomxplayer

Python wrapper around OMXPlayer for the Raspberry Pi
Python
104
star
3

python-rust-fst

Python bindings for Rust's fst crate
Python
47
star
4

gphoto2-cffi

Python bindings for libgphoto2 with an idiomatic API
Python
36
star
5

demetsiiify

Web service for creating and hosting IIIF manifests from METS/MODS documents
Python
31
star
6

pdiiif

Create PDFs from IIIF manifests, completely client-side (with server-based fallback for unsupported browsers)
TypeScript
27
star
7

hocrviewer-mirador

View HOCR files with Mirador
Python
23
star
8

beetfs

A beets FUSE filesystem
Python
20
star
9

archiscribe

Web application for transcribing OCR ground truth from Archive.org
Go
16
star
10

chdkptp.py

Python bindings for chdkptp
Python
13
star
11

plugin.video.mubi

MUBI Plugin for XBMC
Python
12
star
12

hocrviewer-old

Display books in hOCR format using the OpenLibrary BookReader
JavaScript
11
star
13

wav2msu

Utility to convert a RIFF Wave file to a MSU1-compatible format
C
9
star
14

archiscribe-corpus

Repository for 19th century German fraktur lines transcribed via archiscribe.jbaiter.de
8
star
15

ff4msu

Romhack to provide MSU1-support for Final Fantasy II/IV (SNES)
Assembly
7
star
16

mirador3-plugin-dev-tutorial

Tutorial for Mirador 3 Plugin Development
JavaScript
7
star
17

mocri

Small gRPC microservice for OCR based on kraken
Python
5
star
18

mubipy

Python module to access MUBI.com functionality
Python
5
star
19

mopidy-kodi

Mopidy extension for playing music from your Kodi library
Python
4
star
20

pyragarga

Python module to scrape karagarga.net
Python
3
star
21

hidapi-cffi

Python bindings for hidapi via CFFI
Python
3
star
22

epeg-cffi

Python bindings for 'epeg': "Insanely fast JPEG/ JPG thumbnail scaling with the minimum fuss and CPU overhead."
C
3
star
23

tei2hocr

XSLT Stylesheet to convert TEI OCR data to HOCR
XSLT
2
star
24

iiif_dhberlin

CSS
2
star
25

kenlm-java

Java SWIG Wrapper around kenlm
Java
1
star
26

bass.vim

VIM syntax highlighting for the the 'bass' assembler
Vim Script
1
star
27

az-ner-corpus

Named Entity Recognition corpus for "Allgemeine Zeitung"
PLpgSQL
1
star
28

annotare

Minimalist responsive tagging of linguistic resources
Clojure
1
star
29

kinoknecht

a film/video library written in Python/SQLAlchemy/Flask
1
star
30

cantaloupe-docker

Docker setup for Cantaloupe based on Dockerfile from MIT
Dockerfile
1
star
31

sz_epaper

Command-Line utility to download the PDF E-Paper of the "Süddeutsche Zeitung"
Python
1
star