我通过 Wand 0.4.4、Python 3.4.3 和 Django 1.11 在 Ubuntu 14.04 上使用 ImageMagick 6.7.7-10 2017-07-31 Q16。我正在尝试创建 pdf 文件的 jpg 缩略图。
在命令行上,我可以做到这一点没有错误:
convert -thumbnail x300 -background white -alpha remove Lucy.pdf[0] output_thumbnail.jpg
但是当我尝试在同一张图片上使用 wand 时,我得到了这个错误:
Traceback (most recent call last):
File "/home/mark/python-projects/memorabilia-project/memorabilia/models.py", line 24, in make_thumb
pages = Image(blob = b)
File "/home/mark/.virtualenvs/memorabilia/lib/python3.4/site-packages/wand/image.py", line 2742, in __init__
self.read(blob=blob, resolution=resolution)
File "/home/mark/.virtualenvs/memorabilia/lib/python3.4/site-packages/wand/image.py", line 2822, in read
self.raise_exception()
File "/home/mark/.virtualenvs/memorabilia/lib/python3.4/site-packages/wand/resource.py", line 222, in raise_exception
raise e
wand.exceptions.MissingDelegateError: no decode delegate for this image format `' @ error/blob.c/BlobToImage/367
我查看了 /etc 中 ImageMagic 的 delegates.xml 文件,并且有 pdf 文件的条目。
感谢您提供有关如何通过魔杖进行此转换的任何建议。
标记
我从我的 django 代码中破解了一个简单的 Python 脚本来测试带有 pdf 的 wand,它给出了同样的错误。我还测试了 Ghostscript 是否在路径中,并打印了 convert -list 委托的结果。
from wand.image import Image
from os.path import splitext
import uuid
import os
import hashlib
newname = "sam.jpg"
oldnames = ["16u.jpg", "Lucy.pdf", "Jimbo.tiff", "john_edmonds.pdf", ]
size = "200"
RESOLUTION = 200
test = "4"
TEST_DATA = "test_data/"
def test_delegate():
print ("test_delegate")
print(os.system("convert -list delegate"))
def test_gs():
print ("test_gs")
retval = os.system("gs --version")
print (retval)
def read_image(file_name):
f = open(file_name, 'rb')
h = hashlib.sha256()
buff_size = 128*1024
for b in iter(lambda : f.read(buff_size), b""):
h.update(b)
computed_sha256 = h.hexdigest()
f.close()
print ("b="+str(len(b))+", computed_sha256="+computed_sha256)
return b
def create_thumb(names):
for img in names:
img = TEST_DATA + img
print(img)
thumb_name, thumb_extension = os.path.splitext(img)
thumb_extension = thumb_extension.lower()
bytes = read_image(img)
if thumb_extension in [".pdf",]:
print("found pdf")
#pages = Image(filename = img)
pages = Image(blob = bytes)
first_page = pages.sequence[0]
image = Image(first_page)
else:
print("found image")
#image = Image(filename=img)
image = Image(blob = bytes)
image.transform(resize="x"+size)
image.format = "jpg"
image.save(filename=thumb_name+"_thumb_"+test+".jpg")
print("finished successfully")
def main():
print("main")
test_delegate()
test_gs()
create_thumb(oldnames)
if __name__ == '__main__':
main()
该程序的输出如下。它具有相同的缺少代表错误。tiff 文件也会出现相同的错误,但 jpg 文件不会出现(第一个测试文件是 jpg 并且代码处理了文件并创建了缩略图)。该程序还打印 GS 的版本,即 9.10,以便可以从程序中访问,并打印出它找到的代表,其中包括 pdfs。:
python thumbs.py
main
test_delegate
Path: /etc/ImageMagick/delegates.xml
Delegate Command
-------------------------------------------------------------------------------
blender => "blender" -b "%i" -F PNG -o "%o""\n"convert" -concatenate "%o*.png" "%o"
cdr => "uniconvertor" "%i" "%o.svg"; mv "%o.svg" "%o"
cgm => "ralcgm" -d ps -oC < "%i" > "%o" 2> "%Z"
dng:decode => "ufraw-batch" --silent --create-id=also --out-type=png --out-depth=16 "--output=%u.png" "%i"
dot => "dot" -Tsvg "%i" -o "%o"
dvi => "dvips" -q -o "%o" "%i"
eps<=>pdf "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i"
eps<=>ps "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=nodevice" "-sOutputFile=%o" "-f%i"
fig => "fig2dev" -L ps "%i" "%o"
hpg => "hp2xx" -q -m eps -f `basename "%o"` "%i"; mv -f `basename "%o"` "%o"
hpgl => "if [ -e hp2xx -o -e /usr/bin/hp2xx ]; then hp2xx -q -m eps -f `basename "%o"` "%i"; mv -f `basename "%o"` "%o"; else echo "You need to install hp2xx to use HPGL files with ImageMagick."; exit 1; fi"
htm => "html2ps" -U -o "%o" "%i"
html => "html2ps" -U -o "%o" "%i"
https => "curl" -s -k -o "%o" "https:%F"
ilbm => "ilbmtoppm" "%i" > "%o"
man => "groff" -man -Tps "%i" > "%o"
miff<= show "/usr/bin/display" -delay 0 -window-group %[group] -title "%l " "ephemeral:%i"
mpeg:decode => "ffmpeg" -v -1 -i "%i" -vframes %S -vcodec pam -an -f rawvideo -y "%u.pam" 2> "%Z"
pdf<=>eps "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i"
pdf<=>ps "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=nodevice" "-sOutputFile=%o" "-f%i"
pnm<= ilbm "ppmtoilbm" -24if "%i" > "%o"
pov => "povray" "+i%i" -D0 "+o%o" +fn%q +w%w +h%h +a -q9 "-kfi%s" "-kff%n";"convert" -concatenate "%o*.png" "%o"
ps<=>eps "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i"
ps<=>pdf "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i"
ps<= print "lpr "%i"
rgba<= rle "rawtorle" -o "%o" -v "%i"
scan => "scanimage" -d "%i" > "%o"
scanx => "scanimage" > "%o"
shtml => "html2ps" -U -o "%o" "%i"
sid => "mrsidgeodecode" -if sid -i "%i" -of tif -o "%o" > "%u"
svg => "rsvg-convert" -o "%o" "%i"
tiff<= launch "gimp" "%i"
txt<=>ps "enscript" -o "%o" "%i"
wmf => "wmf2eps" -o "%o" "%i"
0
test_gs
9.10
0
test_data/16u.jpg
b=43597, computed_sha256=0bac89048bbbcfa75ad7d9dbc84eae42ff6b30c0a057dd76e180a205d9021b8d
found image
finished successfully
test_data/Lucy.pdf
b=61053, computed_sha256=6e108603ad4f6ae2e08b3d2a419a65d3cc1f60b788e9377be15b1926892189f8
found pdf
Traceback (most recent call last):
File "thumbs.py", line 63, in <module>
main()
File "thumbs.py", line 60, in main
create_thumb(oldnames)
File "thumbs.py", line 44, in create_thumb
pages = Image(blob = bytes)
File "/home/mark/.virtualenvs/memorabilia/lib/python3.4/site-packages/wand/image.py", line 2742, in __init__
self.read(blob=blob, resolution=resolution)
File "/home/mark/.virtualenvs/memorabilia/lib/python3.4/site-packages/wand/image.py", line 2822, in read
self.raise_exception()
File "/home/mark/.virtualenvs/memorabilia/lib/python3.4/site-packages/wand/resource.py", line 222, in raise_exception
raise e
wand.exceptions.MissingDelegateError: no decode delegate for this image format `' @ error/blob.c/BlobToImage/367
Exception ignored in: <bound method Image.__del__ of <wand.image.Image: (empty)>>
Traceback (most recent call last):
File "/home/mark/.virtualenvs/memorabilia/lib/python3.4/site-packages/wand/resource.py", line 232, in __del__
File "/home/mark/.virtualenvs/memorabilia/lib/python3.4/site-packages/wand/image.py", line 2767, in destroy
TypeError: object of type 'NoneType' has no len()
也许这个简单的python代码会帮助有人给我一个解决这个问题的方向。但是,当使用字节数组而不是文件名时,我开始怀疑 wand 中存在错误。
谢谢!
标记