import base64
import io
import mimetypes
import os
import sys
import requests
import tempfile
try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse
from PIL import Image

THUMB_SIZE = (340, 210)


class DataURIConverter(object):
    def __init__(self, location):
        self.location = location
        self.mime, _ = mimetypes.guess_type(location)

    def __call__(self):
        if os.path.exists(self.location):
            with open(self.location, "rb") as fp:
                return self._encode(self.resize_and_convert(fp).read())
        elif self.is_url():
            content = requests.get(self.location).content
            fp = io.BytesIO()
            fp.write(content)
            fp.seek(0)
            return self._encode(self.resize_and_convert(fp).read())
        else:
            raise IOError("{} not found".format(self.location))

    def resize_and_convert(self, fp):
        im = Image.open(fp)
        im.thumbnail(THUMB_SIZE)
        tmp = tempfile.TemporaryFile()
        im.save(tmp, format='png')
        tmp.seek(0)
        return tmp

    def is_py3(self):
        return sys.version_info[0] == 3

    def is_url(self):
        return self.location is not None and urlparse(self.location).scheme != ''

    def _encode(self, content):
        if self.is_py3():
            data64 = base64.b64encode(content).decode("ascii")
        else:
            data64 = content.encode('base64').replace("\n", "")
        return data64


def data_uri_from(location):
    return DataURIConverter(location)()

import base64
import io
import os
import sys
import requests
import tempfile
try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse
from PIL import Image

THUMB_SIZE = (340, 210)


class DataURIConverter(object):
    def __init__(self, location):
        self.location = location

    def __call__(self):
        if os.path.exists(self.location):
            with open(self.location, "rb") as fp:
                return self._encode(self.resize_and_convert(fp).read())
        elif self.is_url():
            content = requests.get(self.location).content
            fp = io.BytesIO()
            fp.write(content)
            fp.seek(0)
            return self._encode(self.resize_and_convert(fp).read())
        else:
            raise IOError("{} not found".format(self.location))

    def resize_and_convert(self, fp):
        im = Image.open(fp)
        im.thumbnail(THUMB_SIZE)
        tmp = tempfile.TemporaryFile()
        im.save(tmp, format='png')
        tmp.seek(0)
        return tmp

    def is_py3(self):
        return sys.version_info[0] == 3

    def is_url(self):
        return self.location is not None and urlparse(self.location).scheme != ''

    def _encode(self, content):
        if self.is_py3():
            data64 = base64.b64encode(content).decode("ascii")
        else:
            data64 = content.encode('base64').replace("\n", "")
        return data64


def data_uri_from(location):
    return DataURIConverter(location)()

Remove mimetype recognition