2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-03-03 05:07:16 +05:30
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
2011-05-08 08:59:36 +05:30
|
|
|
# Copyright (C) 2011 Adam Stein <adam@csh.rit.edu>
|
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.
|
|
|
|
#
|
2007-09-11 03:44:33 +05:30
|
|
|
# This program is distributed in the hope that it will be useful,
|
2002-10-20 19:55:16 +05:30
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2011-10-23 08:43:50 +05:30
|
|
|
# $Id$
|
2006-03-22 00:41:32 +05:30
|
|
|
|
2007-09-11 03:44:33 +05:30
|
|
|
"""
|
|
|
|
Image manipulation routines.
|
|
|
|
"""
|
|
|
|
|
2006-03-22 00:41:32 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2002-10-20 19:55:16 +05:30
|
|
|
import os
|
2006-03-29 08:51:29 +05:30
|
|
|
import tempfile
|
2006-03-22 00:41:32 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK/Gnome modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2010-10-09 18:03:47 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import Utils
|
|
|
|
|
2006-03-22 00:41:32 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2007-09-11 09:21:35 +05:30
|
|
|
# resize_to_jpeg
|
2006-03-22 00:41:32 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2011-07-12 09:00:58 +05:30
|
|
|
def resize_to_jpeg(source, destination, width, height, crop=None):
|
2007-09-11 09:21:35 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Create the destination, derived from the source, resizing it to the
|
2007-09-11 09:21:35 +05:30
|
|
|
specified size, while converting to JPEG.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
: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
|
2011-07-12 09:00:58 +05:30
|
|
|
:param crop: cropping coordinates
|
|
|
|
:type crop: array of integers ([start_x, start_y, end_x, end_y])
|
2007-09-11 09:21:35 +05:30
|
|
|
"""
|
2009-11-17 04:09:54 +05:30
|
|
|
import gtk
|
2011-07-12 09:00:58 +05:30
|
|
|
|
2007-09-11 09:21:35 +05:30
|
|
|
img = gtk.gdk.pixbuf_new_from_file(source)
|
2011-07-12 09:00:58 +05:30
|
|
|
|
|
|
|
if crop:
|
|
|
|
# Gramps cropping coorinates are [0, 100], so we need to convert to pixels
|
|
|
|
start_x = int((crop[0]/100.0)*img.get_width())
|
|
|
|
start_y = int((crop[1]/100.0)*img.get_height())
|
|
|
|
end_x = int((crop[2]/100.0)*img.get_width())
|
|
|
|
end_y = int((crop[3]/100.0)*img.get_height())
|
|
|
|
|
|
|
|
img = img.subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y)
|
|
|
|
|
|
|
|
# Need to keep the ratio intact, otherwise scaled images look stretched
|
|
|
|
# if the dimensions aren't close in size
|
|
|
|
(width, height) = image_actual_size(width, height, img.get_width(), img.get_height())
|
|
|
|
|
|
|
|
scaled = img.scale_simple(int(width), int(height), gtk.gdk.INTERP_BILINEAR)
|
2007-09-11 09:21:35 +05:30
|
|
|
scaled.save(destination, 'jpeg')
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2011-05-08 08:59:36 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# image_dpi
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def image_dpi(source):
|
|
|
|
"""
|
|
|
|
Return the dpi found in the image header. None is returned if no dpi attribute
|
|
|
|
is available.
|
|
|
|
|
|
|
|
:param source: source image file, in any format that PIL recognizes
|
|
|
|
:type source: unicode
|
|
|
|
:rtype: int
|
|
|
|
:returns: the DPI setting in the image header
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
import PIL.Image
|
|
|
|
except ImportError:
|
|
|
|
import sys
|
|
|
|
print >> sys.stderr, _("WARNING: PIL module not loaded. "
|
|
|
|
"Image cropping in report files will not be available.")
|
|
|
|
|
|
|
|
dpi = None
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
img = PIL.Image.open(source)
|
|
|
|
except IOError:
|
|
|
|
dpi = None
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
dpi = img.info["dpi"]
|
2012-02-17 19:50:01 +05:30
|
|
|
except (AttributeError, KeyError):
|
2011-05-08 08:59:36 +05:30
|
|
|
dpi = None
|
|
|
|
|
|
|
|
return dpi
|
|
|
|
|
2006-03-22 00:41:32 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2007-09-11 09:21:35 +05:30
|
|
|
# image_size
|
2006-03-22 00:41:32 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2007-09-11 09:21:35 +05:30
|
|
|
def image_size(source):
|
2007-09-11 03:44:33 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return the width and size of the specified image.
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
: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
|
2007-09-11 09:21:35 +05:30
|
|
|
"""
|
2009-11-17 04:09:54 +05:30
|
|
|
import gtk
|
|
|
|
import gobject
|
2005-02-24 05:55:34 +05:30
|
|
|
try:
|
2007-09-11 09:21:35 +05:30
|
|
|
img = gtk.gdk.pixbuf_new_from_file(source)
|
2007-09-11 09:43:20 +05:30
|
|
|
width = img.get_width()
|
|
|
|
height = img.get_height()
|
2007-09-11 09:21:35 +05:30
|
|
|
except gobject.GError:
|
|
|
|
width = 0
|
|
|
|
height = 0
|
|
|
|
return (width, height)
|
2005-08-18 11:28:28 +05:30
|
|
|
|
2011-05-08 08:59:36 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2011-05-13 09:28:41 +05:30
|
|
|
# image_actual_size
|
2011-05-08 08:59:36 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def image_actual_size(x_cm, y_cm, x, y):
|
|
|
|
"""
|
|
|
|
Calculate what the actual width & height of the image should be.
|
|
|
|
|
|
|
|
:param x_cm: width in centimeters
|
|
|
|
:type source: int
|
|
|
|
:param y_cm: height in centimeters
|
|
|
|
:type source: int
|
|
|
|
:param x: desired width in pixels
|
|
|
|
:type source: int
|
|
|
|
:param y: desired height in pixels
|
|
|
|
:type source: int
|
|
|
|
:rtype: tuple(int, int)
|
|
|
|
:returns: a tuple consisting of the width and height in centimeters
|
|
|
|
"""
|
|
|
|
|
|
|
|
ratio = float(x_cm)*float(y)/(float(y_cm)*float(x))
|
|
|
|
|
|
|
|
if ratio < 1:
|
|
|
|
act_width = x_cm
|
|
|
|
act_height = y_cm*ratio
|
|
|
|
else:
|
|
|
|
act_height = y_cm
|
|
|
|
act_width = x_cm/ratio
|
|
|
|
|
|
|
|
return (act_width, act_height)
|
|
|
|
|
2011-09-02 07:02:03 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# resize_to_buffer
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def resize_to_buffer(source, size, crop=None):
|
|
|
|
"""
|
|
|
|
Loads the image and resizes 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 size: desired size of the destination image ([width, height])
|
|
|
|
:type size: list
|
|
|
|
:param crop: cropping coordinates
|
|
|
|
:type crop: array of integers ([start_x, start_y, end_x, end_y])
|
|
|
|
:rtype: buffer of data
|
|
|
|
:returns: raw data
|
|
|
|
"""
|
|
|
|
import gtk
|
|
|
|
img = gtk.gdk.pixbuf_new_from_file(source)
|
|
|
|
|
|
|
|
if crop:
|
|
|
|
# Gramps cropping coorinates are [0, 100], so we need to convert to pixels
|
|
|
|
start_x = int((crop[0]/100.0)*img.get_width())
|
|
|
|
start_y = int((crop[1]/100.0)*img.get_height())
|
|
|
|
end_x = int((crop[2]/100.0)*img.get_width())
|
|
|
|
end_y = int((crop[3]/100.0)*img.get_height())
|
|
|
|
|
|
|
|
img = img.subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y)
|
|
|
|
|
|
|
|
# Need to keep the ratio intact, otherwise scaled images look stretched
|
|
|
|
# if the dimensions aren't close in size
|
|
|
|
(size[0], size[1]) = image_actual_size(size[0], size[1], img.get_width(), img.get_height())
|
|
|
|
|
|
|
|
scaled = img.scale_simple(int(size[0]), int(size[1]), gtk.gdk.INTERP_BILINEAR)
|
|
|
|
|
|
|
|
return scaled
|
|
|
|
|
2007-09-11 09:21:35 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# resize_to_jpeg_buffer
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2011-07-27 08:56:12 +05:30
|
|
|
def resize_to_jpeg_buffer(source, size, crop=None):
|
2007-09-11 09:21:35 +05:30
|
|
|
"""
|
|
|
|
Loads the image, converting the file to JPEG, and resizing it. Instead of
|
|
|
|
saving the file, the data is returned in a buffer.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param source: source image file, in any format that gtk recognizes
|
|
|
|
:type source: unicode
|
2011-07-27 08:56:12 +05:30
|
|
|
:param size: desired size of the destination image ([width, height])
|
|
|
|
:type size: list
|
|
|
|
:param crop: cropping coordinates
|
|
|
|
:type crop: array of integers ([start_x, start_y, end_x, end_y])
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: buffer of data
|
|
|
|
:returns: jpeg image as raw data
|
2007-09-11 09:21:35 +05:30
|
|
|
"""
|
2009-11-17 04:09:54 +05:30
|
|
|
import gtk
|
2007-09-11 09:43:20 +05:30
|
|
|
filed, dest = tempfile.mkstemp()
|
2007-09-11 09:21:35 +05:30
|
|
|
img = gtk.gdk.pixbuf_new_from_file(source)
|
2011-07-27 08:56:12 +05:30
|
|
|
|
|
|
|
if crop:
|
|
|
|
# Gramps cropping coorinates are [0, 100], so we need to convert to pixels
|
|
|
|
start_x = int((crop[0]/100.0)*img.get_width())
|
|
|
|
start_y = int((crop[1]/100.0)*img.get_height())
|
|
|
|
end_x = int((crop[2]/100.0)*img.get_width())
|
|
|
|
end_y = int((crop[3]/100.0)*img.get_height())
|
|
|
|
|
|
|
|
img = img.subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y)
|
|
|
|
|
|
|
|
# Need to keep the ratio intact, otherwise scaled images look stretched
|
|
|
|
# if the dimensions aren't close in size
|
|
|
|
(size[0], size[1]) = image_actual_size(size[0], size[1], img.get_width(), img.get_height())
|
|
|
|
|
|
|
|
scaled = img.scale_simple(int(size[0]), int(size[1]), gtk.gdk.INTERP_BILINEAR)
|
2010-04-16 21:06:31 +05:30
|
|
|
os.close(filed)
|
2010-10-09 18:03:47 +05:30
|
|
|
dest = Utils.get_unicode_path_from_env_var(dest)
|
2007-09-11 09:21:35 +05:30
|
|
|
scaled.save(dest, 'jpeg')
|
2007-09-11 09:43:20 +05:30
|
|
|
ofile = open(dest, mode='rb')
|
|
|
|
data = ofile.read()
|
|
|
|
ofile.close()
|
2007-09-11 09:21:35 +05:30
|
|
|
try:
|
|
|
|
os.unlink(dest)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return data
|
2006-02-02 10:50:42 +05:30
|
|
|
|