0

So I have installed Wand, Ghostscript, ImageMagick. I am trying to convert a PDF to Image. My Code is as following.

Code:

from wand.image import Image
image_pdf = Image(filename="/<fullpath>/xyz.pdf", resolution=500)
image_jpeg = image_pdf.convert('jpeg')
print (len(image_jpeg.sequence))

When I run the code through terminal (I mean open python terminal and paste the code there), it works. But the same code fails in PyCharm.

Error:

File "/usr/local/lib/python2.7/site-packages/wand/resource.py", line 222, in raise_exception
raise e
wand.exceptions.DelegateError: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r500x500'  '-sOutputFile=/var/folders/61/7q0vknr92mndbbgzqvsk3xl4r4yw3f/T/magick-24738xypZ5LDqNaTJ%d' '-f/var/folders/61/7q0vknr92mndbbgzqvsk3xl4r4yw3f/T/magick-24738Tr70PW391Vdt' '-f/var/folders/61/7q0vknr92mndbbgzqvsk3xl4r4yw3f/T/magick-24738wI4q1Lv6Aich'' (1) @ error/pdf.c/InvokePDFDelegate/292
Exception TypeError: TypeError("object of type 'NoneType' has no len()",) in <bound method Image.__del__ of <wand.image.Image: (empty)>> ignored

I checked the python version in terminal which python2 and I get /usr/local/bin/python2

My PyCharm pythton Interpreter is located at /usr/local/Cellar/python/2.7.13_1/....

What am I missing here?

martineau
  • 119,623
  • 25
  • 170
  • 301
AgentX
  • 1,402
  • 3
  • 23
  • 38
  • Looks like `image_jpeg` is `None`, which means that's what the `Image(filename="//xyz.pdf", resolution=500)` call returned—so you need to figure out why that happened. See if it works outside of PyCharm. – martineau Sep 10 '17 at 15:45
  • @martineau the issue was due to incomplete path variable in PyCharm. – AgentX Sep 10 '17 at 16:09
  • Glad you've found at least a workaround. I've seen quite a few question here about things not working in PyCharm that work outside of it. – martineau Sep 10 '17 at 16:17

2 Answers2

1

You should look to isolate your Python environment from your system Python (and yes, your Homebrew installation would be considered a system Python installation too) by using virtualenvwrapper.

From what I'm seeing, the Python that your system is using is not the same Python that PyCharm is using. While that can be fixed by going to Settings > Project Interpreter and selecting the right path for your interpreter...

enter image description here

...it would be best to ensure that you install all of the necessary dependencies your project needs in an isolated virtual environment instead.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • @Makoto - I have a question. The same issue for me - working on the console but not working on pycharm. I set the path of the pycharm to python interpreter in isolated env like this: /Volumes/WORK/Projects/BazeanAnalytics/web-app-baz-analytics/API/data-api/venv, and not working yet. – Deimos620 Jan 30 '18 at 16:38
0

The issue was that since PyCharm was not starting through Terminal it was not picking up all the Path Variables and hence was not able to find the required dependencies (I assume GhostScript might be the missing dep here)

So when I started PyCharm from Terminal, it worked.

AgentX
  • 1,402
  • 3
  • 23
  • 38