quantize() reduces to far fewer colors than requested in some cases
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 42/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- À l'abandon
- Stack technique
- python
- Domaine
- computer-vision
Piste de recherche
Start by reproducing the reported result with test-in.png and the Python quantize() call described in the issue. Trace the quantization entry point and its handling of RGB values with varying alpha, then add a regression test for the 508-color image. Done means quantizing this case no longer unexpectedly produces only 25 colors and the existing test suite still passes.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
What did you do?
This image, named test-in.png, has 508 distinct colors according to getcolors().

I tried to quantize it to 256 colors with the following code.
from PIL import Image
MAXCOLORCOUNT = 1024
with Image.open("test-in.png") as im:
colsbefore = im.getcolors(maxcolors=MAXCOLORCOUNT)
colsbefore = len(colsbefore) if colsbefore is not None else f"{MAXCOLORCOUNT}<"
im = im.quantize(colors=256)
colsafter = len(im.getcolors(maxcolors=MAXCOLORCOUNT))
print(f"colors {colsbefore} -> {colsafter}")
im.save("test-out.png")
What did you expect to happen?
Reduced to 256 colors, i.e. print colors 508 -> 256
What actually happened?
Reduced to only 25 colors, i.e. prints colors 508 -> 25
What are your OS, Python and Pillow versions?
- OS: Ubuntu 20.04 (Kubuntu)
- Python: 3.9.1 (via pyenv)
- Pillow: 8.1.0
Other remarks
I'm guessing it's something about the image having only a few distinct RGB values but with a wide range of alpha values. A test image with over 256 distinct RGB values had no such issue.
Why don't I use pngquant or imagemagick instead? I depend on Pillow because I need a quantization method that preserves the colors of fully transparent pixels. (I'm modding a game whose texture filtering causes the colors of fully-transparent pixels to be faintly but noticeably visible around the edges of the fully-transparent regions.)
Edit: Using im.convert("P") on an image that already has 256 or fewer colors (according to len(im.getcolors())) can cause it to lose more colors unnecessarily during the conversion.
- Langage dominant
- Python
- Étoiles
- 14k
- Forks
- 2.6k
- Merge moyen
- 1 j 1 h
- PR mergées (30 j)
- 205
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de python-pillow/Pillow
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
python-pillow/Pillow#10590 ·
Les mainteneurs répondent en général sous 1 jour
-
Image.thumbnail() raises ZeroDivisionError when the target height is 0Peut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
python-pillow/Pillow#10589 ·
Les mainteneurs répondent en général sous 1 jour
-
Sun raster files with RLE encoding and odd-width rows decode shiftedPeut-être pris @PerryLink l’a pris il y a 21 jours. Ouverte🤖-assisted
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
python-pillow/Pillow#10370 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Warn that pre-applying arabic_reshaper + python-bidi corrupts text on a Raqm buildPeut-être pris @Syamjith-NK l’a pris il y a 85 jours. OuverteDocumentation Font Rendering PNG Third Party Framework
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
python-pillow/Pillow#9907 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Example code in ImageText documentation makes incorrect use of get_bbox()Peut-être pris @mmporong l’a pris il y a 1 jour. OuverteDocumentation
Difficulté 1/5 Moins d'une heure Accessibilité débutants 25/100
python-pillow/Pillow#10583 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de python-pillow/Pillow
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
vllm-project/vllm#69304 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
interpreter-core type-bug
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
Les mainteneurs répondent en général sous 1 jour
-
pydanty:is-working
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
pydantic/pydantic-ai#14187 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
Les mainteneurs répondent en général sous 1 jour
-
test
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
prebid/salesagent#2676 ·
Les mainteneurs répondent en général sous 1 jour