簡體   English   中英

在Python中獲取二進制圖像數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM