Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wand issues MissingDelegateError error when reading pdfs and tiffs as blobs, but no errors with jpgs #352

Closed
pmi123 opened this issue Nov 14, 2017 · 9 comments

Comments

@pmi123
Copy link

pmi123 commented Nov 14, 2017

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.

On the command line, I can do this with no errors:
convert -thumbnail x300 -background white -alpha remove Lucy.pdf[0] output_thumbnail.jpg
But when I try to use wand on the same image I get this error:

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

To remove django from the equation, I wrote a simple Python script to test my django 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()

I run this script in the same virtual environment of my django application. It generates the following output. Note that (1) it prints the correct version of GS, 9.10, and it prints out all the delegates it found, which include the delegates for pdfs and tiffs. However, it raises a delegate error when I try to process pdfs or tiffs.

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()

I think the bug is in how wand processes byte arrays, as that is how I am giving the file to wand. I have attached the test files.
16u
Lucy.pdf

Thanks!

Mark

@dahlia
Copy link
Collaborator

dahlia commented Nov 15, 2017

Could you try changing the code pages = Image(blob = bytes) to the following code?:

with Image() as pages:
    pages.format = 'pdf'
    pages.read(blob=bytes)

@pmi123
Copy link
Author

pmi123 commented Nov 15, 2017

Hong,

Sure. Code and output below. There is a different, but related, error: ValueError: 'pdf' is unsupported format

Output from this test:

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 65, in <module>
    main()
  File "thumbs.py", line 62, in main
    create_thumb(oldnames)
  File "thumbs.py", line 44, in create_thumb
    pages.format = 'pdf'
  File "/home/mark/.virtualenvs/memorabilia/lib/python3.4/site-packages/wand/image.py", line 2932, in format
    raise ValueError(repr(fmt) + ' is unsupported format')
ValueError: 'pdf' is unsupported format

Code used for the test:

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")
            with Image() as pages:
                pages.format = 'pdf'
                pages = read(blob = bytes)
            #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()

I also tried pages = Image(blob=bytes, format='pdf') earlier, and it generated this error. I just incldued the output from the point the program encountered the pdf file. The output above the line "found pdf" is the same.

found pdf
Error: /undefined in !
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1916   1   3   %oparray_pop   1915   1   3   %oparray_pop   1899   1   3   %oparray_pop   1787   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--
Dictionary stack:
   --dict:1178/1684(ro)(G)--   --dict:0/20(G)--   --dict:77/200(L)--
Current allocation mode is local
Current file position is 3
GPL Ghostscript 9.10: Unrecoverable error, exit code 1
Traceback (most recent call last):
  File "thumbs.py", line 65, in <module>
    main()
  File "thumbs.py", line 62, in main
    create_thumb(oldnames)
  File "thumbs.py", line 46, in create_thumb
    pages = Image(blob = bytes, format='pdf')
  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.DelegateError: Postscript delegate failed `/tmp/magick--VbkkTYU': No such file or directory @ error/pdf.c/ReadPDFImage/677
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()

I do have a /tmp directory, my hard drive is far from full, and there are other 'magic-xxxxxxx' files in the /tmp directory, so I am pretty confident ImageMagic can read and write that directory.

Thanks for looking into this!!

Mark

@pmi123
Copy link
Author

pmi123 commented Nov 15, 2017

A quick update. I have been experimenting with different image and pdf files. It seems that the original jpg file I used was just a lucky choice. I have other jpg files that fail.

I thought it might be file size related, because I took a large jpg that failed and reduced it to a smaller size, and that file worked. However, a gif file of 31052 bytes in size worked, and a jpg file of 30150 bytes failed.

The error message for all pdfs is 'pdf' is unsupported format". All the image file errors are "no decode delegate for this image format `' @ error/blob.c/BlobToImage/367"

I can send you the test files if you want.

Mark

@pmi123
Copy link
Author

pmi123 commented Nov 16, 2017

Another update. I replaced reading the files from binary with reading the files from the file system in my test program, and wand processed all of the files with no errors. So, I would say there is something broken in the reading of blobs versus files from the file system. Hope that helps!

Mark

@Tehlonewolf
Copy link

@pmi123 I'm facing a similar here. Would you be kind and let me know how you read the files from file system? My code doesnt have any of the open(filename'rb') functions, yet the DelegateError persists for me.

@prasun-singh
Copy link

prasun-singh commented Apr 4, 2018

I am trying to convert one of the pdf but it's giving me wand.CorruptImage exception, Though it is able to convert most of the pdf but for few it's giving the error. I checked the pdf to I can open in adobe, foxit, IE so it doesn't seem to be a problem with pdf. Can anyone help me in identifying what could be the possible reason for the error

this is how I am loading the pdf:
with Image(filename=f) as img:
img.alpha_channel = False
img.background_color = Color("white")

print('pages = ', len(img.sequence))

Here is detail error.

Traceback (most recent call last):
File "E:/PythonPractice/Practice/pdf_to_png_2.py", line 30, in
with Image(filename=f) as img:
File "C:\Users\prasun.singh\AppData\Local\Programs\Python\Python35\lib\site-packages\wand\image.py", line 2744, in init
self.read(filename=filename, resolution=resolution)
File "C:\Users\prasun.singh\AppData\Local\Programs\Python\Python35\lib\site-packages\wand\image.py", line 2822, in read
self.raise_exception()
File "C:\Users\prasun.singh\AppData\Local\Programs\Python\Python35\lib\site-packages\wand\resource.py", line 222, in raise_exception
raise e
wand.exceptions.CorruptImageError: unable to read image data `C:/Users/PRASUN~1.SIN/AppData/Local/Temp/magick-21120S5vtZDqxipZz1' @ error/pnm.c/ReadPNMImage/1337
Exception ignored in: <bound method Resource.del of <wand.image.Image: (empty)>>

@emcconville
Copy link
Owner

Don't know what to make of this issue, but I know this works for me.

with open('Lucy.pdf', 'rb') as fd:
    with Image(file=fd, resolution=100, format="PDF[0]") as pdf:
        pdf.transform(resize="x200")
        pdf.save(filename="first_page_thumbnail.jpg")

first_page_thumbnail

@Danielydsm
Copy link

I'm facing a similar problem, when I tried to open some files on wand the code gives me the following error:
raise e
wand.exceptions.CorruptImageError: unable to read image data `C:/Users/h119092/AppData/Local/Temp/magick-2964IqBjfCX3xNAi1' @ error/pnm.c/ReadPNMImage/1346

But I can convert the file through command lines (convert file.pdf file.jpg) using imagemagick.

My code in python:

def pdf_image(file, dir):
"""Convert pdf on image"""
name = re.sub('pdf', 'png', file.filename)
with Img(file=file, resolution=300) as img:
img.compression_quality = 99
img.save(filename=dir+'\'+name)

Is anyone have some thoughts about a solution?

Thank you in advance

@emcconville
Copy link
Owner

Issue should be resolved with #410, just be sure to use format parameter when reading blob/file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants