[英]Get binary image data in Python
如何拍攝圖像並將其轉換為二進制圖像數據?
這是我嘗試過的:
class get_binary_data(image_url):
#Get the image online online_image = http://www.myimg.com/test.jpg
online_image = requests.get(image_url).content
image_data = BytesIO(online_image)
但是,這似乎沒有給我二進制圖像數據,有人可以幫忙解釋一下獲取二進制圖像數據的過程嗎?
這就是我想要做的:
app = subprocess.Popen("externalApp",
in=subprocess.PIPE,
out=subprocess.PIPE)
app.in.write(image_data)
app.in.close()
這給了我錯誤:
IOError:[Errno 32]管道損壞
您無需僅將響應數據包裝到BytesIO
對象中就可以將其寫入管道中。 直接使用response.content
數據:
class get_binary_data(image_url):
#Get the image online online_image = http://www.myimg.com/test.jpg
return requests.get(image_url).content
我在回答上一個問題時使用了BytesIO
對象,僅是因為您想將該數據加載到PIL Image
對象中。 對於Image.open()
,要求提供具有查找支持的類似文件的對象,而BytesIO
正是這樣做的。
但是,這里需要一個(字節)字符串,因此只需使用.contents
值即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.