Fix alpha premultiplication being used when generating mipmaps

hotfix/mipmap-alpha-fix
Andrew Cassidy 3 years ago
parent 24b064e6b4
commit f7d57aa859

@ -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

@ -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:

@ -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)

Loading…
Cancel
Save