转换下载的图像为单行中的文件对象

2024-06-16 16:00:33 发布

您现在位置:Python中文网/ 问答频道 /正文

这是引用的死亡验证码API文档我正在使用。你知道吗

dict deathbycaptcha.Client.decode(file imageFile, int timeout)
dict deathbycaptcha.Client.decode(str imageFileName, int timeout)

它可以接受图像文件的路径或文件对象

我有这行代码。你知道吗

client.decode(urllib.urlopen('https://example/image.jpg').read(), 30)

但是它给了我一个错误,因为.read()确实返回了file-like对象。你知道吗

我喜欢一行一行地做。你知道吗

有没有什么方法可以轻松地将read()读取的流转换为类似文件的对象?你知道吗

如果不能像PHP (string) 100那样在一行中实现,那么我将接受多行


Tags: 文件对象文档clientapireadtimeout验证码
1条回答
网友
1楼 · 发布于 2024-06-16 16:00:33

我不确定urllib.urlopen,但是您可以使用urllib2.urlopen方法来获取可以传递给decode函数的file-like对象。它看起来或多或少像:

client.decode(urllib2.urlopen('https://example/image.jpg'), 30)

编辑:我查看了urllib documentation,它看起来urllib.urlopen也返回file-like对象。你知道吗

相关问题 更多 >