Abstracted out the use of Imagick vs. PIL

svn: r741
This commit is contained in:
Don Allingham 2002-02-02 01:14:15 +00:00
parent b54a2f8527
commit d6f1a8b1c5
15 changed files with 2085 additions and 1346 deletions

View File

@ -249,7 +249,11 @@ class Gallery(ImageSelect):
def add_thumbnail(self, photo):
object = photo.getReference()
name = utils.thumb_path(self.db.getSavePath(),object)
thumb = GdkImlib.Image(name)
try:
thumb = GdkImlib.Image(name)
except IOError,msg:
gnome.ui.GnomeErrorDialog(_("Could not import %s - %s") % (name,msg))
return
self.icon_cache.append(thumb)
description = object.getDescription()
if len(description) > 50:

106
gramps/src/ImgManip.py Normal file
View File

@ -0,0 +1,106 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000 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
# 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
#
import os
import const
#-------------------------------------------------------------------------
#
# Check for the python imaging library
#
#-------------------------------------------------------------------------
try:
import PIL.Image
no_pil = 0
except:
import popen2
import GDK
import GTK
import gtk
import GdkImlib
no_pil = 1
class ImgManip:
def __init__(self,source):
self.source = source
def jpg_thumbnail(self,dest,width,height):
if no_pil:
w = int(width)
h = int(height)
cmd = "%s -geometry %dx%d '%s' 'jpg:%s'" % (const.convert,w,h,self.source,dest)
os.system(cmd)
else:
im = PIL.Image.open(self.source)
im.thumbnail((width,height))
if im.mode != 'RGB':
im.draft('RGB',im.size)
im = im.convert("RGB")
im.save(dest,"JPEG")
def png_thumbnail(self,dest,width,height):
if no_pil:
w = int(width)
h = int(height)
cmd = "%s -geometry %dx%d '%s' 'png:%s'" % (const.convert,w,h,self.source,dest)
os.system(cmd)
else:
im = PIL.Image.open(self.source)
im.thumbnail((width,height))
if im.mode != 'RGB':
im.draft('RGB',im.size)
im = im.convert("RGB")
im.save(dest,"PNG")
def size(self):
if no_pil:
img = GdkImlib.Image(self.source)
return (img.rgb_width,img.rgb_height)
else:
return PIL.Image.open(self.source).size
def jpg_data(self):
if no_pil:
cmd = "%s '%s' 'jpg:-'" % (const.convert,self.source)
r,w = popen2.popen2(cmd)
buf = r.read()
r.close()
w.close()
return buf
else:
im = PIL.Image.open(self.source)
return im.tostring("jpeg","RGB")
def png_scale_data(self,x,y):
if no_pil:
cmd = "%s -geometry %dx%d'%s' 'jpg:-'" % (const.convert,x,y,self.source)
r,w = popen2.popen2(cmd)
buf = r.read()
r.close()
w.close()
return buf
else:
im = PIL.Image.open(self.source)
im.thumbnail((width,height))
if im.mode != 'RGB':
im.draft('RGB',im.size)
im = im.convert("RGB")
return im.tostring("png","RGB")

View File

@ -41,19 +41,10 @@ from gnome.ui import GnomeErrorDialog, GnomeWarningDialog
#-------------------------------------------------------------------------
import const
import utils
import ImgManip
from intl import gettext
_ = gettext
#-------------------------------------------------------------------------
#
# Check for the python imaging library
#
#-------------------------------------------------------------------------
try:
import PIL.Image
no_pil = 0
except:
no_pil = 1
#-------------------------------------------------------------------------
#
@ -88,7 +79,7 @@ def import_media_object(filename,path,base):
path = "%s/%s" % (thumb,base)
mk_thumb(filename,path,const.thumbScale)
except:
GnomeErrorDialog(_("Error creating the thumbnail : %s") + "\n" + msg)
GnomeErrorDialog(_("Error creating the thumbnail : %s"))
return ""
try:
@ -159,20 +150,12 @@ def mk_thumb(source,dest,size):
if not os.path.exists(source):
GnomeErrorDialog(_("Could not create a thumbnail for %s\nThe file has been moved or deleted") % source)
if no_pil:
cmd = "%s -geometry %dx%d '%s' '%s'" % (const.convert,size,size,source,dest)
os.system(cmd)
else:
try:
im = PIL.Image.open(source)
im.thumbnail((size,size))
if im.mode != 'RGB':
im.draft('RGB',im.size)
im = im.convert("RGB")
im.save(dest,"JPEG")
except:
GnomeErrorDialog(_("Could not create a thumbnail for %s") % source)
return
try:
img = ImgManip.ImgManip(source)
img.jpg_thumbnail(dest,size,size)
except:
GnomeErrorDialog(_("Could not create a thumbnail for %s") % source)
return
#-------------------------------------------------------------------------
#

View File

@ -26,7 +26,8 @@ import utils
def need_to_run():
if gnome.config.get_string("/gramps/config/startup") == None:
return 1
if gnome.config.get_int("/gramps/config/startup") < const.startup:
val = gnome.config.get_int("/gramps/config/startup")
if val < const.startup:
return 1
return 0

View File

@ -34,20 +34,10 @@ from latin_utf8 import latin_to_utf8
import const
import string
import Plugins
import ImgManip
import intl
_ = intl.gettext
#-------------------------------------------------------------------------
#
# Attemp to import the Python Imaging Library
#
#-------------------------------------------------------------------------
try:
import PIL.Image
no_pil = 0
except:
no_pil = 1
#-------------------------------------------------------------------------
#
# Class Definitions
@ -102,23 +92,16 @@ class AbiWordDoc(TextDoc):
file = file_tuple[0]
width = file_tuple[1]
height = file_tuple[2]
base = "/tmp/%s.png" % os.path.basename(file)
base = "%s/%s_png" % (os.path.dirname(file),os.path.basename(file))
tag = string.replace(base,'.','_')
if no_pil:
cmd = "%s -geometry %dx%d '%s' '%s'" % (const.convert,width,height,file,base)
os.system(cmd)
else:
im = PIL.Image.open(file)
im.thumbnail((width,height))
im.save(base,"PNG")
img = ImgManip.ImgManip(file)
buf = img.png_scale_data(width,height)
self.f.write('<d name="')
self.f.write(tag)
self.f.write('" mime-type="image/png" base64="yes">\n')
f = open(base,"rb")
base64.encode(f,self.f)
f.close()
self.f.write(base64.encodestring(buf))
os.unlink(base)
self.f.write('</d>\n')
self.f.write('</data>\n')
@ -238,5 +221,4 @@ class AbiWordDoc(TextDoc):
self.f.write('; text-decoration:underline')
self.f.write('">')
Plugins.register_text_doc(_("AbiWord"),AbiWordDoc,0,1,1)

View File

@ -24,22 +24,12 @@ import re
import utils
import gnome.ui
import Plugins
import ImgManip
from TextDoc import *
from intl import gettext
_ = gettext
from TextDoc import *
#------------------------------------------------------------------------
#
# Attempt to load the Python Imaging Library for the handling of photos.
#
#------------------------------------------------------------------------
try:
import PIL.Image
no_pil = 0
except:
no_pil = 1
t_header_line_re = re.compile(r"(.*)<TITLE>(.*)</TITLE>(.*)", re.DOTALL|re.IGNORECASE|re.MULTILINE)
@ -238,41 +228,22 @@ class HtmlDoc(TextDoc):
self.f.close()
def add_photo(self,name,pos,x,y):
if no_pil:
return
self.empty = 0
try:
im = PIL.Image.open(name)
except:
return
nx,ny = im.size
scale = float(nx)/float(ny)
if scale > 1.0:
scale = 1.0/scale
act_width = float(x)
act_height = float(y * scale)
else:
act_width = float(x * scale)
act_height = float(y)
cmtopt = float(150.0/2.54)
pixx = int(act_width*cmtopt)
pixy = int(act_height*cmtopt)
im.thumbnail((pixx,pixy))
size = int(max(x,y) * float(150.0/2.54))
refname = "is%s" % os.path.basename(name)
imdir = self.base + os.sep + "images"
if not os.path.isdir(imdir):
try:
os.mkdir(imdir)
except:
return
refname = "is%s" % os.path.basename(name)
try:
im.save(imdir + os.sep + refname)
img = ImgManip.ImgManip(name)
img.jpg_thumbnail(imdir + os.sep + refname,size,size)
except:
return
@ -283,8 +254,8 @@ class HtmlDoc(TextDoc):
else:
xtra = ''
self.f.write('<img src="images/%s" border="0" width="%d" height="%d"%s>\n' % \
(refname,pixx,pixy,xtra))
self.f.write('<img src="images/%s" border="0""%s>\n' % \
(refname,xtra))
def start_table(self,name,style):
self.tbl = self.table_styles[style]

View File

@ -27,15 +27,10 @@ import os
import gzip
from TarFile import TarFile
import Plugins
import ImgManip
import intl
_ = intl.gettext
try:
import PIL.Image
no_pil = 0
except:
no_pil = 1
def points(val):
inch = float(val)/2.54
return (int(inch*72))
@ -356,11 +351,9 @@ class KwordDoc(TextDoc):
pass
def add_photo(self,name,pos,x,y):
if no_pil:
return
im = PIL.Image.open(name)
nx,ny = im.size
im = ImgManip.ImgMapip(name)
nx,ny = im.size()
scale = float(nx)/float(ny)
x = points(x)

View File

@ -28,13 +28,9 @@ import const
import utils
import Plugins
import intl
_ = intl.gettext
import ImgManip
try:
import PIL.Image
no_pil = 0
except:
no_pil = 1
_ = intl.gettext
try:
from codecs import *
@ -502,13 +498,12 @@ class OpenOfficeDoc(TextDoc):
height = file_tuple[2]
base = os.path.basename(file)
image_name = self.tempdir + os.sep + "Pictures" + os.sep + base
if no_pil:
cmd = "%s -geometry %dx%d '%s' '%s'" % (const.convert,width,height,file,image_name)
os.system(cmd)
else:
im = PIL.Image.open(file)
im.thumbnail((width,height))
im.save(image_name,"JPEG")
try:
img = ImgManip.ImgManip(file)
img.jpg_thumbnail(image_name)
except:
pass
def _write_manifest(self):
file = self.tempdir + os.sep + "META-INF" + os.sep + "manifest.xml"

View File

@ -40,17 +40,6 @@ from reportlab.lib.colors import Color
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
import reportlab.lib.styles
#------------------------------------------------------------------------
#
# Attempt to load the python imaging library
#
#------------------------------------------------------------------------
try:
import PIL.Image
no_pil = 0
except:
no_pil = 1
#------------------------------------------------------------------------
#
# GrampsDocTemplate
@ -267,22 +256,20 @@ class PdfDoc(TextDoc):
self.col = self.col + self.span
def add_photo(self,name,pos,x,y):
if no_pil == 0:
im = PIL.Image.open(name)
img = ImgManip.ImgManip(name)
nx,ny = img.size()
scale = float(nx)/float(ny)
if scale > 1.0:
scale = 1.0/scale
act_width = x
act_height = y * scale
else:
act_width = x * scale
act_height = y
nx,ny = im.size
scale = float(nx)/float(ny)
if scale > 1.0:
scale = 1.0/scale
act_width = x
act_height = y * scale
else:
act_width = x * scale
act_height = y
self.story.append(Image(name,act_width*cm,act_height*cm))
self.story.append(Spacer(1,0.5*cm))
self.image = 1
self.story.append(Image(name,act_width*cm,act_height*cm))
self.story.append(Spacer(1,0.5*cm))
self.image = 1
def write_text(self,text):
self.text = self.text + text

View File

@ -25,20 +25,11 @@
#------------------------------------------------------------------------
from TextDoc import *
import Plugins
import ImgManip
import intl
_ = intl.gettext
#------------------------------------------------------------------------
#
# Attempt to load the Python Imaging Library for the handling of photos.
#
#------------------------------------------------------------------------
try:
import PIL.Image
no_pil = 0
except:
no_pil = 1
#------------------------------------------------------------------------
#
# RTF uses a unit called "twips" for its measurements. According to the
@ -324,17 +315,14 @@ class RTFDoc(TextDoc):
# to JPEG, since it is smaller, and supported by RTF. The data is
# dumped as a string of HEX numbers.
#
# If the PIL library is not loaded, ignore the request to load the
# photo.
#
#--------------------------------------------------------------------
def add_photo(self,name,pos,x_cm,y_cm):
if no_pil:
return
im = PIL.Image.open(name)
nx,ny = im.size
buf = im.tostring("jpeg","RGB")
im = ImgManip.ImgManip(name)
nx,ny = im.size()
buf = im.jpg_data()
scale = float(ny)/float(nx)
if scale > 1:

View File

@ -17,7 +17,7 @@ install:
${INSTALL} *.py *.pyo ${datadir}/filters
uninstall:
rm ${filters}/*.py ${filters}/*.pyo ${filters}/*.glade ${filters}/*.xpm
rm ${filters}/*.py ${filters}/*.pyo
-rmdir ${filters}
clean:

View File

@ -12,13 +12,12 @@ pycomp = ${srcdir}/py-compile
all:
${pycomp} *.py
install:
${INSTALL} -d ${plugins}
${INSTALL} *.py *.pyo *.glade ${plugins}
uninstall:
rm ${plugins}/*.py ${plugins}/*.pyo ${plugins}/*.glade ${plugins}/*.xpm
rm ${plugins}/*.py ${plugins}/*.pyo ${plugins}/*.glade
-rmdir ${plugins}
clean:

View File

@ -671,7 +671,7 @@ class WebReport(Report):
#------------------------------------------------------------------------
def dump_index(self,person_list,styles,template,html_dir):
doc = HtmlLinkDoc(styles,template)
doc = HtmlLinkDoc(self.selected_style,None,template,None)
doc.set_title(_("Family Tree Index"))
doc.open(html_dir + os.sep + "index.html")
@ -728,7 +728,7 @@ class WebReport(Report):
filter(self.db,self.person,ind_list,self.max_gen)
self.progress_bar_setup(float(len(ind_list)))
doc = HtmlLinkDoc(self.selected_style,self.template_name)
doc = HtmlLinkDoc(self.selected_style,None,self.template_name,None)
self.add_styles(doc)
doc.build_style_declaration()
@ -736,7 +736,7 @@ class WebReport(Report):
for l in ind_list:
my_map[l] = 1
for person in ind_list:
tdoc = HtmlLinkDoc(self.selected_style,None,doc)
tdoc = HtmlLinkDoc(self.selected_style,None,None,None,doc)
idoc = IndividualPage(person, self.photos, self.restrict,
self.private, self.srccomments,
self.include_link, my_map, dir_name, tdoc)

File diff suppressed because it is too large Load Diff