gpt4 book ai didi

python - 将 pdf 转换为图像时出现类型错误 : TypeError ("object of type ' NoneType' has no len()", )

转载 作者:行者123 更新时间:2023-12-01 02:14:04 25 4
gpt4 key购买 nike

我正在构建 API,在 MacOS 环境下基于 Django REST 框架将上传的 pdf 转换为图像。当然,在系统上安装好imagemagick和ghostscript。

这是我的代码片段。

from wand.image import Image 
try:
with Image(filename=pdffile, resolution=300) as img:
for i, page in enumerate(img.sequence):
filename = '{}-{}.jpg'.format(imagefilename, i + 1)
file ='{}/{}'.format(tmp_dir, filename)
Image(page).save(filename=file)
filelist.append(filename)
except WandException as e:
print e
except TypeError as e:
print e

但我收到此错误:

Exception TypeError: TypeError("object of type 'NoneType' has no len()",) in bound method Image.del of wand.image.Image: (empty) ignored

在线:

with Image(filename=pdffile, resolution=300) as img:

这里是引用引用:

内部服务器错误:/api/v1/admin/upload/pdf

Traceback (most recent call last): File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response response = self.process_exception_by_middleware(e, request)

File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs)

File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv/lib/python2.7/site-packages/rest_framework/views.py", line 466, in dispatch response = self.handle_exception(exc) File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv/lib/python2.7/site-packages/rest_framework/views.py", line 463, in dispatch response = handler(request, *args, **kwargs)

File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/data_api/announcement/views.py", line 582, in post filelist = self._slice_n_convert_pdf_to_image(tmp_dir, file_obj.name)

File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/data_api/announcement/views.py", line 557, in _slice_n_convert_pdf_to_image with Image(filename=pdffile, resolution=300) as img:

File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv/lib/python2.7/site-packages/wand/image.py", line 2744, in init self.read(filename=filename, resolution=resolution)

File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv/lib/python2.7/site-packages/wand/image.py", line 2822, in read self.raise_exception()

File "/Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv/lib/python2.7/site-packages/wand/resource.py", line 222, in raise_exception raise e DelegateError: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r300x300' '-sOutputFile=/var/folders/kl/5qf5q4x50xj2rbdp8t2prsbh0000gn/T/magick-645443wbRHI2nYLWV%d' '-f/var/folders/kl/5qf5q4x50xj2rbdp8t2prsbh0000gn/T/magick-64544qCe-gNjUlDXO' '-f/var/folders/kl/5qf5q4x50xj2rbdp8t2prsbh0000gn/T/magick-64544VNalr_sUZm2w'' (1) @ error/pdf.c/InvokePDFDelegate/292

我用谷歌搜索了这个问题,但找不到正确的解决方案。大多数人是这样说的:

  • 重新安装 ImageMagick/Ghostscript。
  • 检查系统环境路径变量。

这些解决方案对我不起作用。您曾经遇到过这样的问题吗?还有其他解决方案吗?

最佳答案

异常...

Exception TypeError: TypeError("object of type 'NoneType' has no len()",) in bound method Image.del of wand.image.Image: (empty) ignored

... 是由 wand 认为光栅图像已加载而引起的 - 即使它没有加载。此 TypeError 已在未来版本中得到解决,但这只是问题的症状。真正的解决方案是找出原因 无法读取/呈现 PDF。

Have you ever faced issues like this?

通常 PDF 是问题所在。也许是格式错误的数据流,或者是 Ghostscript/libtiff 无法理解的令人困惑的元标记。无论出于何种原因,gs 都会以非零错误状态退出,并且 ImageMagick 将作为委托(delegate)错误拒绝请求。

Have any other solutions?

  1. 验证 ImageMagick 是否可以按预期渲染文件。

    convert -density 300x300 source.pdf output%d.png
    • 观察写入 stdout 和 stderr 的任何警告/错误消息。
    • 使用-debug All查看所有操作的详细日志。
  2. 验证 Ghostscript 是否可以按预期呈现文件。

     gs -sstdout=%stderr -dQUIT -dSAFER -dBATCH \
    -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
    -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=pngalpha \
    -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300x300 \
    -sOutputFile=output%d.png \
    -fsource.pdf
    • 观察上述任何消息,但立即调用 $?在使用 gs 命令后,通过返回 0 状态来验证 Ghostscript 是否已成功完成。

关于python - 将 pdf 转换为图像时出现类型错误 : TypeError ("object of type ' NoneType' has no len()", ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48518891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com