Python Barcode Generation
Question: I'm using the elaphe package for python to generate ean-13 barcode images. The
package was installed from source using the tar file found at
<https://pypi.python.org/pypi/elaphe>.

When I run the code:

    
    
    BARCODE_IMAGE_PATH = "/tmp/"
    
    def create_barcode_image(product_barcode):
    
        path = BARCODE_IMAGE_PATH + product_barcode + '.png'
    
        img = barcode('ean13', product_barcode, 
                      options=dict(includetext=True, height=0.4), margin=1)
        img.save(path, 'PNG')
    
        return path 
    

from the python interpreter it seems to work perfectly. The correct barcode is
generated to the path that I specify. When I run it from apache using web.py
as my web framework I receive the error:

    
    
    Traceback (most recent call last):
      ...
         img_path = create_barcode_image(barcode)
       File "/var/www/py/documents/barcode_images.py", line 27, in create_barcode_image
         img.save(path, 'PNG')
       File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1406, in save
         self.load()
       File "/usr/local/lib/python2.7/dist-packages/PIL/EpsImagePlugin.py", line 283, in load
         self.im = Ghostscript(self.tile, self.size, self.fp)
       File "/usr/local/lib/python2.7/dist-packages/PIL/EpsImagePlugin.py", line 75, in Ghostscript
         raise IOError("gs failed (status %d)" % status)
     IOError: gs failed (status 256) 
    

Does anyone know what might be causing this error or how to go about debugging
it?


Answer: Add in some debug statements that you can walk through:

    
    
    import sys
    
    BARCODE_IMAGE_PATH = "/tmp/"
    
    def create_barcode_image(product_barcode):
    
        print >> sys.stderr, "product_barcode: %s" % product_barcode
    
        path = BARCODE_IMAGE_PATH + product_barcode + '.png'
    
        print >> sys.stderr, "path: %s" % path
    
        img = barcode('ean13', product_barcode, 
                      options=dict(includetext=True, height=0.4), margin=1)
    
        print >> sys.stderr, "img data: %s" % img.tostring()
    
        img.save(path, 'PNG')
    
        print >> sys.stderr, "Saved to %s" % path
    
        return path 
    

Then in your shell:

    
    
    $ tail -F /var/log/httpd/error.log # or wherever you put it
    

You're looking for: First: the output of "`product_barcode: ...`". Hopefully
that's not blank. If it is, then the problem lies elsewhere, maybe in your
server config. Then the output of "`img data: ...`". Hopefully it's a png and
not blank. If it's blank, then the problem lies with your ghostscript
installation.

This is a very rudimentary way of debugging, and I feel that for small
projects it is just as easy to throw in some debug statements rather than
messing around with the debugger, which can be difficult to set up properly.

