在python中读取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48729915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:50:24  来源:igfitidea点击:

Reading images in python

pythonimagescipy

提问by Gerges

I am trying to read a pngimage in python. The imreadfunction in scipyis being deprecatedand they recommend using imageiolibrary.

我正在尝试png在 python 中读取图像。该imread函数scipy弃用,他们建议使用imageio图书馆。

However, I am would rather restrict my usage of external libraries to scipy, numpyand matplotliblibraries. Thus, using imageioor scikit imageis not a good option for me.

但是,我宁愿将外部库的使用限制为 scipy,numpymatplotlib库。因此,使用imageioorscikit image对我来说不是一个好选择。

Are there any methods in python or scipy, numpyor matplotlibto read images, which are not being deprecated?

在 python 或scipy,numpymatplotlib读取图像中是否有任何方法没有被弃用?

回答by Shai Léger

With matplotlib you can use (as shown in the matplotlib documentation)

您可以使用 matplotlib(如 matplotlib文档中所示)

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)

回答by tsveti_iko

You can also use Pillowlike this:

您还可以像这样使用Pillow

from PIL import Image
image = Image.open("image_path.jpg")
image.show()

回答by AndrewPt

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.

jupyter 中的警告消息建议使用“matplotlib.pyplot.imread”。

回答by Izak Sensei

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:

imread()您可以传递目录 .so 您还可以使用str()+组合动态目录和固定目录,如下所示:

path = '/home/pictures/'
for i in range(2) :
    image = cv2.imread(str(path)+'1.jpg')
    plt.imshow(image)
    plt.show()

Both are the same.

两者都是一样的。

回答by 0x48piraj

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

如果您只想使用指定的库在 Python 中读取图像,我会选择matplotlib

In matplotlib :

在 matplotlib 中:

import matplotlib.image
read_img = matplotlib.image.imread('your_image.png')

回答by irudyak

From documentation:

文档

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

Matplotlib 只能在本机读取 PNG。通过可选的 Pillow 依赖项支持更多图像格式。

So in case of PNGwe may use plt.imread(). In other cases it's probably better to use Pillowdirectly.

所以在这种情况下,PNG我们可以使用plt.imread(). 在其他情况下,Pillow直接使用可能更好。

回答by siful islam

you can try to use cv2 like this

你可以尝试像这样使用 cv2

import cv2

image= cv2.imread('image page')

cv2.imshow('image', image)

cv2.waitKey(0)

cv2.destroyAllWindows()

回答by PyMatFlow

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

我阅读了所有答案,但我认为最好的方法之一是使用openCV库。

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()