You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python转换PDF为Group4压缩TIFF出现Fax3SetupState错误如何解决

问题原因

Fax3SetupState: Bits/sample must be 1 for Group 3/4 encoding/decoding.

这个报错的核心是TIFF的Group4压缩协议仅支持1位深度的二值黑白图像,你之前的操作没有把图像成功转换为1位深度就直接调用保存逻辑,所以报错。你之前写的image_file.convert('1')没有生效,是因为没有把转换后的PIL图像对象实际传递到保存步骤里,且PyMuPDF直接输出的pixmap默认是8位RGB/灰度格式,不满足压缩要求。

修复代码
import fitz
from PIL import Image

mat = fitz.Matrix(300 / 72, 300 / 72)  # 300DPI缩放系数
doc = fitz.open("/home/Image Conversion/test-pdf")

for page in doc:
    print(page.number)
    # 提取页面为灰度pixmap,减少后续转换开销
    pix = page.get_pixmap(matrix=mat, colorspace=fitz.csGRAY, alpha=False)
    # 将pixmap转为PIL Image对象
    pil_img = Image.frombytes("L", [pix.width, pix.height], pix.samples)
    # 转换为1位二值黑白图像,不需要抖动可加参数dither=Image.Dither.NONE
    bin_img = pil_img.convert("1")
    # 保存为Group4压缩的TIFF
    img_filename = f"/home/Image Conversion/page_b_w-%04i.tiff" % page.number
    bin_img.save(img_filename, format="TIFF", dpi=(300,300), compression="group4")

内容的提问来源于stack exchange,提问作者adan11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 00:48:03