用 Python 显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35286540/
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
Display an image with Python
提问by FiReTiTi
I tried to use IPython.display with the following code:
我尝试将 IPython.display 与以下代码一起使用:
from IPython.display import display, Image
display(Image(filename='MyImage.png'))
I also tried to use matplotlib with the following code:
我还尝试将 matplotlib 与以下代码一起使用:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
plt.imshow(mpimg.imread('MyImage.png'))
In both cases, nothing is displayed, not even an error message.
在这两种情况下,都不会显示任何内容,甚至不会显示错误消息。
采纳答案by Joe Bathelt
If you are using matplotlib and want to show the image in your interactive notebook, try the following:
如果您正在使用 matplotlib 并希望在交互式笔记本中显示图像,请尝试以下操作:
%pylab inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('your_image.png')
imgplot = plt.imshow(img)
plt.show()
回答by MaxPowers
If you use matplotlib
, you need to show the image using plt.show()
unless you are not in interactive mode.
E.g.:
如果使用matplotlib
,则需要使用显示图像,plt.show()
除非您未处于交互模式。例如:
plt.figure()
plt.imshow(sample_image)
plt.show() # display it
回答by the_unknown_spirit
In a much simpler way, you can do the same using
以更简单的方式,您可以使用
from PIL import Image
image = Image.open('image.jpg')
image.show()
回答by Qhan
Using opencv-python is faster for more operation on image:
使用 opencv-python 可以更快地对图像进行更多操作:
import cv2
import matplotlib.pyplot as plt
im = cv2.imread('image.jpg')
im_resized = cv2.resize(im, (224, 224), interpolation=cv2.INTER_LINEAR)
plt.imshow(cv2.cvtColor(im_resized, cv2.COLOR_BGR2RGB))
plt.show()
回答by Ayush Srivastava
It's simple Use following pseudo code
很简单 使用下面的伪代码
from pylab import imread,subplot,imshow,show
import matplotlib.pyplot as plt
image = imread('...') // choose image location
plt.imshow(image)
plt.show()
// this will show you the image on console.
plt.show()
// 这将在控制台上显示图像。
回答by Punnerud
This worked for me, Inspired by @the_unknown_spirit
这对我有用,灵感来自@the_unknown_spirit
from PIL import Image
image = Image.open('test.png')
image.show()
回答by Harry Moreno
Your first suggestion works for me
你的第一个建议对我有用
from IPython.display import display, Image
display(Image(filename='path/to/image.jpg'))
回答by Rohit Madan
Your code:
您的代码:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
What it should be:
它应该是什么:
plt.imshow(mpimg.imread('MyImage.png'))
File_name = mpimg.imread('FilePath')
plt.imshow(FileName)
plt.show()
you're missing a plt.show()
unless you're in Jupyter notebook, other IDE's do not automatically display plots so you have to use plt.show()
each time you want to display a plot or made a change to an existing plot in follow up code.
plt.show()
除非您在 Jupyter 笔记本中,否则您会错过一个,其他 IDE 不会自动显示绘图,因此您plt.show()
每次要显示绘图或在后续代码中对现有绘图进行更改时都必须使用。
回答by Rajan saha Raju
import IPython.display as display
from PIL import Image
image_path = 'my_image.jpg'
display.display(Image.open(image_path))
回答by Fei Yao
Using Jupyter Notebook, the code can be as simple as the following.
使用 Jupyter Notebook,代码可以像下面这样简单。
%matplotlib inline
from IPython.display import Image
Image('your_image.png')
Sometimes you might would like to display a series of images in a for loop, in which case you might would like to combine display
and Image
to make it work.
有时您可能希望在 for 循环中显示一系列图像,在这种情况下,您可能希望组合display
并Image
使其工作。
%matplotlib inline
from IPython.display import display, Image
for your_image in your_images:
display(Image('your_image'))