DEV Community

sium_hossain
sium_hossain

Posted on

8 4

Read an image from an Internet URL in Python3 cv2

By this piece of code we can read image from URL in opencv python3.

Make sure you have installed opencv in system.

pip install opencv-python
Enter fullscreen mode Exit fullscreen mode
import cv2
import urllib.request
import numpy as np

req = urllib.request.urlopen('https://variety.com/wp-content/uploads/2021/12/doctor-strange.jpg?w=681&h=383&crop=1&resize=681%2C383')
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr, -1) # 'Load it as it is'

cv2.imshow('random_title', img)
if cv2.waitKey() & 0xff == 27: quit()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Retry later