FITS: BZERO/BSCALE are ignored and 16-bit data is read as unsigned
维护者通常 1 天内回复
评估
调研方向
Start by locating the FITS plugin code that unpacks BITPIX=16 data and reads header cards; the issue does not name files or tests. Compare its handling of signed values and BZERO/BSCALE with the three behavior options in the issue, then check how the chosen behavior affects existing image modes. Done means the maintainers’ intended semantics are implemented and covered for signed 16-bit, BZERO/BSCALE scaling, and the unchanged 32-bit case.
由索引模型根据 Issue 内容生成。
描述
What did you do?
Opened small FITS images whose headers use BZERO/BSCALE, plus one with signed 16-bit
data, and compared the pixel values with astropy. The files are built in memory, so the
script has no data dependencies:
import io, struct
from PIL import Image
def fits(bitpix, values, extra=()):
cards = ["SIMPLE = T", "BITPIX = %20d" % bitpix,
"NAXIS = 2", "NAXIS1 = %20d" % len(values),
"NAXIS2 = 1", *extra, "END"]
header = b"".join(c.ljust(80).encode() for c in cards)
header += b" " * (-len(header) % 2880)
data = struct.pack(">%d%s" % (len(values), {8: "B", 16: "h", 32: "i"}[bitpix]), *values)
return header + data + b"\0" * (-len(data) % 2880)
unsigned16 = fits(16, [-32768, -32767, -1, 0, 32767], ["BZERO = 32768"])
signed16 = fits(16, [-32768, -1, 0, 1, 32767])
for blob in (unsigned16, signed16):
with Image.open(io.BytesIO(blob)) as im:
print(im.mode, [im.getpixel((x, 0)) for x in range(im.width)])
What did you expect to happen?
FITS integers are signed big-endian, and the values an application should see are
BZERO + BSCALE * array value (FITS Standard 4.0, Sect. 4.4.2.5, Eq. 3). Unsigned 16-bit
data, which most cameras produce, is stored as signed integers with BZERO = 32768
(Sect. 5.2.5, Table 11). astropy 8.0.2 (astropy.io.fits.getdata) gives:
| file | astropy |
|---|---|
| BITPIX=16, BZERO=32768 | [0, 1, 32767, 32768, 65535] |
| BITPIX=16, no BZERO | [-32768, -1, 0, 1, 32767] |
| BITPIX=8, BZERO=-128 | [-128, -127, -1, 0, 127] |
| BITPIX=16, BSCALE=0.5, BZERO=100 | [99.0, 100.0, 101.0, 150.0, 600.0] |
| BITPIX=32, no BZERO | [-2147483648, -1, 0, 1, 2147483647] |
What actually happened?
| file | Pillow 13.0.0 (also main 923a595) |
|---|---|
| BITPIX=16, BZERO=32768 | I;16 [32768, 32769, 65535, 0, 32767] |
| BITPIX=16, no BZERO | I;16 [32768, 65535, 0, 1, 32767] |
| BITPIX=8, BZERO=-128 | L [0, 1, 127, 128, 255] |
| BITPIX=16, BSCALE=0.5, BZERO=100 | I;16 [65534, 0, 2, 100, 1000] |
| BITPIX=32, no BZERO | I [-2147483648, -1, 0, 1, 2147483647] (correct) |
16-bit data is unpacked with I;16B, i.e. as unsigned, and BZERO/BSCALE are never
read. For the common unsigned convention every pixel comes out with its top bit flipped,
so a 16-bit camera frame looks like a solarised image. Signed 16-bit data without BZERO
wraps negative values to large positive ones. 32-bit data is already read as signed
(I;32B).
Question for maintainers
Before writing a PR I'd like to know which behaviour you want:
- Physical values. Apply
BZERO/BSCALE: unsigned 16-bit (BZERO=32768) stays
I;16, signed 16-bit and signed bytes becomeI, and non-integer scaling becomesF.
This matches astropy, but the mode of some existing files would change fromI;16toI. - Array values, correct signedness. Read BITPIX=16 as signed (
I;16BStoI, as
the TIFF plugin does), leave the scaling to the caller and exposeBZERO/BSCALEin
im.info. - Keep the raw reading as it is, and document that FITS integers are returned as
unsigned array values without scaling.
For option 1 with BZERO=32768, I don't see an existing rawmode that flips the sign bit
into I;16. It would need a small unpacker or a post-processing step in the plugin, so
tell me if you'd rather avoid that.
What are your OS, Python and Pillow versions?
- OS: Ubuntu 24.04 (x86_64, Docker)
- Python: 3.12.3
- Pillow: 13.0.0 from PyPI, and main at 923a595 built from source
- 主要语言
- Python
- 星标
- 13.9k
- 派生
- 2.6k
- 平均合并
- 1 天 6 小时
- 30 天内合并 PR
- 159
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-pillow/Pillow 的其他 Issue
-
Warn that pre-applying arabic_reshaper + python-bidi corrupts text on a Raqm build可能已有人在做 @Syamjith-NK 于 58 天前认领。 未关闭Documentation Font Rendering PNG Third Party Framework
难度 2/5 1-3 小时 新手友好度 78/100
python-pillow/Pillow#9907 · 2 条评论 ·
维护者通常 1 天内回复
-
Image.putpixel incorrectly labels supported arguments in type annotations可能已有人在做 @radarhere 于 186 天前认领。 未关闭PNG
难度 2/5 1-3 小时 新手友好度 70/100
python-pillow/Pillow#9584 · 1 条评论 ·
维护者通常 1 天内回复
-
cp316 drops required macro for Pyllow to compile可能已有人在做 @lazerg 今天认领。 未关闭
难度 4/5 3-5 天 新手友好度 48/100
python-pillow/Pillow#10323 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 54/100
python-pillow/Pillow#10317 · 1 条评论 ·
维护者通常 1 天内回复
-
Wheels for Android?可能已有人在做 @mhsmith 于 9 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 48/100
python-pillow/Pillow#10311 · 1 条评论 ·
维护者通常 1 天内回复
查看 python-pillow/Pillow 的全部 Issue
相似的 Issue
-
flaky-watch
难度 2/5 1-3 小时 新手友好度 88/100
btclib-org/btclib#2839 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
nion-software/nionswift#2018 ·
维护者通常 1 天内回复
-
Deleting a member fails when another member's share transfer points to them可能已有人在做 @Beerlesklopfer 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
FoodCoopX/wirgarten-tapir#1336 ·
维护者通常 3 天内回复
-
pydanty:is-working
难度 2/5 1-3 小时 新手友好度 71/100
pydantic/pydantic-ai#11467 ·
维护者通常 1 天内回复
-
automated issue linkchecker report
难度 2/5 1-3 小时 新手友好度 66/100
QuantEcon/lecture-python.myst#1098 ·
维护者通常 1 天内回复