I have a script, part of a Django application that creates thumbnails from PDF files. I use a great wand package to do this: Here are the Wand docs
It works fine if I do ./manage.py runserver from the command line, but if I start from PyCharm, it breaks.
When I look at the code, the problem is that the code used to open the block always returns an empty wand.image object. This is the correct class ( wand.image ), but it is empty. The object I am passing is pdf, but the blob conversion that does not produce any error is empty.
The error occurs on the next line ( single_image = all_pages.sequence[0] ), because all_pages empty, so the index is out of range.
Again, if I start the server from the command line, it works, but if I start from PyCharm, it is interrupted.
I am using virtualenv.
Here is the code I'm running:
from wand.image import Image as WandImage from wand.color import Color def convert_to_thumb(pdf_path, slug): with open(pdf_path) as f: image_binary = f.read() all_pages = WandImage(blob=image_binary) #<-- Here image_binary is a pdf single_image = all_pages.sequence[0] #<-- BOOM! all_pages is a wand.image, but it empty. Gives an Index error with WandImage(single_image) as i: i.format = 'png' i.background_color = Color('white') i.alpha_channel = 'remove' i.transform(resize='x100') save_name = slug + '_pdf_preview.png' i.save(filename='/foo/bar/' + save_name) return i
EDIT: Here is some debugging information.
When I run from the CLI and use pdb.set_trace() to check the value of all_pages , I get this
(Pdb) p all_pages <wand.image.Image: 3da0549 'PDF' (612x792)>
But when I do the same from the PyCharm console, I get:
(Pdb) >? p all_pages <wand.image.Image: (empty)>
The value of image_binary is the same in both cases. I distinguished between them.
In addition, the value of libmagick (setting ImageMagick) is /usr/local/lib/libMagickWand.dylib in both cases.
EDIT: This is interesting. If I run PyCharm from the system terminal, it works fine.
EDIT: Added appropriate launch configuration options
<configuration default="false" name="foo_bar_app" type="Python.DjangoServer" factoryName="Django server"> <option name="INTERPRETER_OPTIONS" value="" /> <option name="PARENT_ENVS" value="true" /> <envs> <env name="PYTHONUNBUFFERED" value="1" /> <env name="FOO_USERID" value="foobar" /> <env name="DJANGO_SETTINGS_MODULE" value="foo_bar.settings" /> </envs> <option name="SDK_HOME" value="$USER_HOME$/venv/foo_bar/bin/python" /> <option name="WORKING_DIRECTORY" value="" /> <option name="IS_MODULE_SDK" value="false" /> <option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" /> <module name="foo_bar_app" /> <option name="launchJavascriptDebuger" value="false" /> <option name="port" value="8000" /> <option name="host" value="" /> <option name="additionalOptions" value="" /> <option name="browserUrl" value="" /> <option name="runTestServer" value="false" /> <option name="runNoReload" value="false" /> <option name="useCustomRunCommand" value="false" /> <option name="customRunCommand" value="" /> <RunnerSettings RunnerId="PyDebugRunner" /> <RunnerSettings RunnerId="PythonCover" /> <RunnerSettings RunnerId="PythonRunner" /> <ConfigurationWrapper RunnerId="PyDebugRunner" /> <ConfigurationWrapper RunnerId="PythonCover" /> <ConfigurationWrapper RunnerId="PythonRunner" /> <method /> </configuration>