我正在运行python脚本,但它不会显示图片。
我在这个程序的目录中有一个apple.jpg图像,它应该显示图片,但事实并非如此。这是代码:
#!/usr/bin/env python
from PIL import Image
Apple = Image.open("apple.jpg")
Apple.show()
我的操作系统是Ubuntu,这个程序刚刚完成而没有显示任何错误。
答案 0 :(得分:42)
它适用于Ubuntu。它使用Imagemagick显示图像。试试这个:
sudo apt-get install imagemagick
答案 1 :(得分:11)
我知道,这是一个老问题,但这是我如何在Ubuntu中修复它,以防有人遇到同样的问题并且不想安装imagemagick(无论如何都无法解决问题的根本原因) )。
可以使用命令" eog"启动Ubuntu上的默认查看器。在终端。默认情况下,Pillow仅搜索命令" xv"和"显示",后者由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)
复制该块并将其粘贴在下面,更改"显示"命令Ubuntu" eog"命令:
class DisplayViewer(UnixViewer):
def get_command_ex(self, file, **options):
command = executable = "eog"
return command, executable
if which("eog"):
register(DisplayViewer)
保存ImageShow.py后,Pillow应该在默认查看器中正确显示()图像。
答案 2 :(得分:4)
这是一个老问题,然而,它已经让我感到困扰了一段时间。这是我的解决方案:
我在Linux Mint上运行Python 2.7,在调用Image.show()
时没有任何反应。在我的系统上,默认查看器是“图像查看器”,在bash中称为“eog”。我的Python认为我使用的东西可能是“xv”。所以,打开一个终端并运行
sudo gedit /usr/lib/python2.7/dist-packages/PIL/ImageShow.py
搜索“xv”并将其替换为“eog”。
保存文件,Image.show()
正常工作(出于调试目的)。
答案 3 :(得分:3)
我希望我发现的here对某人有帮助,对我有用。它解释了当默认查看器不接受命令行图像文件时,您可以使用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)
答案 4 :(得分:1)
对我有用的是:
brew install imagemagick
答案 5 :(得分:0)
可能是您没有默认的图像查看器设置。尝试打开程序外部的图像文件,看看它是否要求您选择程序或只是在另一个程序中打开。
答案 6 :(得分:0)
就我而言,我正在使用Ubuntu on Windows,并且在使用枕头时无法显示图像。
我们需要做两件事:
然后将以下设置添加到您的.bashrc
中并获取它:
export DISPLAY=:0
现在,您应该能够看到成功显示的图像。
答案 7 :(得分:0)
Mint19.3的默认查看器是xviewer(我不知道Ubunutu是否适用)
您可以通过键入以下内容来确认您的系统:
xviewer /path/to/test_image.jpg
观众应该启动
我以xviewer为例,如果您知道从命令行启动该命令的方法,则该方法应适用于任何查看器
通过键入which xviewer
,我知道xviewer位于:/ usr / bin / xviewer
display
当前未分配,则可以通过检查which diplay
是否返回任何内容来确认。如果返回路径,则可能需要检查以确保另一个已安装程序没有出于其他目的使用该命令。要为python分配新内容(不更改源代码),则可以使用符号链接将display
分配给xviewer
:
sudo ln -T /usr/bin/xviewer /usr/bin/display
答案 8 :(得分:-2)
PIL在这一点上几乎是放弃软件。您应该考虑使用它的后继pillow
,可以通过easy_install或pip下载。
在ubuntu上,我建议如下:
sudo apt-get install python-pip
pip install pillow --user
从那里,您应该能够安装枕头,并以同样的方式使用它Image
课程。