pythonauthenticationcaptchayodlee

Yodlee: Unable to convert image bytes to captcha in getMFAResponseForSite - Python


As you can see in the post (Java):

getMFAResponseForSite - rendering array as a captcha image

and (C#)

Yodlee: Unable to convert image codes into captcha in getMFAResponseForSite(Captcha type) - C#

Yodlee API getMFAResponseForSite answers with a JSON containing the MFA form. In Python I am trying the following solution with no result:

import array
import base64

img_array = [66, 77, -98, -19, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40,...]
new_img_array = []

for x in img_array:
    new_img_array.append(abs(x))

img_byte_array = bytearray(new_img_array)
fh = open("path.jpg", "wb")
fh.write(img_byte_array)
fh.close()

I tryed to cast the bytes array directly, but it throws an error because byte values must be between 0-255

I hope someone knows how to solve this


Solution

  • Thanks to user Apporv for his help, now I am answering my question:

    Using the following post, I converted the Yodlee byte array to a Python one. Code is:

    import array
    import base64
    
    img_array = [66, 77, -98, -19, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40,...]    
    
    bin_data = ''.join(map(lambda x: chr(x % 256), img_array))
    new_img_array = []
    
    for x in bin_data:
        new_img_array.append(x)
    
    img_byte_array = bytearray(new_img_array)
    fh = open("path.jpg", "wb")
    fh.write(img_byte_array)
    fh.close()
    

    That's it!