【问题标题】:Yodlee: Unable to convert image bytes to captcha in getMFAResponseForSite - PythonYodlee:无法在 getMFAResponseForSite - Python 中将图像字节转换为验证码
【发布时间】:2015-08-06 16:57:02
【问题描述】:

正如您在帖子中看到的(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 之间

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

【问题讨论】:

标签: python authentication captcha yodlee


【解决方案1】:

这里有许多额外的步骤,以及未使用的导入。另外,对我来说,返回的 yodlee 图像数据是 windows bmp 数据(不是 jpg)。这是答案的精髓:

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

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

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

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

【讨论】:

  • 与操作代码的主要区别在于将有符号字节转换为无符号字节:x % 256
【解决方案2】:

感谢用户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()

就是这样!

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2014-08-23
      • 2014-08-14
      • 2014-12-12
      • 2018-11-30
      • 2015-06-13
      • 2019-05-18
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      相关资源
      最近更新 更多