2001-05-13 07:26:57 +05:30
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK/Gnome modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
|
|
|
import const
|
|
|
|
import intl
|
2001-05-31 08:10:08 +05:30
|
|
|
from gnome.ui import *
|
2001-05-13 07:26:57 +05:30
|
|
|
|
|
|
|
_ = intl.gettext
|
|
|
|
|
2001-05-24 03:46:19 +05:30
|
|
|
try:
|
2001-06-10 06:55:35 +05:30
|
|
|
import PIL.Image
|
2001-05-24 03:46:19 +05:30
|
|
|
no_pil = 0
|
|
|
|
except:
|
|
|
|
no_pil = 1
|
|
|
|
|
2001-05-13 07:26:57 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# import_photo
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def import_photo(filename,path,prefix):
|
|
|
|
import gnome.mime
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
type = gnome.mime.type_of_file(filename)
|
|
|
|
if type[0:6] != "image/":
|
2001-05-24 03:46:19 +05:30
|
|
|
GnomeErrorDialog(_("Currently only image files are supported"))
|
2001-05-13 07:26:57 +05:30
|
|
|
return None
|
|
|
|
|
|
|
|
for index in range(0,1000):
|
2001-08-31 09:10:23 +05:30
|
|
|
name = "%s%s%s_%d.jpg" % (path,os.sep,prefix,index)
|
2001-05-13 07:26:57 +05:30
|
|
|
if os.path.exists(name) == 0:
|
|
|
|
break
|
|
|
|
|
2001-08-31 09:10:23 +05:30
|
|
|
thumb = "%s%s.thumb" % (path,os.sep)
|
2001-06-18 00:43:49 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
if not os.path.exists(thumb):
|
|
|
|
os.mkdir(thumb)
|
|
|
|
except IOError,msg:
|
|
|
|
GnomeErrorDialog(_("Could not create %s") % thumb + "\n" + str(msg))
|
|
|
|
except:
|
|
|
|
GnomeErrorDialog(_("Could not create %s") % thumb)
|
2001-05-31 23:27:14 +05:30
|
|
|
|
2001-05-13 07:26:57 +05:30
|
|
|
try:
|
2001-08-31 09:10:23 +05:30
|
|
|
path = "%s%s%s" % (thumb,os.sep,base)
|
2001-05-31 23:27:14 +05:30
|
|
|
|
|
|
|
mk_thumb(filename,path,const.thumbScale)
|
|
|
|
|
2001-05-13 07:26:57 +05:30
|
|
|
if type == "image/jpeg":
|
|
|
|
shutil.copy(filename,name)
|
|
|
|
else:
|
2001-05-24 03:46:19 +05:30
|
|
|
if no_pil:
|
2001-05-31 19:08:24 +05:30
|
|
|
cmd = "%s '%s' '%s'" % (const.convert,filename,name)
|
2001-05-24 03:46:19 +05:30
|
|
|
os.system(cmd)
|
|
|
|
else:
|
2001-06-10 06:55:35 +05:30
|
|
|
PIL.Image.open(filename).save(name)
|
2001-05-13 07:26:57 +05:30
|
|
|
except:
|
|
|
|
return None
|
|
|
|
return name
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# scale_image
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def scale_image(path,size):
|
|
|
|
import GdkImlib
|
2001-05-31 08:10:08 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
image1 = GdkImlib.Image(path)
|
|
|
|
except:
|
2001-06-14 22:42:32 +05:30
|
|
|
GnomeWarningDialog(_("Could not load image file %s") % path)
|
2001-05-31 08:10:08 +05:30
|
|
|
return
|
2001-05-13 07:26:57 +05:30
|
|
|
|
|
|
|
width = image1.rgb_width
|
|
|
|
height = image1.rgb_height
|
|
|
|
|
|
|
|
scale = size / float(max(width,height))
|
|
|
|
image2 = image1.clone_scaled_image(int(scale*width), int(scale*height))
|
|
|
|
return image2
|
2001-05-31 08:10:08 +05:30
|
|
|
|
2001-05-31 23:27:14 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# scale_image
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def mk_thumb(source,dest,size):
|
|
|
|
|
|
|
|
dir = os.path.dirname(dest)
|
2001-06-18 00:43:49 +05:30
|
|
|
try:
|
|
|
|
if not os.path.exists(dir):
|
|
|
|
os.mkdir(dir)
|
|
|
|
except IOError,msg:
|
|
|
|
GnomeErrorDialog(_("Could not create %s") % dir + "\n" + str(msg))
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
GnomeErrorDialog(_("Could not create %s") % dir)
|
|
|
|
return
|
2001-05-31 23:27:14 +05:30
|
|
|
|
|
|
|
if no_pil:
|
|
|
|
cmd = "%s -geometry %dx%d '%s' '%s'" % (const.convert,size,size,source,dest)
|
|
|
|
os.system(cmd)
|
|
|
|
else:
|
2001-06-01 06:31:45 +05:30
|
|
|
try:
|
2001-06-10 06:55:35 +05:30
|
|
|
im = PIL.Image.open(source)
|
2001-06-01 06:31:45 +05:30
|
|
|
im.thumbnail((size,size))
|
|
|
|
im.save(dest,"JPEG")
|
|
|
|
except:
|
|
|
|
pass
|
2001-05-31 23:27:14 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# scale_image
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def check_thumb(source,dest,size):
|
|
|
|
if not os.path.isfile(source):
|
|
|
|
return
|
|
|
|
if not os.path.isfile(dest):
|
|
|
|
mk_thumb(source,dest,size)
|
|
|
|
elif os.path.getmtime(source) > os.path.getmtime(dest):
|
|
|
|
mk_thumb(source,dest,size)
|
|
|
|
|
|
|
|
|
2001-05-31 08:10:08 +05:30
|
|
|
|