Zito Relova
Zito Relova

Reputation: 1041

How to show images from a url

I am trying to manipulate images using cv2. The images are in url form. When I try to run a cv2.imshow() function, I get the following error:

error: (-215) size.width>0 && size.height>0 in function cv::imshow

Here is some sample code:

im = cv2.imread('https://mp-media.reebonz.com/images/p-9a/reebonz-31-phillip-
lim--ladies-bag-31-phillip-lim-1-9ad505f2-03fa-41c1-97dd-
63c6695f89fa.jpg;mode=pad;bgcolor=fff;404=404.jpg')
cv2.imshow('image', im)

How would I get the images to show? Would I have to download each image locally before manipulating?

Upvotes: 3

Views: 3362

Answers (3)

Micka
Micka

Reputation: 20140

Did you try VideoCapture?

import numpy as np
import cv2

cap = cv2.VideoCapture('URL/To/Your.jpg')

# Read frame (read again if source changes like updated jpegs from a webcam)
ret, frame = cap.read()

cv2.imshow('image', frame)

cv2.waitKey(0)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Google says something similar: http://answers.opencv.org/question/16385/cv2imread-a-url/

Upvotes: 0

Kallz
Kallz

Reputation: 3523

from skimage import io

url_path = "URL/path"    

image = io.imread(url_path)

It will still create a temporary file for processing the image

for detail imread()

using PIL and urllib

from StringIO import StringIO
from PIL import Image
import urllib

Image.open(StringIO(urllib.urlopen(url).read()))

Upvotes: 1

Tim
Tim

Reputation: 255

You can convert the image to an Numpy Array and then read it into OpenCV format. For that you can simply write a Method:

 def url_to_image(url):
        resp = urllib.urlopen(url)
        image = np.asarray(bytearray(resp.read()), dtype="uint8")
        image = cv2.imdecode(image, cv2.IMREAD_COLOR)

        return image

Then in your Main Code you can call the Method and show the image like this:

image = url_to_image(url)
cv2.imshow("Image", image)

Let me know if it worked!

Upvotes: 1

Related Questions