* 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:
parent
992322d9b8
commit
643c75099c
11
ChangeLog
11
ChangeLog
@ -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>
|
2007-09-10 Don Allingham <don@gramps-project.org>
|
||||||
* src/ThumbNails.py: Added to handle the thumbnailing routines
|
* src/ThumbNails.py: Added to handle the thumbnailing routines
|
||||||
* src/DataViews/_MediaView.py: thumbnail updates
|
* src/DataViews/_MediaView.py: thumbnail updates
|
||||||
|
@ -46,6 +46,7 @@ src/Spell.py
|
|||||||
src/SubstKeywords.py
|
src/SubstKeywords.py
|
||||||
src/TipOfDay.py
|
src/TipOfDay.py
|
||||||
src/ToolTips.py
|
src/ToolTips.py
|
||||||
|
src/ThumbNails.py
|
||||||
src/TransUtils.py
|
src/TransUtils.py
|
||||||
src/TreeTips.py
|
src/TreeTips.py
|
||||||
src/Utils.py
|
src/Utils.py
|
||||||
|
208
src/ImgManip.py
208
src/ImgManip.py
@ -24,14 +24,12 @@
|
|||||||
Image manipulation routines.
|
Image manipulation routines.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Standard python modules
|
# Standard python modules
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
import os
|
import os
|
||||||
import md5
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
@ -44,151 +42,79 @@ import gobject
|
|||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# gramps modules
|
# resize_to_jpeg
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
import const
|
def resize_to_jpeg(source, destination, width, height):
|
||||||
import Mime
|
|
||||||
import Config
|
|
||||||
import Utils
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
#
|
|
||||||
# ImgManip
|
|
||||||
#
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
class ImgManip:
|
|
||||||
"""
|
"""
|
||||||
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):
|
img = gtk.gdk.pixbuf_new_from_file(source)
|
||||||
self.src = source
|
scaled = img.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)
|
||||||
try:
|
scaled.save(destination, 'jpeg')
|
||||||
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
|
|
||||||
|
|
||||||
def size(self):
|
#-------------------------------------------------------------------------
|
||||||
"""
|
#
|
||||||
Returns a tuple consisting of the width, height of the image in pixels.
|
# image_size
|
||||||
|
#
|
||||||
@rtype width and height of the image in pixels
|
#-------------------------------------------------------------------------
|
||||||
@return tuple of two integers
|
def image_size(source):
|
||||||
"""
|
"""
|
||||||
return (self.width, self.height)
|
Returns the width and size of the specified image.
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
@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:
|
try:
|
||||||
path = Utils.find_file( path)
|
img = gtk.gdk.pixbuf_new_from_file(source)
|
||||||
if not os.path.isfile(filename):
|
width = self.img.get_width()
|
||||||
set_thumbnail_image(path, mtype)
|
height = self.img.get_height()
|
||||||
elif os.path.getmtime(path) > os.path.getmtime(filename):
|
except gobject.GError:
|
||||||
set_thumbnail_image(path, mtype)
|
width = 0
|
||||||
return gtk.gdk.pixbuf_new_from_file(filename)
|
height = 0
|
||||||
except (gobject.GError, OSError):
|
return (width, height)
|
||||||
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"))
|
|
||||||
|
|
||||||
def get_thumbnail_path(path, mtype=None):
|
#-------------------------------------------------------------------------
|
||||||
filename = _build_thumb_path(path)
|
#
|
||||||
if not os.path.isfile(filename):
|
# resize_to_jpeg_buffer
|
||||||
set_thumbnail_image(path, mtype)
|
#
|
||||||
return filename
|
#-------------------------------------------------------------------------
|
||||||
|
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
|
|
||||||
|
@ -75,6 +75,7 @@ gdir_PYTHON = \
|
|||||||
Spell.py\
|
Spell.py\
|
||||||
SubstKeywords.py\
|
SubstKeywords.py\
|
||||||
TipOfDay.py\
|
TipOfDay.py\
|
||||||
|
ThumbNails.py\
|
||||||
ToolTips.py\
|
ToolTips.py\
|
||||||
TransUtils.py\
|
TransUtils.py\
|
||||||
TreeTips.py\
|
TreeTips.py\
|
||||||
|
@ -44,7 +44,7 @@ import gtk
|
|||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
import const
|
import const
|
||||||
import ImgManip
|
import ThumbNails
|
||||||
from DisplayModels import MediaModel
|
from DisplayModels import MediaModel
|
||||||
from _BaseSelector import BaseSelector
|
from _BaseSelector import BaseSelector
|
||||||
|
|
||||||
@ -90,6 +90,6 @@ class SelectObject(BaseSelector):
|
|||||||
return
|
return
|
||||||
handle = id_list[0]
|
handle = id_list[0]
|
||||||
obj = self.get_from_handle_func()(handle)
|
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)
|
self.preview.set_from_pixbuf(pix)
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#
|
#
|
||||||
# Gramps - a GTK+/GNOME based genealogy program
|
# 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
|
# 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
|
# 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
|
# 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.
|
Handles generation and access to thumbnails used in GRAMPS.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Standard python modules
|
# Standard python modules
|
||||||
|
@ -374,8 +374,7 @@ class HtmlDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
|
|||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
img = ImgManip.ImgManip(name)
|
ImgManip.resize_to_jpeg(name, newfile, size, size)
|
||||||
img.jpg_thumbnail("%s%s%s" % (imdir,os.path.sep,refname),size,size)
|
|
||||||
except:
|
except:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -466,6 +466,7 @@ class LaTeXDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
|
|||||||
|
|
||||||
def add_media_object(self,name,pos,x,y):
|
def add_media_object(self,name,pos,x,y):
|
||||||
"""Add photo to report"""
|
"""Add photo to report"""
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pic = ImgManip.ImgManip(name)
|
pic = ImgManip.ImgManip(name)
|
||||||
|
@ -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
|
# try to open the image. If the open fails, it probably wasn't
|
||||||
# a valid image (could be a PDF, or a non-image)
|
# a valid image (could be a PDF, or a non-image)
|
||||||
try:
|
(x,y) = ImgManip.image_size(name)
|
||||||
image = ImgManip.ImgManip(name)
|
if (x,y) == (0,0):
|
||||||
(x,y) = image.size()
|
|
||||||
ratio = float(x_cm)*float(y)/(float(y_cm)*float(x))
|
|
||||||
except:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if ratio < 1:
|
if ratio < 1:
|
||||||
|
@ -362,12 +362,8 @@ class RTFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
|
|||||||
#
|
#
|
||||||
#--------------------------------------------------------------------
|
#--------------------------------------------------------------------
|
||||||
def add_media_object(self,name,pos,x_cm,y_cm):
|
def add_media_object(self,name,pos,x_cm,y_cm):
|
||||||
try:
|
|
||||||
im = ImgManip.ImgManip(name)
|
nx,ny = ImgManip.image_size(name)
|
||||||
except:
|
|
||||||
return
|
|
||||||
|
|
||||||
nx,ny = im.size()
|
|
||||||
|
|
||||||
if (nx,ny) == (0,0):
|
if (nx,ny) == (0,0):
|
||||||
return
|
return
|
||||||
@ -384,7 +380,8 @@ class RTFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc):
|
|||||||
act_height = y_cm
|
act_height = y_cm
|
||||||
act_width = x_cm/ratio
|
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_width = twips(act_width)
|
||||||
act_height = twips(act_height)
|
act_height = twips(act_height)
|
||||||
|
@ -70,7 +70,6 @@ from GrampsDbUtils import XmlWriter
|
|||||||
import Mime
|
import Mime
|
||||||
import const
|
import const
|
||||||
import QuestionDialog
|
import QuestionDialog
|
||||||
import ImgManip
|
|
||||||
from PluginUtils import register_export
|
from PluginUtils import register_export
|
||||||
|
|
||||||
_title_string = _("Export to CD")
|
_title_string = _("Export to CD")
|
||||||
@ -307,15 +306,6 @@ class PackageWriter:
|
|||||||
target.close()
|
target.close()
|
||||||
original.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
|
# Register the plugin
|
||||||
|
Loading…
Reference in New Issue
Block a user