I have a script, part of a Django app, that makes thumbnails from pdfs. I'm using the excellent wand package to do it: Here are the Wand docs
It runs fine if I do ./manage.py runserver
from the command line, but if I run from PyCharm, it breaks.
When I step through the code, the problem is that the code used to open the blob always returns an empty wand.image
object. It's the right class (wand.image
) but it's an empty one. The object I pass it is a pdf, but the blob conversion, which produces no error at all, is empty.
The error occurs in the next line (single_image = all_pages.sequence[0]
) because all_pages
is empty, so the index is out of range.
Again, if I launch the server from the command line, it works, but if I launch from PyCharm, it breaks.
I'm using virtualenv.
Here's 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's 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's some Debug info
When I run from 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 thing from the PyCharm console, I get:
(Pdb) >? p all_pages
<wand.image.Image: (empty)>
The value of image_binary
appears to be identical in both cases. I diffed them.
Furthermore, the value of libmagick
(the ImageMagick install) is /usr/local/lib/libMagickWand.dylib
in both cases.
EDIT: This is interesting. If I launch PyCharm from the system Terminal, it works fine.
EDIT: Added relevant run configuration settings
<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>