【发布时间】: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