Giter Site home page Giter Site logo

imdirect's Introduction

imdirect

Build Status Coverage Status

PIL extension performing automatic rotation of opened JPEG images.

Description

The orientation of the photographed object or scene with respect to the digital camera is encoded in the resulting image's Exif [1] data (given that it is saved as a JPEG). When working with such digital camera images, this orientation might lead to problems handling the image and is very often desired to be counteracted.

This module is a small extension to Pillow that monkey patches the PIL.Image.open method to automatically rotate the image [2] (by lossless methods) and update the Exif tag accordingly, given that image is a JPEG.

The package also features a save method that includes the Exif data by default when saving JPEGs.

Installation

pip install imdirect

Usage

Demonstration of the monkey patching and how it works:

>>> from PIL import Image
>>> import imdirect
>>> img = Image.open('image.jpg')
>>> print("{0}, Orientation: {1}".format(img, img._getexif().get(274)))
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=4032x3024 at 0x7F44B5E4FF10>, Orientation: 6
>>> imdirect.monkey_patch()
>>> img_autorotated = Image.open('image.jpg')
>>> print("{0}, Orientation: {1}".format(img_autorotated, img_autorotated._getexif().get(274)))
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3024x4032 at 0x7F44B5DF5150>, Orientation: 1

The package can also be used without monkey patching, by applying the imdirect.imdirect_open method directly:

>>> from imdirect import imdirect_open
>>> img = imdirect_open('image.jpg')

or by using the imdirect.autorotate on a PIL.Image.Image object:

>>> from PIL import Image
>>> import imdirect
>>> img = Image.open('image.jpg')
>>> img_rotated = imdirect.autorotate(img)

The last method does not return a PIL.JpegImagePlugin.JpegImageFile, but can still be used if the Exif information of the original image is undesired.

Tests

Tests can be run with pytest:

Testing started at 13:28 ...
============================= test session starts ==============================
platform linux2 -- Python 2.7.12, pytest-3.0.1, py-1.4.31, pluggy-0.3.1
rootdir: /home/hbldh/Repos/imdirect, inifile:
collected 4 items

test_autorotate.py ...
test_monkey_patching.py .

=========================== 4 passed in 0.08 seconds ===========================

References

[1]Exif on Wikipedia (https://en.wikipedia.org/wiki/Exif)
[2]Exif orientation (http://sylvana.net/jpegcrop/exif_orientation.html)

imdirect's People

Contributors

hbldh avatar clarsen avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.