configuration magic - can now load configuration data from ./degal.cfg, --config, folder/degal.cfg
"""
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 img (self) :
"""
Loads the image as a PIL.Image
"""
# open it up
return PIL.Image.open(self.path)
@property
def img_size (self) :
"""
Returns the pixel (width, height) resolution tuple of this image.
"""
return self.img.size
@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()
# XXX: avoid having to open the image?
size = self.img_size
# build
metadata = dict({
"File name": self.name,
"Resolution": "%dx%d" % size,
"File size": format.filesize(stat.st_size),
"Last modified": format.filetime(stat.st_mtime),
})
# optionally load Exif metadata
if self.config.with_exif :
exif = self.exif
# Get the wanted tags
metadata.update(dict(
(name, exif[tag]) for tag, name in self.config.exif_tags if exif and tag in exif
))
return metadata
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()