Yodlee:无法在 getMFAResponseForSite 中将图像字节转换为验证码 - Python

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

如您在 post (Java) 中所见:

getMFAResponseForSite - rendering array as a captcha image

和(C#)

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

Yodlee API getMFAResponseForSite 用包含 MFA 表格的 JSON 回答。在 Python 中,我正在尝试以下解决方案但没有结果:

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()

我试图直接转换字节数组,但它抛出错误,因为字节值必须在 0-255 之间

我希望有人知道如何解决这个问题

感谢用户Apporv的帮助,现在回答我的问题:

使用以下 post,我将 Yodlee 字节数组转换为 Python。代码是:

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()

就是这样!

这里有一些额外的步骤,以及未使用的导入。此外,对我来说,返回的 yodlee 图像数据是 windows bmp 数据(不是 jpg)。这是答案的本质:

with open('captcha.bmp', 'wb') as c:
    write(''.join(map(lambda x: chr(x % 256), img_array)))

或者,按照链接 post 中的建议:

with open('captcha.bmp', 'wb') as c:
    write(str(bytearray(map(lambda x: chr(x % 256), img_array))))

这直接适用于 getMFAResponseForSite 提供的 fieldInfo.image 数组。

和c#版本:

var array = new int[] { 66, 77, 110, -60, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0};
var byteList = new List<byte>();

foreach (var item in array)
{
    var value = (byte)(item < 0 ? byte.MaxValue + 1 + item : item);
    byteList.Add(value);
}

File.WriteAllBytes(@"captcha.jpg", byteList.ToArray());