From 8120bcd4559694f70629f30443df732e75b444b4 Mon Sep 17 00:00:00 2001 From: Aaron Griffith Date: Sat, 23 Oct 2010 13:09:19 -0400 Subject: [PATCH] added a wrapper for alpha_over that falls back on PIL paste() if needed --- composite.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 composite.py diff --git a/composite.py b/composite.py new file mode 100644 index 0000000..45719a9 --- /dev/null +++ b/composite.py @@ -0,0 +1,51 @@ +# This file is part of the Minecraft Overviewer. +# +# Minecraft Overviewer 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 3 of the License, or (at +# your option) any later version. +# +# Minecraft Overviewer 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 the Overviewer. If not, see . + +import logging + +from PIL import Image + +""" +This module has an alpha-over function that is used throughout +Overviewer. It defaults to the PIL paste function when the custom +alpha-over extension cannot be found. +""" + +extension_alpha_over = None +try: + from _composite import alpha_over as _extension_alpha_over + extension_alpha_over = _extension_alpha_over +except ImportError: + logging.warning("alpha_over extension not found; using default PIL paste()") + +def alpha_over(dest, src, pos_or_rect=(0, 0), mask=None): + """Composite src over dest, using mask as the alpha channel (if + given), otherwise using src's alpha channel. pos_or_rect can + either be a position or a rectangle, specifying where on dest to + put src. Falls back to dest.paste() if the alpha_over extension + can't be found.""" + if mask == None: + mask = src + + global extension_alpha_over + if extension_alpha_over != None: + # extension ALWAYS expects rects, so convert if needed + if len(pos_or_rect) == 2: + pos_or_rect = (pos_or_rect[0], pos_or_rect[1], src.size[0], src.size[1]) + extension_alpha_over(dest, src, pos_or_rect, mask) + else: + # fallback + dest.paste(src, pos_or_rect, mask) +