How to Read Image Data from a Url in Python

How do I read image data from a URL in Python?

The following works for Python 3:

from PIL import Image
import requests

im = Image.open(requests.get(url, stream=True).raw)

References:

  • https://github.com/python-pillow/Pillow/pull/1151
  • https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#280-2015-04-01

Python - how to read an image from a URL?

Image.open() expects filename or file-like object - not file data.

You can write image locally - i.e. as "temp.jpg" - and then open it

from PIL import Image
import urllib.request

URL = 'http://www.w3schools.com/css/trolltunga.jpg'

with urllib.request.urlopen(URL) as url:
with open('temp.jpg', 'wb') as f:
f.write(url.read())

img = Image.open('temp.jpg')

img.show()

Or you can create file-like object in memory using io module

from PIL import Image
import urllib.request
import io

URL = 'http://www.w3schools.com/css/trolltunga.jpg'

with urllib.request.urlopen(URL) as url:
f = io.BytesIO(url.read())

img = Image.open(f)

img.show()

EDIT: 2022

Because urlopen() also gives file-like object so you can even skip io and use directly url (without .read()) in Image.open()

from PIL import Image
import urllib.request

URL = 'http://www.w3schools.com/css/trolltunga.jpg'

with urllib.request.urlopen(URL) as url:
img = Image.open(url)
img.show()

Is there a way to read an image from a url on openCV?

You need to convert link to array and then decode it with opencv.

function

import numpy as np
import urllib
import cv2
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

Merge in your code

import numpy as np
import urllib
import cv2
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
def get_isbn(x):
print(x)

image = url_to_image(x)


print(type(image))
detectedBarcodes = decode(image)
for barcode in detectedBarcodes:
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 5)

# print(barcode.data)
# print(type(barcode.data))
byte_isbn = barcode.data
string_isbn = str(byte_isbn, encoding='utf-8')

return string_isbn

Reading URL image content from HTTPResponse in Python

Modification points:

  • In your URL of https://drive.google.com/file/d/###fileId###/view?usp=sharing, this file is not publicly shared. When you want to use the URL of this file, please publicly share it.
  • https://drive.google.com/file/d/###fileId###/view?usp=sharing of "webViewLink" is not the direct link. In this case, please use "webContentLink" like https://drive.google.com/uc?export=download&id=###fileId###".
  • I think that it is required to modify requests.get(URL).raw of Image.open(requests.get(URL).raw) to BytesIO.

When these points are reflected in your script, it becomes as follows.

Modified script:

Please replace ###fileId### of https://drive.google.com/uc?export=download&id=###fileId### with your file ID.

from PIL import Image
import requests
import io

URL = "https://drive.google.com/uc?export=download&id=###fileId###"
img = Image.open(io.BytesIO(requests.get(URL).content))
print(img.format, img.size, img.mode)
  • I could confirm that when the file of https://drive.google.com/uc?export=download&id=###fileId### is publicly shared, the script works.

Reference:

  • Resource representations of Files

How do you find the filetype of an image in a url with nonobvious filetype in Python

Building on the responses to this question, you could try:

import requests
from PIL import Image # pillow package
from io import BytesIO

url = "your link"

image = Image.open( BytesIO( requests.get( url ).content))
file_type = image.format

This calls for downloading the entire file, though. If you're looking to do this in bulk, you might want to explore the option in the comment above that mentions "magic bytes"...

Edit:
You can also try to get the image type from the headers of the response to your url:

headers = requests.get(url).headers
file_type =headers.get('Content-Type', "nope/nope").split("/")[1]
# Will print 'nope' if 'Content-Type' header isn't found
print(file_type)
# Will print 'gif' or 'jpeg' for your listed urls

Edit 2:
If you're really only concerned with the file type of the link and not the file itself, you could use the head method instead of the get method of the requests module. It's faster:

headers = requests.head(url).headers
file_type =headers.get('Content-Type', "nope/nope").split("/")[1]


Related Topics



Leave a reply



Submit