Unable to convert captcha object to base64

I am currently working with python3 and my project requires captcha generation. my goal is to generate a captcha and then return it as base64 so that it can be sent in JSON to the client.

However, I am having trouble converting it to base64 string:

    captcha=image.generate(captchatext)
    assert isinstance(captcha, BytesIO)
    captcha=base64.b64encode(captcha)

      

returns an error:

  captcha=base64.b64encode(captcha)
  File "/usr/lib/python3.6/base64.py", line 58, in b64encode
  encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not '_io.BytesIO'

      

I don't quite understand why? can anyone help me understand why it won't convert?

Thanks for any help :)

+3


source to share


1 answer


Convert object BytesIO

to bytes

:

captcha = base64.b64encode(image.generate(captchatext).getvalue())

      



These types are not interchangeable, BytesIO

is a file-like object, but bytes

simply stores an immutable value likestr

+3


source







All Articles