Search code examples
pythonscikit-learnurllib2scikit-imageimread

Load JPEG from URL to skimage without temporary file


Is it possible to load image in skimage (numpy matrix) format from URL without creating temporary file?

skimage itself uses temporary files: https://github.com/scikit-image/scikit-image/blob/master/skimage/io/util.py#L23

Is there any way to pass urlopen(url).read() to imread.imread() (or any other image reading library) directly?


Solution

  • From imread documentation:

    Image file name, e.g. test.jpg or URL

    So you can directly pass your URL:

    io.imread(url)
    

    Notice that it will still create a temporary file for processing the image...


    Edit:

    The library imread also have a method imread_from_blob which accept a string as input. So you may pass your data directly to this function.

    from imread import imread_from_blob
    img_data = imread_from_blob(data, 'jpg')
    
    >>> img_data
    array([[[ 23, 123, 149],
    [ 22, 120, 147],
    [ 22, 118, 143],
    ...,
    

    The second parameter is the extension typically associated with this blob. If None is given, then detect_format is used to auto-detect.