[Python] How to prevent color loss for transparent colors during Image.resize() when using...

Discussão em 'Python' iniciado por Stack, Agosto 10, 2025.

  1. Stack

    Stack Membro Participativo

    Google is being absolutely no help here. I've identified an issue (bug, feature?) with Pillow wherein certain low-transparency color values are lost during resizing when using the BOX resample method.

    Left is the original sprite, and right is the 5x resampled version. You'll note the depreciation of the "lightest" transparent values.

    Pre-scaled Image Scaled Image

    I'm looking for suggestions on how to mitigate this, pre-multiplying alpha values was still lossy, and also did not work with my method for loading sprites before pasting layers.

    Relevant image loading code below.

    def recolor(self, colorOverride):
    for i, color in enumerate(colorOverride):
    self.data[..., :-1][self.masks.T] = color

    return Image.fromarray(self.data)

    generated_item = Sprites.CraftingAsset(Image.open(f'navi-me/assets/{sprite_meta[item]["type"]}/{item}.png').convert('RGBA'), sprite_meta[item], inter)

    current = generated_item.recolor([self.to_rgb(hex) for hex in generated_item.mask])
    item_resized = current.resize((current.size[0] * 5, current.size[1] * 5), resample=Image.BOX)
    item_resized.save(f"navi-me/assets/temp/{inter.author.id}_custom_item", "PNG")

    Continue reading...

Compartilhe esta Página