Search code examples
pythonhttphttpsurllibhttp-status-code-403

How to read image from URL (urllib.error.HTTPError: HTTP Error 403: Forbidden)


I have a code that returns an image (API) :

url1 = "https://api.twilio.com/2010-04-01/Accounts/AC9567862a70d7b000488e8ba80bc19787/Messages/MMebd9e089637c2c740608199af762fc19/Media/ME49a13b9e0732d78b84fe81d6c3c5a3f2"

Manually open the image on broswer automatically converts the https address to below:

url2 = "https://s3-external-1.amazonaws.com/media.twiliocdn.com/AC9567862a70d7b000488e8ba80bc19787/f28ad1174d0871048be68207a88d5cea"

from skimage import io
img = io.imread(url1) # < ---- error here
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
text = pytesseract.image_to_string(img)

I'm trying to read the image from the url1 and perform OCR on the image. (That is to read the image directly from (url1) without the need to open it on broswer and use the alternate address)

However, io.imread(url1) gives the error "urllib.error.HTTPError: HTTP Error 403: Forbidden".

io.imread(url2) does not return any error.

Is there any way to read the image directly from url1 ?


Solution

  • You can use this code in the second line as skimage.io.imread() takes URL as a string input.

    img = io.imread(String(URL))
    

    Also you can use cv2 module instead.

    # importing OpenCV(cv2) module
    import cv2
    
    img = cv2.imread(URL)
    
    # Output img with window name as 'image'
    cv2.imshow('image', img)