Search code examples
pythonpython-imaging-libraryeps

Generate barcode image from PIL.EPSImageFile instance


I want to generate a barcode image. So, I used elaphe package. It works correctly but it returns PIL.EPSImageFile instance. I don't know how I can convert it to image format like SVG, PNG or JPG.

The code I have written is:

barcode('code128', 'barcodetest')

And it returns:

<PIL.EpsImagePlugin.EpsImageFile image mode=RGB size=145x72 at 0x9AA47AC>

How can I convert this instance to image? Actually I think my question is wrong but I don't know how to explain it well!


Solution

  • Simply save that file object to something with a .png or .jpg filename:

    bc = barcode('qrcode',
        'Hello Barcode Writer In Pure PostScript.',
        options=dict(version=9, eclevel='M'), 
        margin=10, data_mode='8bits')
    bc.save('yourfile.jpg')
    

    or state the format explicitly:

    bc.save('yourfile.jpg', 'JPEG')
    

    PIL will then convert the image to the correct format.

    Note that the PIL EPS module uses the gs command from the Ghostscript project to do it's conversions, you'll need to have it installed for this to work.