Load JPEG from URL to skimage without temporary file
Asked Answered
W

3

7

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?

Walczak answered 27/8, 2015 at 8:41 Comment(0)
B
14

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.

Barathea answered 27/8, 2015 at 9:1 Comment(4)
Well, that is exactly the thing I'm trying to avoid, that's why I attached link to github which shows the code for handling URLs inside skimage with creating temp file.Walczak
But why are you trying to avoid that ?Barathea
@user1263702: I edit my answer and added a solution that should fit your needs.Barathea
@Cybril I have some limitations on working machine e.g. I don't have permission for creating files even in /tmp/ . Btw imread_from_blob is exactly the thing I was looking for.Walczak
B
8
import matplotlib.pyplot  as plt
from skimage import io

image=io.imread ('https://i.sstatic.net/yt0Xo.jpg')

plt.imshow(image)
plt.show()
Brede answered 16/11, 2019 at 19:21 Comment(3)
Hey @Mahmoud, welcome to SO! Normally we'd expect to see some explanation of the problem along with the code. Besides, I just tried it out and got HttpError: Forbidden.Trapan
Hey @AleksanderLidtke .Thank you! I think the code is very simple and self-explanatory, Try providing your own image URL.Brede
Added a working URL just to make life easier for users reading.Feces
R
0

A little bit tricky, but works (On Python 3.4). Seems that skimage itself cannot parse images from buffer. But anyway it uses pillow implicitly in any case.

You need to fill BytesIO buffer with downloaded data, then feed it into PIL.Image, and then create skimage.io.Image from it.

from urllib.request import urlopen
from io import BytesIO
from PIL import Image
from skimage import io

url = 'http://www.musicnowsg.com/wp-content/uploads/2013/12/grumpy-jazz-cat.jpg'
response = urlopen(url)
buf = BytesIO(response.read())
im = Image.open(buf)
a = io.Image(im)

io.imshow(a)
io.show()
Regenerate answered 27/8, 2015 at 14:51 Comment(1)
Then you still need to send it through the machinery to get a numpy array out. The PIL plugin can read from a file-like object.Ramonitaramos

© 2022 - 2024 — McMap. All rights reserved.