1

I have the following piece of python code to convert PDF to JPG.

with Img(filename=pdfName, resolution=300) as pic:
    pic.compression_quality = self.compressionQuality
    pic.background_color = Color("white")
    pic.alpha_channel = 'remove'
    pic.save(filename=output)

My problem is, with a large PDF file (10mb) I have the following error :

File "/home/nathan/PycharmProjects/oc_for_maarch/worker.py", line 44, in <module>
    launch(args)
  File "/home/nathan/PycharmProjects/oc_for_maarch/src/main.py", line 105, in launch
    q = process(args, path + file, Log, Separator, Config, Image, Ocr, Locale, WebService, q)
  File "/home/nathan/PycharmProjects/oc_for_maarch/src/process/OCForMaarch.py", line 48, in process
    Image.pdf_to_jpg(file + '[0]')
  File "/home/nathan/PycharmProjects/oc_for_maarch/src/classes/Images.py", line 36, in pdf_to_jpg
    self.save_img_with_wand(pdfName, self.jpgName)
  File "/home/nathan/PycharmProjects/oc_for_maarch/src/classes/Images.py", line 46, in save_img_with_wand
    with Img(filename=pdfName, resolution=300) as pic:
  File "/home/nathan/Documents/OpenCV/lib/python3.7/site-packages/wand/image.py", line 6406, in __init__
    self.read(filename=filename, resolution=resolution)
  File "/home/nathan/Documents/OpenCV/lib/python3.7/site-packages/wand/image.py", line 6799, in read
    raise WandRuntimeError(msg)
wand.exceptions.WandRuntimeError: MagickReadImage returns false, but did raise ImageMagick exception. This can occurs when a delegate is missing, or returns EXIT_SUCCESS without generating a raster.

I checked a little on Internet, and for what I've seen the problems was related to ghostscript but It's installed

I have the problem on Debian 10 and Ubuntu 19.04 using Python 3.7

EDIT : If I put the resolution to 100 instead of 300, I didn't have the issue

Nathan Cheval
  • 773
  • 2
  • 7
  • 32
  • Tried these suggestions? https://stackoverflow.com/questions/57271287/user-wand-by-python-to-convert-pdf-to-jepg-raise-wand-exceptions-wandruntimeerr – clubby789 Aug 07 '19 at 14:07
  • Yes. I edit my post because if I put the resolution to 100 instead of 300 I have no problem so there is no dependency issue I think – Nathan Cheval Aug 07 '19 at 14:16
  • 2
    Sounds like you might need to edit your policy.xml file to allow for more ram or map space. See policy.xml at https://imagemagick.org/script/resources.php – fmw42 Aug 07 '19 at 16:49
  • Thanks @fmw42 that's it ! Put your answer as a real answer instead of here so I can mark it as the correct answer ;) – Nathan Cheval Aug 08 '19 at 09:30

1 Answers1

2

When you rasterize at a high density, you will make a potentially very large dimension image from your PDF. So it sounds like you may be running out of RAM. If so, then you need to edit your ImageMagick policy.xml file to allow for more ram or map space. See policy.xml at https://imagemagick.org/script/resources.php. It controls your resources which you can view with the command line command:

convert -list resource
fmw42
  • 46,825
  • 10
  • 62
  • 80