0

When I did a code to convert PDF files to JPG images, but I have met an issue.Sometimes the .pdf file work well.But sometimes there is an error,the first temp file can't read.I checked the temp folder.When it works well, all the created temp file be deleted successfully. And when the error happens,only the first temp file is deleted. I can't resolve it.Anyone can fix?

code:

from wand.image import Image

with open("C:\software\1234.pdf",'rb') as f: image_binary = f.read()

f.close() with Image(blob=image_binary,resolution=400) as img:

Traceback (most recent call last): File "C:\xxxx\eclipse-workspace\test\123\pdf2jpg.py", line 16, in with Image(blob=image_binary,resolution=400) as img: File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\image.py", line 2742, in init self.read(blob=blob, resolution=resolution) File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\image.py", line 2822, in read self.raise_exception() File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\resource.py", line 222, in raise_exception raise e wand.exceptions.CorruptImageError: unable to read image data `C:/xxxx/AppData/Local/Temp/magick-22200zyw89Zpq8IFJ1' @ error/pnm.c/ReadPNMImage/1344 Exception ignored in: Traceback (most recent call last): File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\resource.py", line 232, in del File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\image.py", line 2767, in destroy TypeError: object of type 'NoneType' has no len()

tod.deng
  • 1
  • 1
  • You need to provide code and not just an error message. What version of ImageMagick? What platform? Are you using ImageMagick directly or via Python Wand or something else. Please be more specific. – fmw42 Aug 28 '18 at 02:29
  • Oh, I know you. Several days ago, you add a comment "Might it be possible that the PDF files that work are RGB and the ones that fail are CMYK? That is, your code is expecting 3 channels and your are providing 4 with CMYK. – fmw42 Aug 23 at 3:17" at https://stackoverflow.com/questions/50723020/constant-error-using-imagemagick-with-python-in-windows/51977482#51977482. – tod.deng Aug 28 '18 at 02:41
  • @fmw42, could you give a method to resolve the CMYK PDF file issue? – tod.deng Aug 28 '18 at 02:43
  • `@tod.deng`. It is not proper etiquette to ask a new question in a comment. Nevertheless, In ImageMagick command line, I would always convert PDFs to RGB. So something like `convert -colorspace sRGB -density XX image.pdf result.suffix. You have not told me what you are converting to, so I cannot supply the .suffix properly. If this is not what you need, then please post a new question with example images and code. Note that ImageMagick needs Ghostscript installed as a delegate to process PDFs. – fmw42 Aug 28 '18 at 03:36
  • How are you populating the `image_binary` variable? The error message is informing you that the data is corrupt. Try writing the contents of `image_binary` to disk, and evaluate what is corrupted. – emcconville Aug 28 '18 at 13:05
  • @emcconville with the above code,some PDF files can convert successfully. But some PDF files can't be converted. – tod.deng Aug 30 '18 at 03:24
  • You can try passing the file handler `Image(file=f)` or the filename `Image(filename="C:\software\1234.pdf")`, and see if the problem continues. – emcconville Aug 30 '18 at 13:47
  • @emcconville I have tryed several method.All the methods worked well.But some PDF file are always be converted wrong. I think this is a bug of wand or ImageMagick for some specific PDF files. – tod.deng Aug 31 '18 at 02:39

0 Answers0