quantize() reduces to far fewer colors than requested in some cases
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 42/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- python
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Python
- 星标
- 14k
- 派生
- 2.6k
- 平均合并
- 1 天 1 小时
- 30 天内合并 PR
- 205
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-pillow/Pillow 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
python-pillow/Pillow#10590 ·
维护者通常 1 天内回复
-
Image.thumbnail() raises ZeroDivisionError when the target height is 0可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 70/100
python-pillow/Pillow#10589 ·
维护者通常 1 天内回复
-
Sun raster files with RLE encoding and odd-width rows decode shifted可能已有人在做 @PerryLink 于 21 天前认领。 未关闭🤖-assisted
难度 2/5 1-3 小时 新手友好度 72/100
python-pillow/Pillow#10370 · 3 条评论 ·
维护者通常 1 天内回复
-
Warn that pre-applying arabic_reshaper + python-bidi corrupts text on a Raqm build可能已有人在做 @Syamjith-NK 于 85 天前认领。 未关闭Documentation Font Rendering PNG Third Party Framework
难度 2/5 1-3 小时 新手友好度 78/100
python-pillow/Pillow#9907 · 3 条评论 ·
维护者通常 1 天内回复
-
Example code in ImageText documentation makes incorrect use of get_bbox()可能已有人在做 @mmporong 于 1 天前认领。 未关闭Documentation
难度 1/5 1 小时以内 新手友好度 25/100
python-pillow/Pillow#10583 · 1 条评论 ·
维护者通常 1 天内回复
查看 python-pillow/Pillow 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
vllm-project/vllm#69304 · 1 条评论 ·
维护者通常 1 天内回复
-
interpreter-core type-bug
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
pydanty:is-working
难度 2/5 1-3 小时 新手友好度 68/100
pydantic/pydantic-ai#14187 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
test
难度 2/5 1-3 小时 新手友好度 62/100
prebid/salesagent#2676 ·
维护者通常 1 天内回复