import matplotlib.pyplot as plt
import numpy as np
from skimage.io import imread
from skimage.transform import resize
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Claude_Monet%2C_Saint-Georges_majeur_au_cr%C3%A9puscule.jpg/800px-Claude_Monet%2C_Saint-Georges_majeur_au_cr%C3%A9puscule.jpg'
im = imread(url)
plt.imshow(im);
im = resize(im,(512,512))
plt.imshow(im);
This Python code snippet demonstrates how to use the matplotlib
, numpy
, and scikit-image
libraries to download an image from a URL, resize it, and display it using matplotlib.pyplot
.
Here's a step-by-step explanation of the code:
-
Import the necessary libraries:
import matplotlib.pyplot as plt
: This imports thematplotlib
library under the aliasplt
, which is commonly used for creating plots and displaying images.import numpy as np
: This imports thenumpy
library under the aliasnp
, which is used for numerical operations and array manipulations.from skimage.io import imread
: This imports theimread
function from theskimage.io
module, which is part of the scikit-image library and is used to read images.from skimage.transform import resize
: This imports theresize
function from theskimage.transform
module, which is used for resizing images.
-
Define a URL:
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Claude_Monet%2C_Saint-Georges_majeur_au_cr%C3%A9puscule.jpg/800px-Claude_Monet%2C_Saint-Georges_majeur_au_cr%C3%A9puscule.jpg'
This URL points to an image hosted on Wikimedia Commons.
-
Read and display the original image:
im = imread(url) plt.imshow(im)
im = imread(url)
: This line reads the image from the specified URL using theimread
function and stores it in the variableim
.plt.imshow(im)
: This line displays the original image usingplt.imshow
. This is a quick way to visualize the image.
-
Resize and display the resized image:
im = resize(im, (512, 512)) plt.imshow(im)
im = resize(im, (512, 512))
: This line resizes theim
image to have dimensions 512x512 pixels using theresize
function. The result is stored back in theim
variable.plt.imshow(im)
: This line displays the resized image. The second call toplt.imshow
replaces the original image displayed with the resized one, allowing you to visualize the resized image.
In summary, this code fetches an image from a URL, displays the original image, resizes it to 512x512 pixels, and then displays the resized version of the image using matplotlib
. This can be useful for various image processing tasks and visualizations.