0

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

4
  • Why do you think .content isn't the binary image data? What is it instead? What are you doing with image_data that's giving you the error message? Don't show us an error message without the code that produced it. Commented Aug 16, 2014 at 16:27
  • @Wooble the error I get I will add to OP now Commented Aug 16, 2014 at 16:29
  • @Wooble are you saying requests.get(image_url).content is the binary data? Commented Aug 16, 2014 at 16:33
  • Have you read this? Commented Aug 16, 2014 at 18:36

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.