How to load an image from a URL in Python

You can load an image from a URL in Python using the following code. If you want to learn about computer vision, I strongly advise you to read This book.

# Step 1: Import the required modules
import urllib.request
from PIL import Image
import io

# Step 2: Image URL
url = "https://example.com/img.jpg"

# Step 3: Now download the image from the above URL
img_bytes = urllib.request.urlopen(url).read()

# Step 4: Create an Image object from the downloaded bytes
img = Image.open(io.BytesIO(img_bytes))

# Step 5: Display the image
img.show()

# Step 6: Save the image
img.save("url_img.jpg")

My Recommendations: Master Deep Learning with These Specializations

  1. Deep Learning Specialization
  2. TensorFlow Developer Professional Certificate
  3. TensorFlow: Advanced Techniques

Leave a Comment

Your email address will not be published.