Source code for mg.utils.photo.resizer.base

# MG_GPL_HEADER_BEGIN
# 
# This file is part of Media Gallery, GNU GPLv2.
# 
# Media Gallery 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; version 2 of the License.
# 
# Media Gallery 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 Media Gallery; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
# 
# MG_GPL_HEADER_END

__author__ = "Media Gallery contributors <mg@lists.ssji.net>"
__copyright__ = "(c) 2010"
__credits__ = ["Dimitri Refauvelet", "Nicolas Pouillon"]

__all__ = ["ResizerBase"]

class ResizerBase:
[docs] """ ResizerBase is a tool to resize images in an automated way. Whatever the parameters, aspect ratio of image wont be modified, therefore image wont be deformed. """ LETTERBOX = 1 """ Make the output image contain the whole input image, adding black borders if necessary """ PAN_SCAN = 2 """ Fill the whole output image size with input image, fitting the shortest side of the image. Longest side of the input image may be partly discarded. """ SCALE = 3 """ Scale input image to fit in the output bounding box. Resulting image size may be smaller than given output size. """ def __init__(self, **params): """ Creates a new ResizerBase. This may initialize global states. :param params: Parameters passed to set_output_size verbatim. """ defaults = dict( size = (100, 100), mode = self.SCALE, ) defaults.update(params) self.set_output_size(**defaults) def set_output_size(self, size, mode):
[docs] """ Sets the output parameters :param size: Output size :param mode: Mode of resizing, in ``LETTERBOX``, ``PAN_SCAN`` or ``SCALE``. """ def resize(self,
[docs] output_filename, input_filename, center = None, rot_count = 0): """ Opens the given file as source image to resize, and save its resized version according to parameters. :param output_filename: Path to an image file to save resized version to :param input_filename: Path to an image file to open :param center: Optional coords of center of interest of image. If ommitted, image is centered normally. Only relevant for ``PAN_SCAN`` mode. See :ref:`fitting-modes` for more information. :param rot_count: Count of 90-degree rotations to apply to image, clockwise. This transformation changes nothing to the output image aspect. Rotation is achived before fitting bounding boxes. """