* 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  <don@gramps-project.org>


svn: r8959
This commit is contained in:
Don Allingham 2007-09-11 03:51:35 +00:00
parent 992322d9b8
commit 643c75099c
11 changed files with 91 additions and 171 deletions

View File

@ -1,3 +1,14 @@
2007-09-10 Don Allingham <don@gramps-project.org>
* 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 <don@gramps-project.org>
* src/ThumbNails.py: Added to handle the thumbnailing routines
* src/DataViews/_MediaView.py: thumbnail updates

View File

@ -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

View File

@ -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

View File

@ -75,6 +75,7 @@ gdir_PYTHON = \
Spell.py\
SubstKeywords.py\
TipOfDay.py\
ThumbNails.py\
ToolTips.py\
TransUtils.py\
TreeTips.py\

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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:

View File

@ -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)

View File

@ -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