From f7d57aa859f9eac8b1b6b47c4a5411d149982b47 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Tue, 28 Sep 2021 20:40:01 -0700 Subject: [PATCH] Fix alpha premultiplication being used when generating mipmaps --- CHANGELOG.md | 7 +++++++ quicktex/dds.py | 2 +- quicktex/image_utils.py | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eedc76..1dbf2e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file +## Unreleased + +### Fixed + +- Fixed alpha premultiplication when generating mipmaps + + ## 0.1.0 - 2021-05-10 ### Added diff --git a/quicktex/dds.py b/quicktex/dds.py index 223a09a..a6ae106 100644 --- a/quicktex/dds.py +++ b/quicktex/dds.py @@ -256,7 +256,7 @@ def encode(image: Image.Image, encoder, four_cc: str, mip_count: typing.Optional image = image.convert(mode) sizes = quicktex.image_utils.mip_sizes(image.size, mip_count) - images = [image] + [image.resize(size, Image.BILINEAR) for size in sizes[1:]] + images = [image] + [quicktex.image_utils.resize_no_premultiply(image, size) for size in sizes[1:]] dds = DDSFile() for i in images: diff --git a/quicktex/image_utils.py b/quicktex/image_utils.py index 28d120a..e9b6d4b 100644 --- a/quicktex/image_utils.py +++ b/quicktex/image_utils.py @@ -32,3 +32,20 @@ def mip_sizes(dimensions: Tuple[int, int], mip_count: Optional[int] = None) -> L dimensions = tuple([max(dim // 2, 1) for dim in dimensions]) return chain + + +def resize_no_premultiply(image: Image.Image, size: Tuple[int, int]) -> Image.Image: + """ + Resize an image without premulitplying the alpha. Required due to a quick in Pillow + + :param image: Image to resize + :param size: Size to resize to + :return: The resized image + """ + if image.mode == 'RGBA': + rgb = image.convert('RGB').resize(size, Image.BILINEAR) + a = image.getchannel('A').resize(size, Image.BILINEAR) + rgb.putalpha(a) + return rgb + else: + return image.resize(size, Image.BILINEAR)