Python / PIL: Create and save image from data uri

To expand on the comment from Stephen Emslie, in Python 3 this works and is less code:

data = 'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUAAAhwAAAFoCAYAAAA.......'
response = urllib.request.urlopen(data)
with open('image.jpg', 'wb') as f:
    f.write(response.file.read())

I will assume you have just the base64 part saved in a variable called data. You want to use Python's binascii module.

from binascii import a2b_base64

data = 'MY BASE64-ENCODED STRING'
binary_data = a2b_base64(data)

fd = open('image.png', 'wb')
fd.write(binary_data)
fd.close()

No PIL needed! (thank goodness! :)