在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
Reading images in python
提问by Gerges
I am trying to read a png
image in python. The imread
function in scipy
is being deprecatedand they recommend using imageio
library.
我正在尝试png
在 python 中读取图像。该imread
函数scipy
被弃用,他们建议使用imageio
图书馆。
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.
但是,我宁愿将外部库的使用限制为 scipy
,numpy
和matplotlib
库。因此,使用imageio
orscikit image
对我来说不是一个好选择。
Are there any methods in python or scipy
, numpy
or matplotlib
to read images, which are not being deprecated?
在 python 或scipy
,numpy
或matplotlib
读取图像中是否有任何方法没有被弃用?
回答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
回答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 PNG
we may use plt.imread()
. In other cases it's probably better to use Pillow
directly.
所以在这种情况下,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()