quantize() reduces to far fewer colors than requested in some cases
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 42/100
- Issue-Typ
- Bug
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- python
- Bereich
- computer-vision
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- Python
- Sterne
- 14k
- Forks
- 2.6k
- Ø Merge
- 1 T. 1 Std.
- Gemergte PRs (30 T.)
- 205
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python-pillow/Pillow
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
python-pillow/Pillow#10590 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Image.thumbnail() raises ZeroDivisionError when the target height is 0Evtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
python-pillow/Pillow#10589 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Sun raster files with RLE encoding and odd-width rows decode shiftedEvtl. vergeben @PerryLink hat das vor 21 Tagen übernommen. Offen🤖-assisted
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
python-pillow/Pillow#10370 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Warn that pre-applying arabic_reshaper + python-bidi corrupts text on a Raqm buildEvtl. vergeben @Syamjith-NK hat das vor 85 Tagen übernommen. OffenDocumentation Font Rendering PNG Third Party Framework
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
python-pillow/Pillow#9907 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Example code in ImageText documentation makes incorrect use of get_bbox()Evtl. vergeben @mmporong hat das vor 1 Tag übernommen. OffenDocumentation
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 25/100
python-pillow/Pillow#10583 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in python-pillow/Pillow
Ähnliche Issues
-
CI: pin OS images in build/publish tests instead of `*-latest`Evtl. vergeben @shikhabhadoria hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
vacanza/holidays#4220 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 72/100
open-webui/open-webui#34817 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
can4hou6joeng4/boss-agent-cli#2552 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bench: agent-kanban-smoke accepts the session route's own live fleet read, not only a delegated resultEvtl. vergeben @bnaylor hat das heute übernommen. Offenneeds-triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
gke-labs/kube-agents#6860 ·
Maintainer antworten meist innerhalb von 1 Tag
-
documentation good first issue priority: low
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 85/100
fukubie/StockLlama#57 ·