48

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?

G M
  • 20,759
  • 10
  • 81
  • 84
Gerges
  • 6,269
  • 2
  • 22
  • 44

9 Answers9

62

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)
Shai Léger
  • 845
  • 1
  • 8
  • 16
  • 1
    Note that this is no longer done internally by matplotlib, but delegated to pillow internally ([see the docs](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imread.html)), so you can't avoid the extra dependency. ImageIO does the same internally (unless explicitly told to use a different backend), so - in 2022 - there is almost no difference between this answer, the answer suggesting pillow below, and using ImageIO as suggested in the question. Personally, I prefer ImageIO because it is more flexible than the other options (but I maintain the library, so take that with some salt). – FirefoxMetzger Feb 03 '22 at 08:58
22

You can also use Pillow like this:

from PIL import Image
image = Image.open("image_path.jpg")
image.show()
tsveti_iko
  • 6,834
  • 3
  • 47
  • 39
9
import matplotlib.pyplot as plt
image = plt.imread('images/my_image4.jpg')
plt.imshow(image)

Using 'matplotlib.pyplot.imread' is recommended by warning messages in jupyter.

AndrewPt
  • 685
  • 11
  • 10
  • But it works, because: "If not given, the format is deduced from the filename. If nothing can be deduced, PNG is tried."(https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.imread.html) – AndrewPt Mar 04 '20 at 17:04
5

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')
0x48piraj
  • 395
  • 3
  • 12
4

For the better answer, you can use these lines of code. Here is the example maybe help you :

import cv2

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.

Prof.Plague
  • 649
  • 10
  • 20
  • 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? Thanks – Oscar Dolloway Nov 21 '18 at 00:59
  • @OscarDolloway I hope [**this link**](https://stackoverflow.com/questions/41357641/display-images-one-by-one-using-python-pil-library) help you. – Prof.Plague Nov 22 '18 at 15:35
  • it says `NameError: name 'cv2' is not defined`. is this answer missing an `import`? – John Henckel Jul 16 '21 at 13:23
  • @john-henckel Yes. Sure. You need to install ```OpenCV-Python``` first with this command: ```pip3 install opencv-python``` and then ```import cv2```. For exact answer ```cv2``` is the interface of ```opencv``` so you need to install it first. For reading more this [link](https://stackoverflow.com/questions/10417108/what-is-different-between-all-these-opencv-python-interfaces) is good. – Prof.Plague Jul 17 '21 at 15:29
1

From documentation:

Matplotlib can only read PNGs natively. Further image formats are supported via the optional dependency on Pillow.

So in case of PNG we may use plt.imread(). In other cases it's probably better to use Pillow directly.

irudyak
  • 2,271
  • 25
  • 20
0

you can try to use cv2 like this

import cv2

image= cv2.imread('image page')

cv2.imshow('image', image)

cv2.waitKey(0)

cv2.destroyAllWindows()
Hymns For Disco
  • 7,530
  • 2
  • 17
  • 33
0

I read all answers but I think one of the best method is using openCV library.

import cv2

img = cv2.imread('your_image.png',0)

and for displaying the image, use the following code :

from matplotlib import pyplot as plt
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.show()
PyMatFlow
  • 459
  • 4
  • 8
0

Easy way

from IPython.display import Image

Image(filename ="Covid.jpg" size )

Dhiren Biren
  • 472
  • 4
  • 7