gramps/gramps2/src/ImgManip.py

189 lines
5.5 KiB
Python
Raw Normal View History

2002-10-20 19:55:16 +05:30
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Donald N. Allingham
2002-10-20 19:55:16 +05:30
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# 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-22 00:41:32 +05:30
# $Id$
#-------------------------------------------------------------------------
#
# Standard python modules
#
#-------------------------------------------------------------------------
2002-10-20 19:55:16 +05:30
import os
import md5
2006-03-29 08:51:29 +05:30
import tempfile
2006-03-22 00:41:32 +05:30
#-------------------------------------------------------------------------
#
# GTK/Gnome modules
#
#-------------------------------------------------------------------------
import gtk
import gobject
2002-10-20 19:55:16 +05:30
2006-03-22 00:41:32 +05:30
#-------------------------------------------------------------------------
#
# gramps modules
#
#-------------------------------------------------------------------------
import const
2006-03-03 05:53:04 +05:30
import Mime
2006-03-03 05:40:52 +05:30
import Config
2005-12-06 12:08:09 +05:30
import Utils
2006-03-22 00:41:32 +05:30
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
2002-10-20 19:55:16 +05:30
class ImgManip:
2006-03-29 08:51:29 +05:30
def __init__(self, source):
2002-10-20 19:55:16 +05:30
self.src = source
2005-12-06 12:08:09 +05:30
try:
2006-03-29 08:51:29 +05:30
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 12:08:09 +05:30
except gobject.GError:
self.width = 0
self.height = 0
2006-03-29 08:51:29 +05:30
def size(self):
return (self.width, self.height)
def fmt_thumbnail(self,dest,width,height,cnv):
2006-03-29 08:51:29 +05:30
scaled = self.img.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)
scaled.save(dest,cnv)
def fmt_data(self, cnv):
fd, dest = tempfile.mkstemp()
self.img.save(dest,cnv)
fh = open(dest,mode='rb')
2006-03-29 08:51:29 +05:30
data = fh.read()
fh.close()
try:
os.unlink(dest)
except:
pass
2006-03-29 08:51:29 +05:30
return data
def fmt_scale_data(self, x, y, cnv):
fd, dest = tempfile.mkstemp()
2006-04-04 09:44:51 +05:30
scaled = self.img.scale_simple(x, y, gtk.gdk.INTERP_BILINEAR)
scaled.save(dest,cnv)
fh = open(dest,mode='rb')
2006-03-29 08:51:29 +05:30
data = fh.read()
fh.close()
try:
os.unlink(dest)
except:
pass
2006-03-29 08:51:29 +05:30
return data
def jpg_thumbnail(self, dest, width, height):
self.fmt_thumbnail(dest, width, height, "jpeg")
2002-10-20 19:55:16 +05:30
def jpg_data(self):
return self.fmt_data("jpeg")
def png_data(self):
return self.fmt_data("png")
2006-03-29 08:51:29 +05:30
def jpg_scale_data(self, x, y):
return self.fmt_scale_data(x, y, "jpeg")
2002-10-20 19:55:16 +05:30
def png_scale_data(self,x,y):
2006-03-29 08:51:29 +05:30
return self.fmt_scale_data(x, y, "png")
2002-10-20 19:55:16 +05:30
def _build_thumb_path(path):
m = md5.md5(path)
2006-03-29 08:51:29 +05:30
return os.path.join(const.thumb_dir, m.hexdigest()+'.png')
2005-12-06 12:08:09 +05:30
def run_thumbnailer(mtype, frm, to, size=const.thumbScale):
if const.use_thumbnailer and os.path.isfile(frm):
2006-03-29 08:51:29 +05:30
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)
2006-03-29 08:51:29 +05:30
return True
else:
return False
2005-12-06 12:08:09 +05:30
return False
2006-03-29 08:51:29 +05:30
def set_thumbnail_image(path, mtype=None):
2005-12-06 12:08:09 +05:30
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
2006-03-29 08:51:29 +05:30
def get_thumbnail_image(path, mtype=None):
filename = _build_thumb_path(path)
2005-12-06 12:08:09 +05:30
try:
2005-12-06 12:08:09 +05:30
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)
2005-12-06 12:08:09 +05:30
except (gobject.GError, OSError):
if mtype:
2006-03-03 05:53:04 +05:30
return Mime.find_mime_type_pixbuf(mtype)
2005-12-06 12:08:09 +05:30
else:
return gtk.gdk.pixbuf_new_from_file(os.path.join(
const.image_dir,"document.png"))
2006-03-29 08:51:29 +05:30
def get_thumbnail_path(path, mtype=None):
filename = _build_thumb_path(path)
if not os.path.isfile(filename):
2005-12-06 12:08:09 +05:30
set_thumbnail_image(path,mtype)
return filename
def get_thumb_from_obj(obj):
mtype = obj.get_mime_type()
2006-03-29 08:51:29 +05:30
if mtype.startswith("image/"):
image = get_thumbnail_image(obj.get_path())
else:
2006-03-03 05:53:04 +05:30
image = Mime.find_mime_type_pixbuf(mtype)
if not image:
image = gtk.gdk.pixbuf_new_from_file(const.icon)
return image