Giter Site home page Giter Site logo

escaped / django-exiffield Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 6.0 4.85 MB

django-exiffield extracts exif data by utilizing the exiftool

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
exif exiftool django python36 python django-field hacktoberfest

django-exiffield's Introduction

django-exiffield

PyPI GitHub Workflow Status (master) Coveralls github branch PyPI - Python Version PyPI - License

django-exiffield extracts exif information by utilizing the exiftool.

Requirements

  • Python 3.6.1 or newer
  • exiftool
  • Django >= 2.2

Installation

  1. Install django-exiffield

    pip install django-exiffield
  2. Make sure exiftool is executable from you environment.

Integration

Let's assume we have an image Model with a single ImageField. To extract exif information for an attached image, add an ExifField, specify the name of the ImageField in the source argument

from django.db import models

from exiffield.fields import ExifField


class Image(models.Model):
    image = models.ImageField()
    exif = ExifField(
        source='image',
    )

and create a migration for the new field. That's it.

After attaching an image to your ImageField, the exif information is stored as a dict on the ExifField. Each exif information of the dictionary consists of two keys:

  • desc: A human readable description
  • val: The value for the entry.

In the following example we access the camera model

image = Image.objects.get(...)
print(image.exif['Model'])
# {
#     'desc': 'Camera Model Name',
#     'val': 'DMC-GX7',
# }

As the exif information is encoded in a simple dict you can iterate and access the values with all familiar dictionary methods.

Denormalizing Fields

Since the ExifField stores its data simply as text, it is not possible to filter or access indiviual values efficiently. The ExifField provides a convinient way to denormalize certain values using the denormalized_fields argument. It takes a dictionary with the target field as key and a simple getter function of type Callable[[Dict[Dict[str, str]]], Any]. To denormalize a simple value you can use the provided exiffield.getters.exifgetter

from django.db import models

from exiffield.fields import ExifField
from exiffield.getters import exifgetter


class Image(models.Model):
    image = models.ImageField()
    camera = models.CharField(
        editable=False,
        max_length=100,
    )
    exif = ExifField(
        source='image',
        denormalized_fields={
            'camera': exifgetter('Model'),
        },
    )

There are more predefined getters in exiffield.getters:

exifgetter(exif_key: str) -> str
Get an unmodified exif value.

get_type() -> str
Get file type, e.g. video or image

get_datetaken -> Optional[datetime]
Get when the file was created as datetime

get_orientation -> exiffield.getters.Orientation
Get orientation of media file. Possible values are LANDSCAPE and PORTRAIT.

get_sequenctype -> exiffield.getters.Mode
Guess if the image was taken in a sequence. Possible values are BURST, BRACKETING, TIMELAPSE and SINGLE.

get_sequencenumber -> int
Get image position in a sequence.

Development

This project uses poetry for packaging and managing all dependencies and pre-commit to run flake8, isort, mypy and black.

Clone this repository and run

poetry install
poetry run pre-commit install

to create a virtual enviroment containing all dependencies. Afterwards, You can run the test suite using

poetry run pytest

This repository follows the Conventional Commits style.

Cookiecutter template

This project was created using cruft and the cookiecutter-pyproject template. In order to update this repository to the latest template version run

cruft update

in the root of this repository.

django-exiffield's People

Contributors

escaped avatar jsutlovic avatar vekerdyb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

django-exiffield's Issues

Updates django to latest version during install

In the documentation the Django requirement is >1.8. I have Django 1.11.7 and pip wanted to upgrade Django to the latest version (3.0) during django-exiffield install which brakes everything and it is difficult to roll back.

get_orientation

In get_orientation is line:

orientation = exif['Orientation']['num']

If orientation missing in exif, it get error and function get_orientation not returning landscape/portrait value.

The code should be something like this:

    try:
        orientation = exif['Orientation']['num']
    except:
        orientation = 0

Timezone and get_datetaken

If timezone support is enabled in django, get_datetaken gives RuntimeWarning:

"DateTimeField Photo.taken received a naive datetime (2020-12-12 11:50:20) while time zone support is active."
And saves wrong datetime in to database.

How can I fix it?

denormalized_fields={'taken': get_datetaken}

async using celery or rq

Currently, the exif information is extracted as soon as a model is saved. This can be quite time-consuming. Therefore we need an async mode, where the actual extraction is done using something like celery or rq.

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.