From 643c75099c5ac8ede1cad1549f8853a893a862f0 Mon Sep 17 00:00:00 2001 From: Don Allingham Date: Tue, 11 Sep 2007 03:51:35 +0000 Subject: [PATCH] * src/plugins/WriteCD.py: don't generate thumbnails * src/Selectors/_SelectObject.py: new image sizing routines * src/ImgManip.py: simplify * src/docgen/HtmlDoc.py: new image sizing routines * src/docgen/ODFDoc.py: new image sizing routines * src/docgen/LaTeXDoc.py: new image sizing routines * src/docgen/RTFDoc.py: new image sizing routines * src/Makefile.am: add ThumbNails.py * po/POTFILES.in: add ThumbNails.py 2007-09-10 Don Allingham svn: r8959 --- ChangeLog | 11 ++ po/POTFILES.in | 1 + src/ImgManip.py | 208 +++++++++++---------------------- src/Makefile.am | 1 + src/Selectors/_SelectObject.py | 4 +- src/ThumbNails.py | 5 +- src/docgen/HtmlDoc.py | 3 +- src/docgen/LaTeXDoc.py | 1 + src/docgen/ODFDoc.py | 7 +- src/docgen/RTFDoc.py | 11 +- src/plugins/WriteCD.py | 10 -- 11 files changed, 91 insertions(+), 171 deletions(-) diff --git a/ChangeLog b/ChangeLog index bd10dff0f..56705bd62 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2007-09-10 Don Allingham + * src/plugins/WriteCD.py: don't generate thumbnails + * src/Selectors/_SelectObject.py: new image sizing routines + * src/ImgManip.py: simplify + * src/docgen/HtmlDoc.py: new image sizing routines + * src/docgen/ODFDoc.py: new image sizing routines + * src/docgen/LaTeXDoc.py: new image sizing routines + * src/docgen/RTFDoc.py: new image sizing routines + * src/Makefile.am: add ThumbNails.py + * po/POTFILES.in: add ThumbNails.py + 2007-09-10 Don Allingham * src/ThumbNails.py: Added to handle the thumbnailing routines * src/DataViews/_MediaView.py: thumbnail updates diff --git a/po/POTFILES.in b/po/POTFILES.in index 38dac81d1..2e039e579 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -46,6 +46,7 @@ src/Spell.py src/SubstKeywords.py src/TipOfDay.py src/ToolTips.py +src/ThumbNails.py src/TransUtils.py src/TreeTips.py src/Utils.py diff --git a/src/ImgManip.py b/src/ImgManip.py index 99132d88e..711633cc5 100644 --- a/src/ImgManip.py +++ b/src/ImgManip.py @@ -24,14 +24,12 @@ Image manipulation routines. """ - #------------------------------------------------------------------------- # # Standard python modules # #------------------------------------------------------------------------- import os -import md5 import tempfile #------------------------------------------------------------------------- @@ -44,151 +42,79 @@ import gobject #------------------------------------------------------------------------- # -# gramps modules +# resize_to_jpeg # #------------------------------------------------------------------------- -import const -import Mime -import Config -import Utils - -#------------------------------------------------------------------------- -# -# ImgManip -# -#------------------------------------------------------------------------- -class ImgManip: +def resize_to_jpeg(source, destination, width, height): """ - Image manipulation class + Creates the destination, derived from the source, resizing it to the + specified size, while converting to JPEG. + + @param source: source image file, in any format that gtk recognizes + @type source: unicode + @param destination: destination image file, output written in jpeg format + @type destination: unicode + @param width: desired width of the destination image + @type width: int + @param height: desired height of the destination image + @type height: int """ - def __init__(self, source): - self.src = source - try: - self.img = gtk.gdk.pixbuf_new_from_file(self.src) - self.width = self.img.get_width() - self.height = self.img.get_height() - except gobject.GError: - self.width = 0 - self.height = 0 + img = gtk.gdk.pixbuf_new_from_file(source) + scaled = img.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR) + scaled.save(destination, 'jpeg') - def size(self): - """ - Returns a tuple consisting of the width, height of the image in pixels. - - @rtype width and height of the image in pixels - @return tuple of two integers - """ - return (self.width, self.height) - - def jpg_thumbnail(self, dest, width, height): - """ - @type dest: unicode - @param dest : target filename - @type width: int - @param width: desired width of the image - @type height: int - @param height: desired height of the image - """ - scaled = self.img.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR) - scaled.save(dest, 'jpeg') - - def png_data(self): - fd, dest = tempfile.mkstemp() - self.img.save(dest, "png") - fh = open(dest, mode='rb') - data = fh.read() - fh.close() - try: - os.unlink(dest) - except: - pass - return data - - def jpg_scale_data(self, x, y): - fd, dest = tempfile.mkstemp() - scaled = self.img.scale_simple(int(x), int(y), gtk.gdk.INTERP_BILINEAR) - scaled.save(dest, 'jpeg') - fh = open(dest, mode='rb') - data = fh.read() - fh.close() - try: - os.unlink(dest) - except: - pass - return data - -def _build_thumb_path(path): - m = md5.md5(path) - return os.path.join(const.THUMB_DIR, m.hexdigest()+'.png') - -def run_thumbnailer(mtype, frm, to, size=const.THUMBSCALE): - if const.USE_THUMBNAILER and os.path.isfile(frm): - sublist = { - '%s' : "%dx%d" % (int(size), int(size)), - '%u' : frm, - '%o' : to, - } - - base = '/desktop/gnome/thumbnailers/%s' % mtype.replace('/', '@') - - cmd = Config.get_string(base + '/command') - enable = Config.get_bool(base + '/enable') - - if cmd and enable: - cmdlist = map(lambda x: sublist.get(x, x), cmd.split()) - os.spawnvpe(os.P_WAIT, cmdlist[0], cmdlist, os.environ) - return True - else: - return False - return False - -def set_thumbnail_image(path, mtype=None): - if mtype and not mtype.startswith('image/'): - run_thumbnailer(mtype, path, _build_thumb_path(path)) - else: - try: - pixbuf = gtk.gdk.pixbuf_new_from_file(path) - w = pixbuf.get_width() - h = pixbuf.get_height() - scale = const.THUMBSCALE / (float(max(w, h))) - - pw = int(w*scale) - ph = int(h*scale) - - pixbuf = pixbuf.scale_simple(pw, ph, gtk.gdk.INTERP_BILINEAR) - pixbuf.save(_build_thumb_path(path), "png") - except: - pass - -def get_thumbnail_image(path, mtype=None): - filename = _build_thumb_path(path) +#------------------------------------------------------------------------- +# +# image_size +# +#------------------------------------------------------------------------- +def image_size(source): + """ + Returns the width and size of the specified image. + @param source: source image file, in any format that gtk recongizes + @type source: unicode + @rtype: tuple(int, int) + @returns: a tuple consisting of the width and height + """ try: - path = Utils.find_file( path) - if not os.path.isfile(filename): - set_thumbnail_image(path, mtype) - elif os.path.getmtime(path) > os.path.getmtime(filename): - set_thumbnail_image(path, mtype) - return gtk.gdk.pixbuf_new_from_file(filename) - except (gobject.GError, OSError): - if mtype: - return Mime.find_mime_type_pixbuf(mtype) - else: - return gtk.gdk.pixbuf_new_from_file(os.path.join( - const.IMAGE_DIR, "document.png")) + img = gtk.gdk.pixbuf_new_from_file(source) + width = self.img.get_width() + height = self.img.get_height() + except gobject.GError: + width = 0 + height = 0 + return (width, height) -def get_thumbnail_path(path, mtype=None): - filename = _build_thumb_path(path) - if not os.path.isfile(filename): - set_thumbnail_image(path, mtype) - return filename +#------------------------------------------------------------------------- +# +# resize_to_jpeg_buffer +# +#------------------------------------------------------------------------- +def resize_to_jpeg_buffer(source, width, height): + """ + Loads the image, converting the file to JPEG, and resizing it. Instead of + saving the file, the data is returned in a buffer. + + @param source: source image file, in any format that gtk recognizes + @type source: unicode + @param width: desired width of the destination image + @type width: int + @param height: desired height of the destination image + @type height: int + @rtype: buffer of data + @returns: jpeg image as raw data + """ + fd, dest = tempfile.mkstemp() + img = gtk.gdk.pixbuf_new_from_file(source) + scaled = img.scale_simple(int(width), int(height), gtk.gdk.INTERP_BILINEAR) + scaled.save(dest, 'jpeg') + fh = open(dest, mode='rb') + data = fh.read() + fh.close() + try: + os.unlink(dest) + except: + pass + return data -def get_thumb_from_obj(obj): - mtype = obj.get_mime_type() - if mtype.startswith("image/"): - image = get_thumbnail_image(obj.get_path()) - else: - image = Mime.find_mime_type_pixbuf(mtype) - if not image: - image = gtk.gdk.pixbuf_new_from_file(const.ICON) - return image diff --git a/src/Makefile.am b/src/Makefile.am index f3c2c4dd9..fdb6fc59e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -75,6 +75,7 @@ gdir_PYTHON = \ Spell.py\ SubstKeywords.py\ TipOfDay.py\ + ThumbNails.py\ ToolTips.py\ TransUtils.py\ TreeTips.py\ diff --git a/src/Selectors/_SelectObject.py b/src/Selectors/_SelectObject.py index 490d298c6..320f1b55d 100644 --- a/src/Selectors/_SelectObject.py +++ b/src/Selectors/_SelectObject.py @@ -44,7 +44,7 @@ import gtk # #------------------------------------------------------------------------- import const -import ImgManip +import ThumbNails from DisplayModels import MediaModel from _BaseSelector import BaseSelector @@ -90,6 +90,6 @@ class SelectObject(BaseSelector): return handle = id_list[0] obj = self.get_from_handle_func()(handle) - pix = ImgManip.get_thumbnail_image(obj.get_path()) + pix = ThumbNails.get_thumbnail_image(obj.get_path()) self.preview.set_from_pixbuf(pix) gc.collect() diff --git a/src/ThumbNails.py b/src/ThumbNails.py index 78382ba22..50b8bb637 100644 --- a/src/ThumbNails.py +++ b/src/ThumbNails.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -18,13 +18,10 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # -# $Id: ImgManip.py 8948 2007-09-08 05:54:02Z dallingham $ - """ Handles generation and access to thumbnails used in GRAMPS. """ - #------------------------------------------------------------------------- # # Standard python modules diff --git a/src/docgen/HtmlDoc.py b/src/docgen/HtmlDoc.py index 6d86fdc09..1adfc8daf 100644 --- a/src/docgen/HtmlDoc.py +++ b/src/docgen/HtmlDoc.py @@ -374,8 +374,7 @@ class HtmlDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc): return try: - img = ImgManip.ImgManip(name) - img.jpg_thumbnail("%s%s%s" % (imdir,os.path.sep,refname),size,size) + ImgManip.resize_to_jpeg(name, newfile, size, size) except: return diff --git a/src/docgen/LaTeXDoc.py b/src/docgen/LaTeXDoc.py index 5724a7e02..c0d87f804 100644 --- a/src/docgen/LaTeXDoc.py +++ b/src/docgen/LaTeXDoc.py @@ -466,6 +466,7 @@ class LaTeXDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc): def add_media_object(self,name,pos,x,y): """Add photo to report""" + return try: pic = ImgManip.ImgManip(name) diff --git a/src/docgen/ODFDoc.py b/src/docgen/ODFDoc.py index e086ef253..ec5de8f1c 100644 --- a/src/docgen/ODFDoc.py +++ b/src/docgen/ODFDoc.py @@ -439,11 +439,8 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): # try to open the image. If the open fails, it probably wasn't # a valid image (could be a PDF, or a non-image) - try: - image = ImgManip.ImgManip(name) - (x,y) = image.size() - ratio = float(x_cm)*float(y)/(float(y_cm)*float(x)) - except: + (x,y) = ImgManip.image_size(name) + if (x,y) == (0,0): return if ratio < 1: diff --git a/src/docgen/RTFDoc.py b/src/docgen/RTFDoc.py index 8dd841da7..37b3fbf59 100644 --- a/src/docgen/RTFDoc.py +++ b/src/docgen/RTFDoc.py @@ -362,12 +362,8 @@ class RTFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc): # #-------------------------------------------------------------------- def add_media_object(self,name,pos,x_cm,y_cm): - try: - im = ImgManip.ImgManip(name) - except: - return - - nx,ny = im.size() + + nx,ny = ImgManip.image_size(name) if (nx,ny) == (0,0): return @@ -384,7 +380,8 @@ class RTFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc): act_height = y_cm act_width = x_cm/ratio - buf = im.jpg_scale_data(int(act_width*40),int(act_height*40)) + buf = ImgManip.resize_to_jpeg_buffer(name, int(act_width*40), + int(act_height*40)) act_width = twips(act_width) act_height = twips(act_height) diff --git a/src/plugins/WriteCD.py b/src/plugins/WriteCD.py index 55f8c237d..7c03d682a 100644 --- a/src/plugins/WriteCD.py +++ b/src/plugins/WriteCD.py @@ -70,7 +70,6 @@ from GrampsDbUtils import XmlWriter import Mime import const import QuestionDialog -import ImgManip from PluginUtils import register_export _title_string = _("Export to CD") @@ -307,15 +306,6 @@ class PackageWriter: target.close() original.close() - def make_thumbnail(self,dbname,root,path): - img = ImgManip.ImgManip(path) - data = img.jpg_scale_data(const.THUMBSCALE,const.THUMBSCALE) - - uri = URI('burn:///%s/.thumb/%s.jpg' % (dbname,root)) - th = create(uri,OPEN_WRITE) - th.write(data) - th.close() - #------------------------------------------------------------------------- # # Register the plugin