2002-10-20 14:25:16 +00:00
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-03-02 23:37:16 +00:00
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
2002-10-20 14:25:16 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2007-09-10 22:14:33 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
2002-10-20 14:25:16 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
2006-03-21 19:11:32 +00:00
|
|
|
# $Id$
|
|
|
|
|
2007-09-10 22:14:33 +00:00
|
|
|
"""
|
|
|
|
Image manipulation routines.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2006-03-21 19:11:32 +00:00
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2002-10-20 14:25:16 +00:00
|
|
|
import os
|
2004-11-25 02:06:05 +00:00
|
|
|
import md5
|
2006-03-29 03:21:29 +00:00
|
|
|
import tempfile
|
2006-03-21 19:11:32 +00:00
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK/Gnome modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-08-18 03:55:55 +00:00
|
|
|
import gtk
|
2005-02-24 00:25:34 +00:00
|
|
|
import gobject
|
2002-10-20 14:25:16 +00:00
|
|
|
|
2006-03-21 19:11:32 +00:00
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import const
|
2006-03-03 00:23:04 +00:00
|
|
|
import Mime
|
2006-03-03 00:10:52 +00:00
|
|
|
import Config
|
2005-12-06 06:38:09 +00:00
|
|
|
import Utils
|
|
|
|
|
2006-03-21 19:11:32 +00:00
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2007-09-10 22:14:33 +00:00
|
|
|
# ImgManip
|
2006-03-21 19:11:32 +00:00
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2002-10-20 14:25:16 +00:00
|
|
|
class ImgManip:
|
2007-09-10 22:14:33 +00:00
|
|
|
"""
|
|
|
|
Image manipulation class
|
|
|
|
"""
|
2006-03-29 03:21:29 +00:00
|
|
|
def __init__(self, source):
|
2002-10-20 14:25:16 +00:00
|
|
|
self.src = source
|
2005-12-06 06:38:09 +00:00
|
|
|
try:
|
2006-03-29 03:21:29 +00:00
|
|
|
self.img = gtk.gdk.pixbuf_new_from_file(self.src)
|
|
|
|
self.width = self.img.get_width()
|
|
|
|
self.height = self.img.get_height()
|
2005-12-06 06:38:09 +00:00
|
|
|
except gobject.GError:
|
2006-04-21 04:14:00 +00:00
|
|
|
self.width = 0
|
|
|
|
self.height = 0
|
2006-03-29 03:21:29 +00:00
|
|
|
|
|
|
|
def size(self):
|
2007-09-10 22:14:33 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2006-03-29 03:21:29 +00:00
|
|
|
return (self.width, self.height)
|
|
|
|
|
2007-09-10 22:14:33 +00:00
|
|
|
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):
|
2006-03-29 03:21:29 +00:00
|
|
|
fd, dest = tempfile.mkstemp()
|
2007-09-10 22:14:33 +00:00
|
|
|
self.img.save(dest, "png")
|
|
|
|
fh = open(dest, mode='rb')
|
2006-03-29 03:21:29 +00:00
|
|
|
data = fh.read()
|
|
|
|
fh.close()
|
2006-04-05 04:41:56 +00:00
|
|
|
try:
|
|
|
|
os.unlink(dest)
|
|
|
|
except:
|
|
|
|
pass
|
2006-03-29 03:21:29 +00:00
|
|
|
return data
|
|
|
|
|
2007-09-10 22:14:33 +00:00
|
|
|
def jpg_scale_data(self, x, y):
|
2006-03-29 03:21:29 +00:00
|
|
|
fd, dest = tempfile.mkstemp()
|
2007-05-15 04:17:12 +00:00
|
|
|
scaled = self.img.scale_simple(int(x), int(y), gtk.gdk.INTERP_BILINEAR)
|
2007-09-10 22:14:33 +00:00
|
|
|
scaled.save(dest, 'jpeg')
|
|
|
|
fh = open(dest, mode='rb')
|
2006-03-29 03:21:29 +00:00
|
|
|
data = fh.read()
|
|
|
|
fh.close()
|
2006-04-05 04:41:56 +00:00
|
|
|
try:
|
|
|
|
os.unlink(dest)
|
|
|
|
except:
|
|
|
|
pass
|
2006-03-29 03:21:29 +00:00
|
|
|
return data
|
|
|
|
|
2004-11-25 02:06:05 +00:00
|
|
|
def _build_thumb_path(path):
|
|
|
|
m = md5.md5(path)
|
2007-09-08 05:54:02 +00:00
|
|
|
return os.path.join(const.THUMB_DIR, m.hexdigest()+'.png')
|
2005-12-06 06:38:09 +00:00
|
|
|
|
2007-09-08 05:54:02 +00:00
|
|
|
def run_thumbnailer(mtype, frm, to, size=const.THUMBSCALE):
|
|
|
|
if const.USE_THUMBNAILER and os.path.isfile(frm):
|
2006-03-29 03:21:29 +00:00
|
|
|
sublist = {
|
2007-09-10 22:14:33 +00:00
|
|
|
'%s' : "%dx%d" % (int(size), int(size)),
|
|
|
|
'%u' : frm,
|
|
|
|
'%o' : to,
|
2006-03-29 03:21:29 +00:00
|
|
|
}
|
|
|
|
|
2007-09-10 22:14:33 +00:00
|
|
|
base = '/desktop/gnome/thumbnailers/%s' % mtype.replace('/', '@')
|
2006-03-29 03:21:29 +00:00
|
|
|
|
|
|
|
cmd = Config.get_string(base + '/command')
|
|
|
|
enable = Config.get_bool(base + '/enable')
|
|
|
|
|
|
|
|
if cmd and enable:
|
2007-09-10 22:14:33 +00:00
|
|
|
cmdlist = map(lambda x: sublist.get(x, x), cmd.split())
|
2006-03-29 22:51:27 +00:00
|
|
|
os.spawnvpe(os.P_WAIT, cmdlist[0], cmdlist, os.environ)
|
2006-03-29 03:21:29 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2005-12-06 06:38:09 +00:00
|
|
|
return False
|
|
|
|
|
2006-03-29 03:21:29 +00:00
|
|
|
def set_thumbnail_image(path, mtype=None):
|
2005-12-06 06:38:09 +00:00
|
|
|
if mtype and not mtype.startswith('image/'):
|
2007-09-10 22:14:33 +00:00
|
|
|
run_thumbnailer(mtype, path, _build_thumb_path(path))
|
2005-12-06 06:38:09 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
pixbuf = gtk.gdk.pixbuf_new_from_file(path)
|
|
|
|
w = pixbuf.get_width()
|
|
|
|
h = pixbuf.get_height()
|
2007-09-10 22:14:33 +00:00
|
|
|
scale = const.THUMBSCALE / (float(max(w, h)))
|
2005-12-06 06:38:09 +00:00
|
|
|
|
|
|
|
pw = int(w*scale)
|
|
|
|
ph = int(h*scale)
|
|
|
|
|
2007-09-10 22:14:33 +00:00
|
|
|
pixbuf = pixbuf.scale_simple(pw, ph, gtk.gdk.INTERP_BILINEAR)
|
|
|
|
pixbuf.save(_build_thumb_path(path), "png")
|
2005-12-06 06:38:09 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2006-03-29 03:21:29 +00:00
|
|
|
def get_thumbnail_image(path, mtype=None):
|
2004-11-25 02:06:05 +00:00
|
|
|
filename = _build_thumb_path(path)
|
2005-12-06 06:38:09 +00:00
|
|
|
|
2005-02-24 00:25:34 +00:00
|
|
|
try:
|
2005-12-06 06:38:09 +00:00
|
|
|
path = Utils.find_file( path)
|
|
|
|
if not os.path.isfile(filename):
|
2007-09-10 22:14:33 +00:00
|
|
|
set_thumbnail_image(path, mtype)
|
2005-12-06 06:38:09 +00:00
|
|
|
elif os.path.getmtime(path) > os.path.getmtime(filename):
|
2007-09-10 22:14:33 +00:00
|
|
|
set_thumbnail_image(path, mtype)
|
2005-02-24 00:25:34 +00:00
|
|
|
return gtk.gdk.pixbuf_new_from_file(filename)
|
2005-12-06 06:38:09 +00:00
|
|
|
except (gobject.GError, OSError):
|
|
|
|
if mtype:
|
2006-03-03 00:23:04 +00:00
|
|
|
return Mime.find_mime_type_pixbuf(mtype)
|
2005-12-06 06:38:09 +00:00
|
|
|
else:
|
2006-03-02 23:37:16 +00:00
|
|
|
return gtk.gdk.pixbuf_new_from_file(os.path.join(
|
2007-09-10 22:14:33 +00:00
|
|
|
const.IMAGE_DIR, "document.png"))
|
2005-08-18 05:58:28 +00:00
|
|
|
|
2006-03-29 03:21:29 +00:00
|
|
|
def get_thumbnail_path(path, mtype=None):
|
2005-08-18 05:58:28 +00:00
|
|
|
filename = _build_thumb_path(path)
|
|
|
|
if not os.path.isfile(filename):
|
2007-09-10 22:14:33 +00:00
|
|
|
set_thumbnail_image(path, mtype)
|
2005-08-18 05:58:28 +00:00
|
|
|
return filename
|
2006-02-02 05:20:42 +00:00
|
|
|
|
|
|
|
def get_thumb_from_obj(obj):
|
|
|
|
mtype = obj.get_mime_type()
|
2006-03-29 03:21:29 +00:00
|
|
|
if mtype.startswith("image/"):
|
2006-02-02 05:20:42 +00:00
|
|
|
image = get_thumbnail_image(obj.get_path())
|
|
|
|
else:
|
2006-03-03 00:23:04 +00:00
|
|
|
image = Mime.find_mime_type_pixbuf(mtype)
|
2006-02-02 05:20:42 +00:00
|
|
|
if not image:
|
2007-09-08 05:54:02 +00:00
|
|
|
image = gtk.gdk.pixbuf_new_from_file(const.ICON)
|
2006-02-02 05:20:42 +00:00
|
|
|
return image
|