"""
Per-image gallery state
"""
from __future__ import with_statement
import filesystem, format, thumbnail
from utils import lazy_load
import PIL.Image
from lib import EXIF
class Image (filesystem.File) :
"""
An Image is a filesystem File that represents an image that can be thumbnailed, and handled.
"""
def __init__ (self, *args, **kwargs) :
"""
Initialize as an Image based on the given Node, linked with the given previous node
"""
super(Image, self).__init__(*args, **kwargs)
# links
self.prev = None
self.next = None
# the .html file for this image
self.html = self.parent.subfile(self.basename + '.html')
# our preview/thumbnail
self.preview = thumbnail.Thumbnail(self, self.parent.preview_dir, self.config.preview_size)
self.thumb = thumbnail.Thumbnail(self, self.parent.thumb_dir, self.config.thumb_size)
# info
self.title = self.name
self.description = None
@lazy_load
def pil_image (self) :
"""
Loads the image as a PIL.Image
"""
# open it up
return PIL.Image.open(self.path)
@lazy_load
def exif (self) :
"""
Loads the EXIF data for the image and returns as a dict of EXIF tag names -> values.
Returns None if no exif data was found
"""
# load
with self.open('rb') as fh :
# XXX: details=False?
exif = EXIF.process_file(fh)
# empty dict -> no exif data
if exif :
return exif
else :
return None
@lazy_load
def metadata (self) :
"""
Load and return the metadata for the image as a dictionary
"""
# load stuff
stat = self.stat()
exif = self.exif
# XXX: avoid having to open the image?
size = self.pil_image.size
# build
return dict({
"File name": self.name,
"Resolution": "%dx%d" % size,
"File size": format.filesize(stat.st_size),
"Last modified": format.filetime(stat.st_mtime),
}, **dict(
(name, exif[tag]) for tag, name in self.config.exif_tags if exif and tag in exif
))
def stale (self) :
"""
Tests if this Image is stale, based on preview/thumb.
"""
return self.preview.stale() or self.thumb.stale()
def update (self) :
"""
Updates this Image's thumb/preview
"""
self.preview.update()
self.thumb.update()