SAN
SAN

Reputation: 79

ImageMagick's convert fails only when invoked through python's subprocess.call

I am trying to convert the first page of a pdf file to a thumbnail JPEG file. I have the code similar to the following python code.

from tempfile import TemporaryFile, NamedTemporaryFile
import os
from subprocess import call
…
def createThumbnail(reporttempfile, thumbnailtempfile):
    # build the command for executing for extracting thumbnail
    command = "convert -resize 241x344 -background white -gravity center -extent 241x344 -quality 75" 
    csplit = command.split(' ')
    csplit.append("pdf:"+reporttempfile.name + "[0]")
    csplit.append(thumbnailtempfile.name)
    print "csplit = ", csplit
    print "cmd = %s" % " ".join(csplit)
    # run the command convert to create thumbnail
    retval = call(csplit)
    print "retval = %d" % retval
    return retval

When I invoke the above function I get the following output:

csplit =  ['convert', '-resize', '241x344', '-background', 'white', '-gravity', 'center', '-extent', '241x344', '-quality', '75', 'pdf:/tmp/reportfile0001_zeEJ8B.pdf[0]', '/tmp/thumbnail0001_DTPHGb.jpg']

cmd = convert -resize 241x344 -background white -gravity center -extent 241x344 -quality 75 pdf:/tmp/reportfile0001_zeEJ8B.pdf[0] /tmp/thumbnail0001_DTPHGb.jpg
   **** Error: Cannot find a 'startxref' anywhere in the file.
   **** Warning:  An error occurred while reading an XREF table.
   **** The file has been damaged.  This may have been caused
   **** by a problem while converting or transfering the file.
   **** Ghostscript will attempt to recover the data.
   **** Error:  Trailer is not found.

Requested FirstPage is greater than the number of pages in the file: 0
   No pages will be processed (FirstPage > LastPage).

   **** This file had errors that were repaired or ignored.
   **** Please notify the author of the software that produced this
   **** file that it does not conform to Adobe's published PDF
   **** specification.

convert.im6: Postscript delegate failed `/tmp/reportfile0001_zeEJ8B.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/677.
convert.im6: no images defined `/tmp/thumbnail0001_DTPHGb.jpg' @ error/convert.c/ConvertImageCommand/3044.
retval = 1
1

However, when I invoke the command directly by typing on the shell, convert succeeds without any issues and I can see that .jpg file is correctly converted. The command used is (what is also printed in the output above):

convert -resize 241x344 -background white -gravity center -extent 241x344 -quality 75 pdf:/tmp/reportfile0001_zeEJ8B.pdf[0] /tmp/thumbnail0001_DTPHGb.jpg

I have seen this behaviour on Mac (10.9.5) with ImageMagick 6.9.0-0 Q16 x86_64 2015-04-09 and also with Ubuntu 14 with ImageMagick 6.7.7-10 2014-03-06 Q16.

I am at my wit's end as to why this is happening. Can somebody please help?

Regards,

SN

Upvotes: 0

Views: 1088

Answers (1)

Omar
Omar

Reputation: 56

You have to close the temporary file before it is read by convert. You can open the temporary file with the delete=False option to avoid deleting it when it is closed using its close() method. To delete it manually after the processing has finished, use its unlink() method which happens to be the same as the os.unlink function (you have to pass the temporary file name attribute as its first argument).

Upvotes: 1

Related Questions