Novice module can open images from a url

This commit is contained in:
Michael Hansen
2013-10-22 13:07:16 -04:00
parent 36cfd6d17c
commit f5c68531ab
+15 -2
View File
@@ -1,6 +1,10 @@
import os
import imghdr
import shutil
from collections import namedtuple
from io import BytesIO
from urlparse import urlparse
from urllib2 import urlopen
import numpy as np
from skimage import io
@@ -19,7 +23,7 @@ colors = namedtuple('colors', color_dict.keys())(**color_dict)
def open(path):
"""Return Picture object from the given image path."""
return Picture(path=os.path.abspath(path))
return Picture(path=path)
def _verify_picture_index(index):
@@ -212,7 +216,16 @@ class Picture(object):
elif path is not None:
self.array = img_as_ubyte(io.imread(path))
self._path = path
self._format = imghdr.what(path)
# Copy to in-memory buffer to get format (in case path was a url)
if urlparse(path).scheme == "":
self._format = imghdr.what(path)
else:
data = BytesIO()
shutil.copyfileobj(urlopen(path), data)
data.seek(0)
self._format = imghdr.what(data)
del data
elif array is not None:
self.array = array
elif xy_array is not None: