"""Create the single promised grade 3 variant.""" from io import BytesIO from PIL import Image, ImageOps def thumbnail(data): with Image.open(BytesIO(data), formats=("PNG", "JPEG")) as image: oriented = ImageOps.exif_transpose(image) width, height = oriented.size divisor = max(width, height, 256) size = (max(1, width * 256 // divisor), max(1, height * 256 // divisor)) if "A" in oriented.getbands() or "transparency" in oriented.info: rgba = oriented.convert("RGBA") white = Image.new("RGB", rgba.size, "white") white.paste(rgba, mask=rgba.getchannel("A")) rgb = white else: rgb = oriented.convert("RGB") if rgb.size != size: rgb = rgb.resize(size, Image.Resampling.LANCZOS) result = BytesIO() rgb.save(result, format="JPEG", quality=85) return result.getvalue(), size