diff --git a/setup.py b/setup.py index ebd6db7..a296995 100644 --- a/setup.py +++ b/setup.py @@ -14,6 +14,8 @@ requires = [ 'transaction', 'zope.sqlalchemy', 'SQLAlchemy', + 'pillow', + 'numpy' ] setup(name='Soter', diff --git a/soter/models/master.py b/soter/models/master.py index 1d02743..a7494c9 100644 --- a/soter/models/master.py +++ b/soter/models/master.py @@ -101,6 +101,7 @@ class Picture(Base): album_id = Column('album_id', GUID(), ForeignKey('albums.id'), nullable=False) is_public = Column('is_public', Boolean, nullable=False) + tags = relationship("Tag", secondary=picture_tag) user = relationship('User', primaryjoin="User.id==Picture.user_id", cascade=None) def __init__(self, name, url, user_id, album_id, is_public, id=None, is_fixture=False): diff --git a/soter/views/picture.py b/soter/views/picture.py index da324a3..5057f59 100644 --- a/soter/views/picture.py +++ b/soter/views/picture.py @@ -2,14 +2,14 @@ import os import shutil import uuid +from PIL import Image, ExifTags import pkg_resources from pyramid.response import FileResponse - from pyramid.security import authenticated_userid - from pyramid.view import view_config import transaction +import numpy from soter.models import DBSession from soter.models.master import Picture @@ -32,12 +32,33 @@ def upload(request): with open(temp_file_path, 'wb') as output_file: shutil.copyfileobj(input_file, output_file) os.rename(temp_file_path, file_path) + # Process picture here to get the hash and the exif tags to be added to the database pic = Picture(file_id, str(file_id), uuid.UUID(authenticated_userid(request)), uuid.UUID(album), True, file_id) DBSession.add(pic) transaction.commit() return {'location': file_path} +def process_picture(path): + image = get_image(path) + Picture(file_id, str(file_id), uuid.UUID(authenticated_userid(request)), uuid.UUID(album), True, file_id) + + +def get_image(path): + image = Image.open(path) + orientation = [tag for tag in ExifTags.TAGS.keys() if ExifTags.TAGS[tag] == 'Orientation'][0] + exif = dict(image._getexif().items()) + if exif[orientation] == 3: + image = image.rotate(180, expand=True) + elif exif[orientation] == 6: + image = image.rotate(270, expand=True) + elif exif[orientation] == 8: + image = image.rotate(90, expand=True) + img = numpy.array(image) + # CONVERT RGB TO BGR + return img[:, :, ::-1].copy() + + @view_config(request_method='GET', route_name='picture_raw', permission='Albums') def show_raw(request): package, resource = ('soter:upload/' + request.matchdict['id'] + '.jpg').split(':', 1)