Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9073579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:26:59+00:00 2026-06-16T18:26:59+00:00

I’m trying to insert a barcode image into Reportlab. I know there are a

  • 0

I’m trying to insert a barcode image into Reportlab. I know there are a lot of questions asked on this, but all of them assume that you already have the image file in the directory or on the filesystem.

Due to the fact that Reportlab has issues with EAN13 barcodes, I decided to use another package called pyBarcode to generate the image for me.

Initially I saved the image in a StringIO instance and passed it directly to reportlab.platypus.flowables.Image but that didn’t seem to work. Then I read the documentation:

Formats supported by PIL/Java 1.4 (the Python/Java Imaging Library) are supported.

Does this mean that if I pass a PIL image, this should work? I got an exception when I tried the following:

>>> import PIL
>>> from reportlab.platypus.flowables import Image
>>> fp = StringIO(the_barcode.getvalue())
>>> barcode_image = PIL.Image.open(fp)
>>> doc = SimpleDocTemplate('barcode.pdf')
>>> story = [Image(barcode_image)]
>>> Traceback (most recent call last):
  File "create.py", line 57, in <module>
    main()
  File "create.py", line 24, in main
    save_pdf(fp, STYLE, ART, COLOR, SIZE)
  File "create.py", line 28, in save_pdf
    fp = StringIO(fp.getvalue())
  File "/home/mark/.virtualenvs/barcode/local/lib/python2.7/site-packages/reportlab-2.6-py2.7-linux-i686.egg/reportlab/platypus/flowables.py", line 402, in __init__
    if not fp and os.path.splitext(filename)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']:
  File "/home/mark/.virtualenvs/barcode/lib/python2.7/posixpath.py", line 95, in splitext
    return genericpath._splitext(p, sep, altsep, extsep)
  File "/home/mark/.virtualenvs/barcode/lib/python2.7/genericpath.py", line 91, in _splitext
    sepIndex = p.rfind(sep)
  File "/home/mark/.virtualenvs/barcode/local/lib/python2.7/site-packages/PIL/Image.py", line 512, in __getattr__
    raise AttributeError(name)
AttributeError: rfind

Somehow PIL Image doesn’t seem to work either. What should I pass as the first argument to Reportlab’s Image function if I don’t have the filename of the image (because my images are created in memory)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:27:00+00:00Added an answer on June 16, 2026 at 6:27 pm

    The repetitive declaration “Formats supported by PIL/Java 1.4 (the Python/Java Imaging Library) are supported” simply means that data formats supported by PIL are supported by reportlab (since it uses PIL to read them).

    Now, from peeking in reportlab.platypus.flowables.Image code it is possible to see that it accepts either a filename or a file object as input. The former is not what you want, so let us focus on the later. You said StringIO didn’t seem to work, but it does if you take some care. You probably did something wrong with it, here are two correct ways to use StringIO:

    import sys
    import PIL
    from cStringIO import StringIO
    from reportlab.platypus.flowables import Image
    
    # Method 1
    data = open(sys.argv[1]).read()
    img1 = StringIO(data)
    
    # Method 2
    img2 = StringIO()
    PIL.Image.open(sys.argv[2]).save(img2, 'PNG')
    img2.seek(0)
    
    # Method 3 (fails)
    img3 = StringIO(PIL.Image.open(sys.argv[2]).tostring())
    
    story = [Image(img1), Image(img2)]
    #Image(img3)
    

    The method 3 fails because img3 now holds the raw data of the image, so it has no idea about the actual format of this data. There is no reason to attempt to use this method for such task.

    If you have raw data and you know the image mode of your data (‘L’, ‘RGB’, etc) and also its width, height, then you can use a fourth (correct) method based on PIL.Image.fromstring(...).save(mystrio, 'someformat').

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.