terom@14: # DeGAL - A pretty simple web image gallery terom@14: # Copyright (C) 2007 Tero Marttila terom@14: # http://marttila.de/~terom/degal/ terom@14: # terom@14: # This program is free software; you can redistribute it and/or modify terom@14: # it under the terms of the GNU General Public License as published by terom@14: # the Free Software Foundation; either version 2 of the License, or terom@14: # (at your option) any later version. terom@14: # terom@14: # This program is distributed in the hope that it will be useful, terom@14: # but WITHOUT ANY WARRANTY; without even the implied warranty of terom@14: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terom@14: # GNU General Public License for more details. terom@14: # terom@14: # You should have received a copy of the GNU General Public License terom@14: # along with this program; if not, write to the terom@14: # Free Software Foundation, Inc., terom@14: # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. terom@14: # terom@14: terom@12: import os, os.path terom@12: terom@12: import PIL.Image terom@12: terom@33: import dexif terom@33: terom@28: import settings, utils, log terom@12: from template import image as image_tpl terom@12: terom@12: class Image (object) : terom@12: def __init__ (self, dir, name) : terom@12: # the image filename, e.g. DSC3948.JPG terom@27: self.name = unicode(name) terom@12: terom@12: # the Folder object that we are in terom@12: self.dir = dir terom@12: terom@12: # the relative path from the root to us terom@27: self.path = dir.pathFor(self.name) terom@12: terom@12: # the basename+ext, e.g. DSCR3948, .JPG terom@27: self.base_name, self.ext = os.path.splitext(self.name) terom@12: terom@12: # our user-friendly title terom@27: self.title = self.name terom@12: terom@12: # our long-winded description terom@12: self.descr = '' terom@12: terom@12: # the image before and after us, both may be None terom@12: self.prev = self.next = None terom@12: terom@12: # the image-relative names for the html page, thumb and preview images terom@12: self.html_name = self.name + ".html" terom@12: self.thumb_name = utils.url_join(settings.THUMB_DIR, self.name) terom@12: self.preview_name = utils.url_join(settings.PREVIEW_DIR, self.name) terom@12: terom@12: # the root-relative paths to the html page, thumb and preview images terom@12: self.html_path = self.dir.pathFor(self.html_name) terom@12: self.thumb_path = self.dir.pathFor(settings.THUMB_DIR, self.name) terom@12: self.preview_path = self.dir.pathFor(settings.PREVIEW_DIR, self.name) terom@12: terom@12: # terom@12: # Figured out after prepare terom@12: # terom@12: terom@12: # (w, h) tuple terom@12: self.img_size = None terom@12: terom@12: # the ShortURL code for this image terom@12: self.shorturl_code = None terom@12: terom@33: # EXIF data terom@33: self.exif_data = {} terom@33: terom@12: # what to use in the rendered templates, intended to be overridden by subclasses terom@12: self.series_act = "add" terom@12: self.series_verb = "Add to" terom@12: terom@12: def getObjInfo (self) : terom@12: """ terom@12: Metadata for shorturl2.db terom@12: """ terom@12: return 'img', self.dir.path, self.name terom@12: terom@12: def breadcrumb (self) : terom@12: """ terom@12: Returns a [(fname, title)] list of this image's parents terom@12: """ terom@12: terom@13: return self.dir.breadcrumb(forImg=self) + [(self.html_name, self.title)] terom@12: terom@12: def render (self) : terom@12: """ terom@12: Write out the .html file terom@12: """ terom@12: terom@12: # stat the image file to get the filesize and mtime terom@12: st = os.stat(self.path) terom@12: terom@12: self.filesize = st.st_size terom@12: self.timestamp = st.st_mtime terom@12: terom@12: # open the image in PIL to get image attributes + generate thumbnails terom@12: img = PIL.Image.open(self.path) terom@12: terom@12: self.img_size = img.size terom@12: terom@12: for out_path, geom in ((self.thumb_path, settings.THUMB_GEOM), (self.preview_path, settings.PREVIEW_GEOM)) : terom@12: # if it doesn't exist, or it's older than the image itself, generate terom@28: if utils.mtime(out_path) < self.timestamp : terom@28: log.info("render [%sx%s]", geom[0], geom[1], wait=True) terom@12: terom@12: # XXX: is this the most efficient way to do this? It seems slow terom@12: out_img = img.copy() terom@12: out_img.thumbnail(geom, resample=True) terom@12: out_img.save(out_path) terom@28: terom@28: log.done() terom@12: terom@12: # look for the metadata file terom@12: title_path = self.dir.pathFor(self.base_name + '.txt') terom@12: terom@12: self.title, self.descr = utils.readTitleDescr(title_path) terom@12: terom@12: if not self.title : terom@12: self.title = self.name terom@28: terom@28: if utils.mtime(self.html_path) < self.timestamp : terom@28: log.info("render %s.html", self.name) terom@12: terom@33: # parse the exif data from the file terom@33: try : terom@33: self.exif_data = dexif.parse_exif(self.path) terom@33: except dexif.ExifError, message: terom@33: log.warning("Reading EXIF data for %s failed: %s" % (self.filename, message)) terom@33: self.exif_data = {} terom@33: terom@33: terom@28: image_tpl.render_to(self.html_path, terom@28: stylesheet_url = self.dir.inRoot('style.css'), terom@28: title = self.title, terom@28: breadcrumb = self.breadcrumb(), terom@28: terom@28: prev = self.prev, terom@28: next = self.next, terom@28: img = self, terom@28: terom@28: description = self.descr, terom@28: terom@28: filename = self.name, terom@28: img_size = self.img_size, terom@28: file_size = self.filesize, terom@28: timestamp = self.timestamp, terom@33: exif_data = self.exif_data, terom@28: terom@28: shorturl = self.dir.inRoot('s', self.shorturl_code), terom@28: shorturl_code = self.shorturl_code, terom@28: terom@28: series_url = self.dir.inRoot('series/%s/%s' % (self.series_act, self.shorturl_code)), terom@28: series_verb = self.series_verb, terom@28: ) terom@12: terom@12: def __str__ (self) : terom@12: return "Image `%s' in `%s'" % (self.name, self.dir.path) terom@28: