【问题标题】:Converting .TIF to .PDF gives PIL: Error reading image将 .TIF 转换为 .PDF 会产生 PIL:读取图像时出错
【发布时间】:2019-09-12 13:04:40
【问题描述】:

我一直在尝试批量处理一些 .TIF 文件并将它们转换为 PDF。我确实让它工作了,但是在尝试更改 img2pdf 以便它可以接受更大的文件之后,我再也无法让相同的程序再次运行,即使在重新安装之后也是如此。

目前这是抛出以下错误:

>>>>
ImageOpenError: cannot read input image (not jpeg2000). PIL: error reading image: cannot identify image file <_io.BytesIO object at 0x000001A608255EB8>

这是我一直在使用的代码。有人有什么建议吗?提前致谢。


import img2pdf, sys, os, time
image_directory = r"PATH"

image_files = []

for root, dirs, files in os.walk(image_directory):
    for file in files:
        if file.endswith(".tif") or file.endswith(".TIF"):
             print("Discovered this TIF: ", os.path.join(root, file))
             image_files.append(os.path.join(root, file))

for image in image_files:
    output_file = image[:-4] + ".pdf"
    print ("Putting all TIFs into ", output_file)
    pdf_bytes = img2pdf.convert(image)
    file = open(output_file,"wb")
    file.write(pdf_bytes)

这是完整的回溯

Traceback (most recent call last):

  File "<ipython-input-37-fe96d5eeb049>", line 1, in <module>
    runfile('PATH', wdir='PATH')

  File "PATH", line 704, in runfile
    execfile(filename, namespace)

  File "PATH", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "PATH", line 23, in <module>
    pdf_bytes = img2pdf.convert(image_files)

  File "PATH", line 1829, in convert
    ) in read_images(rawdata, kwargs["colorspace"], kwargs["first_frame_only"]):

  File "PATH", line 1171, in read_images
    "PIL: error reading image: %s" % e

ImageOpenError: cannot read input image (not jpeg2000). PIL: error reading image: cannot identify image file <_io.BytesIO object at 0x000001A6082BE3B8>

【问题讨论】:

  • 似乎某些图像“已损坏”。你能分享你要转换的 TIF 文件吗? P.S.:我已经在一些示例 TIF 文件上尝试了您的代码,它似乎运行良好。
  • 我无法共享文件,不,但我已尝试打开该文件并且它完全可以查看。
  • fileformat.info/format/tiff/sample列表顶部的图片上试过了,还是不行
  • 您使用的是哪个版本的 Pillow?
  • PIL.__version__: Out[46]: '5.3.0'

标签: python-3.x type-conversion python-imaging-library


【解决方案1】:

据我所知,如果您想递归查找所有 TIFF 图像并将每个图像转换为相应命名的 PDF 文件,您可以简单地使用 GNU Parallel 并行执行此操作>ImageMagick 在终端中像这样:

find . -iname "*tif" -print0 | parallel -0 --dry-run mogrify {} {.}.pdf

样本输出

mogrify ./OpenCVTIFF64/result.tif ./OpenCVTIFF64/result.pdf
mogrify ./OpenCVTIFF64/a.tif ./OpenCVTIFF64/a.pdf
mogrify ./OpenCVBasics/a.tif ./OpenCVBasics/a.pdf
mogrify ./CImgDump/image.tif ./CImgDump/image.pdf

该命令说... “从当前目录开始,递归查找所有 TIFF 文件,无论是大写还是小写或混合,并将它们的名称(以空字符结尾)传递给 GNU Parallel。然后它应该读取每个名称并运行 ImageMagick mogrify 以将该 TIFF 转换为具有相同名称但扩展名替换为 PDF 的文件。"

如果它执行您想要的操作,请删除 --dry-run 并再次执行此操作。

【讨论】:

  • 我设法让 python 脚本工作,但我无法处理像素数高的大图片。这种方法能避免吗?
  • 我不希望它有任何问题,试试吧。如果在 macOS 上,请使用 brew install parallel imagemagick
  • 设法避免在 python 上使用 Image.MAX_IMAGE_PIXELS = None 的解压缩警告。不过感谢您的帮助
【解决方案2】:

因此,一旦我执行 pip install 'Pillow>=6.0.0' --force-reinstall,即使命令本身没有正确执行,这最终也会起作用。我在运行时收到了一些警告,但它现在可以正常工作了。简短的版本是,这是枕头的问题。

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2021-12-12
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    相关资源
    最近更新 更多