# -*- coding: utf-8 -*-
"""Module of commonly use python objects
**Disclaimer**
Copyright (C) 2018-2019 Philipp S. Sommer
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 3 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, see <https://www.gnu.org/licenses/>.
"""
from docrep import DocstringProcessor
docstrings = DocstringProcessor()
[docs]def rgba2rgb(image, color=(255, 255, 255)):
"""Alpha composite an RGBA Image with a specified color.
Source: http://stackoverflow.com/a/9459208/284318
Parameters
----------
image: PIL.Image
The PIL RGBA Image object
color: tuple
The rgb color for the background
Returns
-------
PIL.Image
The rgb image
"""
from PIL import Image
image.load() # needed for split()
background = Image.new('RGB', image.size, color)
background.paste(image, mask=image.split()[3]) # 3 is the alpha channel
return background