Opening an Image from a URL

I’ve determined that I’d be a much happier Nick if I had this image on my local machine. However, for the sake of this tutorial, I’m stubbornly refusing to get it via any means besides Python.

This is easy enough.

First, we’ll reach out to the hosting site with requests

import requests

url = ''

conn = requests.get(url)
<Response [200]>

However, when we print the first few characters of this enormous string, we’ve got the leading b character informing us that this is a byte string.

b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xe2\x024ICC_PROFILE\x00\x01\x01\x00\x00\x02$appl\x04\x00\x00\x00mntrRGB XYZ \x07\xe1\x00\x07\x00\x07\x00\r\x00\x16\x00 acspAPPL\x00\x00\x00\x00APPL\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

For this, we’ll leverage the standard library BytesIO object, which creates a stream of byte-like data that we can feed to the final step…

from io import BytesIO

<_io.BytesIO at 0x1dac1e179e8>

Good old PIL

from PIL import Image


The day is saved.