mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-29 11:26:57 +08:00
Novice module can open images from a url
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user