简体   繁体   中英

Get binary image data in Python

How do I take an image and turn that into binary image data?

This is what I have tried:

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)

However this does not seem to give me the binary image data, could someone help explain the process of getting the binary image data?

This is what I'm trying to do:

 app = subprocess.Popen("externalApp",
                            in=subprocess.PIPE,
                            out=subprocess.PIPE)
    app.in.write(image_data)
    app.in.close()

which gives me the error:

IOError: [Errno 32] Broken pipe

You do not need to wrap the response data in a BytesIO object just to write it to a pipe. Use the response.content data directly :

class get_binary_data(image_url):
    #Get the image online online_image = http://www.myimg.com/test.jpg
    return requests.get(image_url).content

I used a BytesIO object in my answer to your previous question only because you wanted to load that data into a PIL Image object. It is a requirement for Image.open() that a file-like object with seek support is provided, and BytesIO does just that.

Here, however, you need a (byte)string, so just use the .contents value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM