title: Errors converting pdf to jpg using wand, Imagemagick, and Django
body: <p>I am using ImageMagick 6.7.7-10 2017-07-31 Q16 on Ubuntu 14.04, through Wand 0.4.4, Python 3.4.3 and Django 1.11. I am trying to create a jpg thumbnail of a pdf file. </p>
  
  <p>On the command line, I can do this with no errors:</p>
  
  <pre><code>convert -thumbnail x300 -background white -alpha remove Lucy.pdf[0] output_thumbnail.jpg
  </code></pre>
  
  <p>But when I try to use wand on the same image I get this error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I looked at the delegates.xml file for ImageMagic in /etc, and there are entries for pdf files. </p>
  
  <p>Thanks for any suggestions on how to get this conversion to work through wand.</p>
  
  <p>Mark</p>
  
  <p>I hacked a simple Python script from my django code to test wand with pdfs, and it gives the same error. I also tested whether Ghostscript is in the path, and printed out the results of convert -list delegates. </p>
  
  <pre><code>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()
  </code></pre>
  
  <p>The output from this program is as follows. It has the same missing delegates error. The same error also occurs with tiff files, but not with jpg files (the first test file is a jpg and the code processed the file and created the thumbnail image). The program also prints the version of GS, which is 9.10, so that is accessible from the program, and it prints out the delegates it found, which include pdfs.:</p>
  
  <pre><code>python thumbs.py
  main
  test_delegate
  
  Path: /etc/ImageMagick/delegates.xml
  
  Delegate                Command
  -------------------------------------------------------------------------------
      blender =&gt;          "blender" -b "%i" -F PNG -o "%o""\n"convert" -concatenate "%o*.png" "%o"
          cdr =&gt;          "uniconvertor" "%i" "%o.svg"; mv "%o.svg" "%o"
          cgm =&gt;          "ralcgm" -d ps -oC &lt; "%i" &gt; "%o" 2&gt; "%Z"
   dng:decode =&gt;          "ufraw-batch" --silent --create-id=also --out-type=png --out-depth=16 "--output=%u.png" "%i"
          dot =&gt;          "dot" -Tsvg "%i" -o "%o"
          dvi =&gt;          "dvips" -q -o "%o" "%i"
          eps&lt;=&gt;pdf       "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i"
          eps&lt;=&gt;ps        "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=nodevice" "-sOutputFile=%o" "-f%i"
          fig =&gt;          "fig2dev" -L ps "%i" "%o"
          hpg =&gt;          "hp2xx" -q -m eps -f `basename "%o"` "%i";     mv -f `basename "%o"` "%o"
         hpgl =&gt;          "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 =&gt;          "html2ps" -U -o "%o" "%i"
         html =&gt;          "html2ps" -U -o "%o" "%i"
        https =&gt;          "curl" -s -k -o "%o" "https:%F"
         ilbm =&gt;          "ilbmtoppm" "%i" &gt; "%o"
          man =&gt;          "groff" -man -Tps "%i" &gt; "%o"
         miff&lt;= show      "/usr/bin/display" -delay 0 -window-group %[group] -title "%l " "ephemeral:%i"
  mpeg:decode =&gt;          "ffmpeg" -v -1 -i "%i" -vframes %S -vcodec pam -an -f rawvideo -y "%u.pam" 2&gt; "%Z"
          pdf&lt;=&gt;eps       "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i"
          pdf&lt;=&gt;ps        "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=nodevice" "-sOutputFile=%o" "-f%i"
          pnm&lt;= ilbm      "ppmtoilbm" -24if "%i" &gt; "%o"
          pov =&gt;          "povray" "+i%i" -D0 "+o%o" +fn%q +w%w +h%h +a -q9 "-kfi%s" "-kff%n";"convert" -concatenate "%o*.png" "%o"
           ps&lt;=&gt;eps       "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i"
           ps&lt;=&gt;pdf       "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i"
           ps&lt;= print     "lpr "%i"
         rgba&lt;= rle       "rawtorle" -o "%o" -v "%i"
         scan =&gt;          "scanimage" -d "%i" &gt; "%o"
        scanx =&gt;          "scanimage" &gt; "%o"
        shtml =&gt;          "html2ps" -U -o "%o" "%i"
          sid =&gt;          "mrsidgeodecode" -if sid -i "%i" -of tif -o "%o" &gt; "%u"
          svg =&gt;          "rsvg-convert" -o "%o" "%i"
         tiff&lt;= launch    "gimp" "%i"
          txt&lt;=&gt;ps        "enscript" -o "%o" "%i"
          wmf =&gt;          "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 &lt;module&gt;
      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: &lt;bound method Image.__del__ of &lt;wand.image.Image: (empty)&gt;&gt;
  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()
  </code></pre>
  
  <p>Maybe this simple python code will help someone give me a direction to go to solve this problem. However, I am beginning to suspect there is a bug in wand when one uses byte arrays instead of file names. </p>
  
  <p>Thanks!</p>
  
  <p>Mark</p>