Image.show()不会显示图片

use*_*116 34 python python-imaging-library

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

我在这个程序的目录中有一个apple.jpg图像,它应该显示图片,但事实并非如此.这是代码:

#!/usr/bin/env python

from PIL import Image

Apple = Image.open("apple.jpg")
Apple.show()
Run Code Online (Sandbox Code Playgroud)

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

Len*_*bro 45

它适用于Ubuntu.它使用Imagemagick显示图像.试试这个:

sudo apt-get install imagemagick
Run Code Online (Sandbox Code Playgroud)


Cle*_*aff 12

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

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

打开查看器的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)
Run Code Online (Sandbox Code Playgroud)

复制该块并将其粘贴到下面,将"display"命令更改为Ubuntu的"eog"命令:

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

if which("eog"):
    register(DisplayViewer)
Run Code Online (Sandbox Code Playgroud)

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

  • 这非常有效!那么为什么它首先被设置为"显示"?对文件进行了这一更改之后,其他一些东西可能以后可能无效,因为它期待"显示"而不是"eog"? (2认同)

NoJ*_*hua 5

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

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

sudo gedit /usr/lib/python2.7/dist-packages/PIL/ImageShow.py
Run Code Online (Sandbox Code Playgroud)

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

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