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@12: import settings, utils terom@12: from log import index, render 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@12: self.name = 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@12: self.path = dir.pathFor(name) terom@12: terom@12: # the basename+ext, e.g. DSCR3948, .JPG terom@12: self.base_name, self.ext = os.path.splitext(name) terom@12: terom@12: # our user-friendly title terom@12: self.title = 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@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@12: if not (os.path.exists(out_path) and os.stat(out_path).st_mtime > self.timestamp) : terom@12: render.info("Create thumbnailed image at %s with geom %s", out_path, geom) 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@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@12: terom@12: render.info("Rendering image %s", self.path) terom@12: terom@12: image_tpl.render_to(self.html_path, terom@12: stylesheet_url = self.dir.inRoot('style.css'), terom@12: title = self.title, terom@12: breadcrumb = self.breadcrumb(), terom@12: terom@12: prev = self.prev, terom@12: next = self.next, terom@12: img = self, terom@12: terom@12: description = self.descr, terom@12: terom@12: filename = self.name, terom@12: img_size = self.img_size, terom@12: file_size = self.filesize, terom@12: timestamp = self.timestamp, terom@12: terom@12: shorturl = self.dir.inRoot('s', self.shorturl_code), terom@12: shorturl_code = self.shorturl_code, terom@12: terom@12: series_url = self.dir.inRoot('series/%s/%s' % (self.series_act, self.shorturl_code)), terom@12: series_verb = self.series_verb, terom@12: ) terom@12: terom@12: def __str__ (self) : terom@12: return "Image `%s' in `%s'" % (self.name, self.dir.path)