Reading images in python
With matplotlib you can use (as shown in the matplotlib documentation)
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('image_name.png')
And plot the image if you want
imgplot = plt.imshow(img)
You can also use Pillow like this:
from PIL import Image
image = Image.open("image_path.jpg")
image.show()
For the better answer, you can use these lines of code. Here is the example maybe help you :
image = cv2.imread('/home/pictures/1.jpg')
plt.imshow(image)
plt.show()
In imread()
you can pass the directory .so you can also use str()
and +
to combine dynamic directories and fixed directory like this:
path = '/home/pictures/'
for i in range(2) :
image = cv2.imread(str(path)+'1.jpg')
plt.imshow(image)
plt.show()
Both are the same.