gramps/src/ImgManip.py

121 lines
3.8 KiB
Python
Raw Normal View History

2002-10-20 19:55:16 +05:30
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Donald N. Allingham
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.
#
# 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
#
2006-03-22 00:41:32 +05:30
# $Id$
"""
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
#
#-------------------------------------------------------------------------
import gtk
import gobject
2002-10-20 19:55:16 +05:30
2006-03-22 00:41:32 +05:30
#-------------------------------------------------------------------------
#
# resize_to_jpeg
2006-03-22 00:41:32 +05:30
#
#-------------------------------------------------------------------------
def resize_to_jpeg(source, destination, width, height):
"""
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
"""
img = gtk.gdk.pixbuf_new_from_file(source)
scaled = img.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)
scaled.save(destination, 'jpeg')
2005-12-06 12:08:09 +05:30
2006-03-22 00:41:32 +05:30
#-------------------------------------------------------------------------
#
# image_size
2006-03-22 00:41:32 +05:30
#
#-------------------------------------------------------------------------
def image_size(source):
"""
Returns the width and size of the specified image.
2005-12-06 12:08:09 +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
"""
try:
img = gtk.gdk.pixbuf_new_from_file(source)
width = img.get_width()
height = img.get_height()
except gobject.GError:
width = 0
height = 0
return (width, height)
#-------------------------------------------------------------------------
#
# 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
"""
filed, 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')
ofile = open(dest, mode='rb')
data = ofile.read()
ofile.close()
try:
os.unlink(dest)
except:
pass
return data