Reading images in python

Go To StackoverFlow.com

10

I am trying to read a png image in python. The imread function in scipy is being deprecated and they recommend using imageio library.

However, I am would rather restrict my usage of external libraries to scipy, numpy and matplotlib libraries. Thus, using imageio or scikit image is not a good option for me.

Are there any methods in python or scipy, numpy or matplotlib to read images, which are not being deprecated?

2018-02-11 09:02
by Gerges


12

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)
2018-02-11 09:08
by Shai Lèger
Possible duplicate : https://stackoverflow.com/a/35287898/825338 - 0x48piraj 2018-02-11 09:11


2

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.

2018-09-06 19:48
by Eshagh Moutabi
if i wanted to create a for loop which opens all the images within the folder 1 by 1 how would i do that using the above? Thank - Oscar Dolloway 2018-11-21 00:59
@OscarDolloway I hope this link help you - Eshagh Moutabi 2018-11-22 15:35


2

You can also use Pillow like this:

from PIL import Image
image = Image.open("image_path.jpg")
image.show()
2019-02-15 10:50
by tsveti_iko


1

If you just want to read an image in Python using the specified libraries only, I will go with matplotlib

In matplotlib :

import matplotlib.image
read_img = matplotlib.image.imread('your_image.png')
2018-02-11 09:09
by 0x48piraj
Ads