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 1063561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:48:02+00:00 2026-05-16T18:48:02+00:00

I want to encode an image into a string using the base64 module. I’ve

  • 0

I want to encode an image into a string using the base64 module. I’ve ran into a problem though. How do I specify the image I want to be encoded? I tried using the directory to the image, but that simply leads to the directory being encoded. I want the actual image file to be encoded.

EDIT

I tried this snippet:

with open("C:\Python26\seriph1.BMP", "rb") as f:
    data12 = f.read()
    UU = data12.encode("base64")
    UUU = base64.b64decode(UU)

    print UUU

    self.image = ImageTk.PhotoImage(Image.open(UUU))

but I get the following error:

Traceback (most recent call last):
  File "<string>", line 245, in run_nodebug
  File "C:\Python26\GUI1.2.9.py", line 473, in <module>
    app = simpleapp_tk(None)
  File "C:\Python26\GUI1.2.9.py", line 14, in __init__
    self.initialize()
  File "C:\Python26\GUI1.2.9.py", line 431, in initialize
    self.image = ImageTk.PhotoImage(Image.open(UUU))
  File "C:\Python26\lib\site-packages\PIL\Image.py", line 1952, in open
    fp = __builtin__.open(fp, "rb")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str

What am I doing wrong?

  • 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-05-16T18:48:02+00:00Added an answer on May 16, 2026 at 6:48 pm

    I’m not sure I understand your question. I assume you are doing something along the lines of:

    import base64
    
    with open("yourfile.ext", "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
    

    You have to open the file first of course, and read its contents – you cannot simply pass the path to the encode function.

    Edit:
    Ok, here is an update after you have edited your original question.

    First of all, remember to use raw strings (prefix the string with ‘r’) when using path delimiters on Windows, to prevent accidentally hitting an escape character. Second, PIL’s Image.open either accepts a filename, or a file-like (that is, the object has to provide read, seek and tell methods).

    That being said, you can use cStringIO to create such an object from a memory buffer:

    import cStringIO
    import PIL.Image
    
    # assume data contains your decoded image
    file_like = cStringIO.StringIO(data)
    
    img = PIL.Image.open(file_like)
    img.show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 523k
  • Answers 523k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, if you want an element to be placed at… May 16, 2026 at 9:45 pm
  • Editorial Team
    Editorial Team added an answer hmmm, you could place UIButton's (with custom backgrounds) next to… May 16, 2026 at 9:45 pm
  • Editorial Team
    Editorial Team added an answer Splitting a string: NSArray *parts = [string componentsSeparatedByString: @"~"]; Assembling… May 16, 2026 at 9:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

How do I read an image into a base64 encoded string by its ImageReader
As the title mentioned, I want to encode a Image Obj into some kind
I have a string that is HTML encoded: '''&lt;img class=&quot;size-medium wp-image-113&quot;\ style=&quot;margin-left: 15px;&quot; title=&quot;su1&quot;\
Greetings, How can I simply encode some binary data into an ASN.1 DER-encoded blob?
I want to encode strings as Python do. Python code is this: def EncodeToUTF(inputstr):
My platform is PHP JOOMLA MYSQL. I want to encode and load uploaded video
I've created an interactive image thing...but it takes a little too long to load.
I'm encoding some video on the iPhone by running the png image data through
So I created images generator (quite simple onewhich generates RGBs) I want to be
I am loading images from a database and want to dynamically resize them according

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.