【问题标题】:Convert downloaded image into file object in single line将下载的图像转换为单行文件对象
【发布时间】:2017-06-02 23:28:08
【问题描述】:

这是我正在使用的 DeathByCaptcha 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 中那样在一行中做,那么我将接受多行

【问题讨论】:

标签: python python-2.7 file


【解决方案1】:

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

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

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

【讨论】:

  • 是的,我能理解……我想要“单线”解决方案,而您提供了它。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-02-14
  • 2020-07-26
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 2016-08-24
相关资源
最近更新 更多