我有一个脚本,一个Django应用程序的一部分,它从pdfs制作缩略图。我使用优秀的魔杖包来完成它:Here are the Wand docs
如果我从命令行执行./manage.py runserver
,它运行正常,但如果我从PyCharm运行,它会中断。
当我单步执行代码时,问题是用于打开blob的代码总是返回一个空的wand.image
对象。它是正确的班级(wand.image
),但它是空的。我传递的对象是pdf,但blob转换(根本不产生任何错误)是空的。
错误发生在下一行(single_image = all_pages.sequence[0]
),因为all_pages
为空,因此索引超出范围。
同样,如果我从命令行启动服务器,它可以工作,但是如果我从PyCharm启动它,它会中断。
我正在使用virtualenv。
这是我正在运行的代码:
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
编辑:这里有一些调试信息
当我从CLI运行并使用pdb.set_trace()
检查all_pages
的值时,我得到了这个
(Pdb) p all_pages
<wand.image.Image: 3da0549 'PDF' (612x792)>
但是当我从PyCharm控制台做同样的事情时,我得到:
(Pdb) >? p all_pages
<wand.image.Image: (empty)>
image_binary
的值在两种情况下都是相同的。我把它们分开了。
此外,在这两种情况下,libmagick
(ImageMagick安装)的值均为/usr/local/lib/libMagickWand.dylib
。
编辑:添加了相关的运行配置设置
<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>
答案 0 :(得分:0)
编辑:这很有趣。如果我从系统终端启动PyCharm,它可以正常工作。
当您从终端启动时,听起来您正在使用不同的用户权限运行PyCharm。
答案 1 :(得分:0)
几个月前我遇到了这个问题。我发现解决此问题的唯一方法是在管理模式下启动所有内容。这可能是系统拒绝权限的问题。