Py学习  »  Python

windows上python提供损坏图像的图像magick错误:无法读取图像数据

kumarm • 5 年前 • 575 次点击  

嗨,我在尝试将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中打开和查看图像,因此我确信页面没有损坏。

谢谢。

更新

谢谢你的回复。

我在网上找到了一些代码,它正在生成jpeg文件,但替换它们,而不是创建新文件,我以前从未做过任何子进程,如果程序正在运行或失败,在这段代码中没有可见性,或者如何杀死它,这里的任何输入也值得赞赏。

我知道它不再使用图像魔术仍然我可以,只要我可以生成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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54025
 
575 次点击