Python 2.7 将图像转换为二进制数据
我想将灰度图像转换成一个二进制数据字符串。 我成功地做到了这一点,但结果定义为none而不是string,这意味着我无法检查长度。有什么建议可以解决这个问题,或者有不同的代码来解决这个问题?谢谢 这是我写的代码:Python 2.7 将图像转换为二进制数据,python-2.7,image-processing,Python 2.7,Image Processing,我想将灰度图像转换成一个二进制数据字符串。 我成功地做到了这一点,但结果定义为none而不是string,这意味着我无法检查长度。有什么建议可以解决这个问题,或者有不同的代码来解决这个问题?谢谢 这是我写的代码: def pass_image(image_url): str = base64.b64encode(requests.get(image_url).content) print(str) print "".join(format(ord(x), "b") f
def pass_image(image_url):
str = base64.b64encode(requests.get(image_url).content)
print(str)
print "".join(format(ord(x), "b") for x in decodestring(str))
我认为问题的出现是因为您正在命名变量str。这是python类中用于字符串的名称,因此本质上您正在用变量覆盖字符串类定义,这意味着您不能再使用它及其函数,如len 我可以通过以下更改运行此代码,请尝试一下。我不完全理解你的目标,但希望这有帮助。此外,如果要在其他函数中使用创建的对象,请确保返回该对象
import base64
import requests
my_url = '...' # your url here
def pass_image(image_url):
output = base64.b64encode(requests.get(image_url).content)
bin = "".join(format(ord(x), "b") for x in base64.decodestring(output))
return bin # or you could print it
len(pass_image(my_url)) # for the url I used, I got length of 387244
希望这有帮助!祝你好运