嗨,我在尝试将PDF文件转换为.jpeg时遇到问题 我正在Windows计算机上的anaconda发行版中运行python。
下面是适用于某些pdf的代码
import os
from wand.image import Image as wi
pdf_dir = r"C:\\Users\Downloads\python computer vison\Computer-Vision-with-Python\pdf_to_convert"
os.chdir(pdf_dir)
path = r"C:/Users/Downloads/python computer vison/Computer-Vision-with-Python/jpeg_extract/"
for pdf_file in os.listdir(pdf_dir):
print("filename is ",pdf_file)
pdf = wi(filename=pdf_file,resolution=300)
#print("filename is ",pdf_file)
pdfImage = pdf.convert("jpeg")
i = 1
for img in pdfImage.sequence:
page = wi(image=img)
page.save(filename=path+pdf_file+str(i)+".jpg")
i+=
下面是输出
filename is tmpdocument-page0.pdf
filename is tmpdocument-page1.pdf
filename is tmpdocument-page100.pdf
filename is tmpdocument-page1000.pdf
filename is tmpdocument-page1001.pdf
filename is tmpdocument-page1002.pdf
filename is tmpdocument-page1003.pdf
filename is tmpdocument-page1004.pdf
filename is tmpdocument-page1005.pdf
filename is tmpdocument-page1006.pdf
filename is tmpdocument-page1007.pdf
filename is tmpdocument-page1008.pdf
filename is tmpdocument-page1009.pdf
filename is tmpdocument-page1012.pdf
---------------------------------------------------------------------------
CorruptImageError Traceback (most recent call last)
<ipython-input-7-84715f25da7c> in <module>()
8 #path = r"C://Users/Downloads/Work /ml_training_samples/tmp/"
9 print("filename is ",pdf_file)
---> 10 pdf = wi(filename=pdf_file,resolution=300)
11 #print("filename is ",pdf_file)
12 pdfImage = pdf.convert("jpeg")
~\Anaconda3\envs\python-cvcourse\lib\site-packages\wand\image.py in __init__(self, image, blob, file, filename, format, width, height, depth, background, resolution, pseudo)
4706 self.read(blob=blob, resolution=resolution)
4707 elif filename is not None:
-> 4708 self.read(filename=filename, resolution=resolution)
4709 # clear the wand format, otherwise any subsequent call to
4710 # MagickGetImageBlob will silently change the image to this
~\Anaconda3\envs\python-cvcourse\lib\site-packages\wand\image.py in read(self, file, filename, blob, resolution)
5000 r = library.MagickReadImage(self.wand, filename)
5001 if not r:
-> 5002 self.raise_exception()
5003
5004 def save(self, file=None, filename=None):
~\Anaconda3\envs\python-cvcourse\lib\site-packages\wand\resource.py in raise_exception(self, stacklevel)
220 warnings.warn(e, stacklevel=stacklevel + 1)
221 elif isinstance(e, Exception):
--> 222 raise e
223
224 def __enter__(self):
CorruptImageError: unable to read image data `C:/Users/AppData/Local/Temp/magick-40700dP2k-1ORw81R1' @ error/pnm.c/ReadPNMImage/1346
巴赫地 因此,我有一个名为tmpdocument的pdf图像文档,该文档有2200多页,因此我使用python将其拆分为单独的pdf文档。现在,我正尝试将其转换为jpeg。
问题:
因此,当我尝试将pdf格式转换为jpeg格式时,某些页面成功执行,而某些页面fa9.ils出现上述错误,因为所有这些页面均来自同一文档,我高度怀疑这是格式问题。我也可以在Adobe中打开并查看图像,所以我确定该页面没有损坏。
最后,映像魔术占用了如此多的磁盘空间,然后我真的迷失了这个问题,那就是还有其他方法可以实现上述场景吗?任何输入都会有所帮助。
感谢您的回复。 是的,我正在使用ghostscript 9.26。 pdf是一种敏感数据,因此我很遗憾无法在线发布。临时文件夹为18mb,所以我认为可以。
我在网上发现了一些代码,它正在生成jpeg文件,但是替换它们而不是创建新文件,我之前从未做过任何子处理,并且如果程序正在运行或失败,或者如何杀死它,则此代码不可见。在这里的输入也表示赞赏。
我了解它不再使用image magick了,只要我可以生成jpeg,我就可以了。
import os, subprocess
pdf_dir = r"C:\\Users\Downloads\latest_python\python computer vison\Computer-Vision-with-Python\pdf_to_convert"
os.chdir(pdf_dir)
pdftoppm_path = r"C:\Program Files\poppler-0.68.0_x86\poppler-0.68.0\bin\pdftoppm.exe"
i = 1
for pdf_file in os.listdir(pdf_dir):
if pdf_file.endswith(".pdf"):
subprocess.Popen('"%s" -jpeg %s out' % (pdftoppm_path, pdf_file))
i+=1