Python Image.show() 不会显示图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16279441/
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-18 22:11:37  来源:igfitidea点击:

Image.show() won't display the picture

pythonpython-imaging-library

提问by user2131116

I'm running a python script but it won't show the picture.

我正在运行一个 python 脚本,但它不会显示图片。

I have an apple.jpg image in a directory with this program and it should show the picture, but it doesn't. Here is the code:

我在这个程序的目录中有一个 apple.jpg 图像,它应该显示图片,但它没有。这是代码:

#!/usr/bin/env python

from PIL import Image

Apple = Image.open("apple.jpg")
Apple.show()

My OS is Ubuntu and this program just gets completed without showing any error.

我的操作系统是 Ubuntu,这个程序刚刚完成,没有显示任何错误。

采纳答案by Lennart Regebro

It works for me on Ubuntu. It displays the image with Imagemagick. Try this:

它在 Ubuntu 上对我有用。它使用 Imagemagick 显示图像。尝试这个:

sudo apt-get install imagemagick

回答by Evo510

It may be that you don't have a default image viewer set. Try opening the image file outside of the program and see if it asks you to select a program or just opens in another program.

可能是您没有设置默认的图像查看器。尝试在程序外打开图像文件,看看它是否要求您选择一个程序或只是在另一个程序中打开。

回答by blz

PIL is pretty much abandonware at this point. You should consider using it's successor pillow, which can be downloaded via easy_install or pip.

在这一点上,PIL 几乎是废弃软件。你应该考虑使用它的后继者pillow,它可以通过 easy_install 或 pip 下载。

On ubuntu, I'd suggest the following:

在 ubuntu 上,我建议如下:

  1. If you don't yet have pip, install it through apt-get: sudo apt-get install python-pip
  2. Install pillow: pip install pillow --user
  1. 如果您还没有 pip,请通过 apt-get 安装它: sudo apt-get install python-pip
  2. 安装枕头: pip install pillow --user

From there, you should be able to install pillow and use it's Imageclass in the same manner.

从那里,您应该能够以Image相同的方式安装枕头并使用它的类。

回答by Clemens Sielaff

I know, it's an old question but here is how I fixed it in Ubuntu, in case somebody has the same problem and does not want to install imagemagick (which does not fix the root cause of the problem anyway).

我知道,这是一个老问题,但这里是我如何在 Ubuntu 中修复它,以防有人遇到同样的问题并且不想安装 imagemagick(无论如何都不能解决问题的根本原因)。

The default viewer on Ubuntu can be started using the command "eog" in the terminal. Pillow, by default, searches only for the commands "xv" and "display", the latter one being provided by imagemagick. Therefore, if you install imagemagick, calling "display" will indeed open up the image. But why not use the viewer that we already have?

Ubuntu 上的默认查看器可以在终端中使用命令“eog”启动。默认情况下,Pillow 仅搜索命令“xv”和“display”,后者由 imagemagick 提供。因此,如果您安装 imagemagick,调用“display”确实会打开图像。但是为什么不使用我们已有的查看器呢?

The Python code to open the viewer can be found in lib/python3.4/site-packages/PIL/ImageShow.py (or the equivalent of your Python installation). Scroll down to below line# 155 and find the code block saying:

打开查看器的 Python 代码可以在 lib/python3.4/site-packages/PIL/ImageShow.py(或 Python 安装的等价物)中找到。向下滚动到第 155 行下方,找到如下代码块:

class DisplayViewer(UnixViewer):
    def get_command_ex(self, file, **options):
        command = executable = "display"
        return command, executable

if which("display"):
    register(DisplayViewer)

Copy that block and paste it right underneath, changing the "display" command to Ubuntu's "eog" command:

复制该块并将其粘贴到正下方,将“display”命令更改为 Ubuntu 的“eog”命令:

class DisplayViewer(UnixViewer):
    def get_command_ex(self, file, **options):
        command = executable = "eog"
        return command, executable

if which("eog"):
    register(DisplayViewer)

After saving ImageShow.py, Pillow should correctly show() images in the default viewer.

保存 ImageShow.py 后,Pillow 应该在默认查看器中正确显示()图像。

回答by Cris C

I hope what I found herecan be helpful to someone, it worked for me. It explains that when the default viewer does not accept command line image files, you can use the webbrowser module to show the image:

我希望我在这里找到的东西可以对某人有所帮助,它对我有用。它解释了当默认查看器不接受命令行图像文件时,您可以使用 webbrowser 模块来显示图像:

import Image

# Apple = Image.open("apple.jpg")    
# Apple.show()
# instead of using .open and .show(), 
# save the image to show it with the webbrowser module

filename = "apple.jpg"
import webbrowser
webbrowser.open(filename)

回答by NoJoshua

This is an old question, however, it has bugged me for a while. This is my solution:

这是一个老问题,然而,它已经困扰了我一段时间。这是我的解决方案:

I run Python 2.7 on Linux Mint and when calling Image.show()nothing happens. On my system, the default viewer is "Image Viewer", which is called "eog" in bash. My Python thinks that I use something else maybe "xv". So, open a terminal and run

我在 Linux Mint 上运行 Python 2.7,调用时Image.show()什么也没发生。在我的系统上,默认查看器是“Image Viewer”,在 bash 中称为“eog”。我的 Python 认为我使用了其他可能是“xv”的东西。所以,打开一个终端并运行

sudo gedit /usr/lib/python2.7/dist-packages/PIL/ImageShow.py

Search for "xv" and replace it with "eog".

搜索“xv”并将其替换为“eog”。

Save the file and Image.show()works fine (for debugging purposes).

保存文件并Image.show()正常工作(用于调试目的)。

回答by TfromNYC

What worked for me was:

对我有用的是:

brew install imagemagick

回答by jdhao

In my case, I am using Ubuntu on Windowsand can not display image when using Pillow.

就我而言,我在 Windows 上使用Ubuntu,并且在使用 Pillow 时无法显示图像。

There are two things we need to do:

我们需要做两件事:

  1. Install a X server on your Windows, for example, xming
  2. Install an image viewer in Ubuntu on Windows(not on Windows, this is important!), for example, imagemagick, as suggested by @Lennart Regebro.
  1. 在 Windows 上安装 X 服务器,例如xming
  2. 在 Windows上的Ubuntu 中安装图像查看器(不是在 Windows 上,这很重要!),例如imagemagick,如@Lennart Regebro 所建议的那样。

Then add the following setting to your .bashrcand source it:

然后将以下设置添加到您的.bashrc并获取它:

export DISPLAY=:0

Now, you should be able to see the image displayed successfully.

现在,您应该能够看到成功显示的图像。

回答by graial

The default viewer for Mint19.3 is xviewer (I don't know if that's true for Ubunutu) You can confirm for your system by typing: xviewer /path/to/test_image.jpgthe viewer should launch

Mint19.3 的默认查看器是 xviewer(我不知道这是否适用于 Ubunutu)您可以通过键入以下内容来确认您的系统: xviewer /path/to/test_image.jpg查看器应该启动

while I'm using xviewer as an example, this approach should work for any viewer if you know the command to launch it from the command line

虽然我使用 xviewer 作为示例,但如果您知道从命令行启动它的命令,则这种方法应该适用于任何查看器

by typing which xviewer, I know that xviewer is located at: /usr/bin/xviewer

通过输入which xviewer,我知道 xviewer 位于:/usr/bin/xviewer

  • if you are on this page, I presume displayis currently unallocated, you can confirm that by checking that which diplayreturns nothing. If it returns a path, then you may want to check that to ensure another installed program is not using that command for a different purpose.
  • 如果您在此页面上,我认为display当前未分配,您可以通过检查不which diplay返回任何内容来确认。如果它返回一个路径,那么您可能需要检查以确保另一个已安装的程序没有将该命令用于其他目的。

To assign a new to python (without changing source code) you can then use a symbolic link to assign displayto xviewer:

要将新分配给python(不更改源代码),您可以使用符号链接分配displayxviewer

sudo ln -T /usr/bin/xviewer /usr/bin/display

sudo ln -T /usr/bin/xviewer /usr/bin/display