【问题标题】:PDF to Image conversion using Wand python使用 Wand python 将 PDF 转换为图像
【发布时间】:2018-08-28 05:29:16
【问题描述】:

我正在尝试将 PDF 转换为 PNG

with Img(filename='read1.pdf', resolution=300) as img:
    img.compression_quality = 99
    img.save(filename='read1.jpg')

但我收到以下错误。

Traceback (most recent call last):
  File "C:/Users/l-gunalan/PycharmProjects/ocr/chech.py", line 4, in <module>
    with Img(filename='read1.pdf', resolution=300) as img:
  File "C:\Users\l-gunalan\AppData\Local\Continuum\anaconda3\lib\site-packages\wand-0.4.4-py3.6.egg\wand\image.py", line 2744, in __init__
  File "C:\Users\l-gunalan\AppData\Local\Continuum\anaconda3\lib\site-packages\wand-0.4.4-py3.6.egg\wand\image.py", line 2822, in read
  File "C:\Users\l-gunalan\AppData\Local\Continuum\anaconda3\lib\site-packages\wand-0.4.4-py3.6.egg\wand\resource.py", line 222, in raise_exception
wand.exceptions.DelegateError: PDFDelegateFailed `Unrecoverable error: rangecheck in .setuserparams
Operand stack:
    --nostringval--  --nostringval--  --nostringval--
START 0 2588256 1247365 1476808 191920 true 1138 5 <3>
END PROCS 0 2588256 1258664 1476808 193304 true 1137 5 <3>
gs_std_e.ps 0 2608352 1264725 1476808 194688 true 1137 5 <6>
gs_il1_e.ps 0 2608352 1266914 1476808 194688 true 1137 5 <9>
END FONTDIR/ENCS 0 2608352 1267092 1476808 194688 true 1137 5 <15>
END DEVS 0 2611856 1274124 1476808 194688 true 1137 5 <15>
END STATD 0 2611856 1281294 1496904 199640 true 1137 5 <39>
END GS_FONTS 0 2652048 1312194 1496904 199640 true 1138 5 <45>
END BASIC COLOR 0 2652048 1318761 1496904 199640 true 1136 5 <48>
END LEVEL 1 COLOR 0 2652048 1320495 1496904 199640 true 1136 5 <51>
END IMAGE 0 2672144 1327155 1496904 199640 true 1136 5 <54>
gs_btokn.ps 0 2672144 1330603 1496904 199640 true 995 4 <57>
gs_dps1.ps 0 2672144 1331954 1496904 199640 true 995 4 <57>
gs_dps2.ps 0 2672144 1333967 1496904 199640 true 995 4 <57>
gs_type1.ps 16 2672144 1335499 1496904 199640 true 995 4 <57>
While reading gs_lev2.ps:
%%[ Error: invalidaccess; OffendingCommand: put ]%%
START 333850 2746856 1404704 1496904 201896 true 999 7 <92>
END PROCS 333850 2746856 1415987 1496904 203280 true 997 7 <92>
gs_std_e.ps 333850 2766952 1422048 1496904 204664 true 997 7 <95>
gs_il1_e.ps 333850 2766952 1424237 1496904 204664 true 997 7 <98>
END FONTDIR/ENCS 333850 2766952 1424415 1496904 204664 true 997 7 <104>
END DEVS 333850 2770456 1431447 1496904 204664 true 997 7 <104>
END STATD 333850 2770456 1438681 1496904 206288 true 997 7 <128>
END GS_FONTS 333850 2800400 1467839 1496904 206288 true 997 7 <134>
END BASIC COLOR 333850 2820496 1477734 1496904 206288 true 995 7 <137>
END LEVEL 1 COLOR 333850 2820496 1479468 1496904 206288 true 995 7 <140>
END IMAGE 333850 2820496 1482776 1496904 206288 true 995 7 <143>
gs_btokn.ps 333850 2820496 1486224 1496904 206288 true 995 7 <146>
gs_dps1.ps 333850 2820496 1487575 1496904 206288 true 995 7 <146>
gs_dps2.ps 333850 2820496 1489588 1496904 206288 true 995 7 <146>
gs_type1.ps 333850 2840592 1494496 1496904 206288 true 995 7 <146>
While reading gs_lev2.ps:
%%[ Error: invalidaccess; OffendingCommand: put ]%%
' @ error/pdf.c/ReadPDFImage/811
Exception ignored in: <bound method Resource.__del__ of <wand.image.Image: (empty)>>
Traceback (most recent call last):
  File "C:\Users\l-gunalan\AppData\Local\Continuum\anaconda3\lib\site-packages\wand-0.4.4-py3.6.egg\wand\resource.py", line 232, in __del__
  File "C:\Users\l-gunalan\AppData\Local\Continuum\anaconda3\lib\site-packages\wand-0.4.4-py3.6.egg\wand\image.py", line 2767, in destroy
TypeError: object of type 'NoneType' has no len()

我真的不知道为什么会出现 NoneType 错误。我也尝试过其他方法,但我得到了同样的错误。 PDF有什么问题吗?我怎样才能找到它? 有关如何纠正此错误的任何建议?

【问题讨论】:

  • 检查您的回溯错误。您正在尝试查找 None 对象的长度。
  • @RavitejaAinampudi 我没有尝试查找对象的长度,但我仍然收到此错误

标签: python


【解决方案1】:

我以为你正在尝试将其转换为 png - 为什么将其另存为 jpg?

另外,为什么不改用这个呢?:

使用 pdf2image

pip install pdf2image

然后用它从pdf中获取图片

from pdf2image import convert_from_path
pages = convert_from_path('pdf_file', 500)

for page in pages:
    page.save('out.png', 'png')

【讨论】:

  • 我在 With 语句中遇到错误。它可以是 png 或 jpg,这不是问题所在。
  • “for page in pages”最终只会将一页保存为“out.png”。有没有办法连接文件并将其保存为一个多页“out.png”?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 2020-06-27
  • 2015-03-05
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多