Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

FITS: BZERO/BSCALE are ignored and 16-bit data is read as unsigned

未关闭
#10,309 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@karandhaodiyal28-hash 已经在做这个了。

开始于 2026年10月25日。

  • #10312 来自 @karandhaodiyal28-hash —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
backend, data

调研方向

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:

  1. Physical values. Apply BZERO/BSCALE: unsigned 16-bit (BZERO=32768) stays
    I;16, signed 16-bit and signed bytes become I, and non-integer scaling becomes F.
    This matches astropy, but the mode of some existing files would change from I;16 to I.
  2. Array values, correct signedness. Read BITPIX=16 as signed (I;16BS to I, as
    the TIFF plugin does), leave the scaling to the caller and expose BZERO/BSCALE in
    im.info.
  3. 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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-pillow/Pillow 的其他 Issue

查看 python-pillow/Pillow 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。