[英]Python - Pillow ImageShow.show() isn't displaying images (Raspbian)
All I'm trying to do is display an image using Pillow on Raspberry Pi 3我要做的就是在 Raspberry Pi 3 上使用 Pillow 显示图像
First tried using this code:首先尝试使用此代码:
from PIL import Image
from PIL import ImageShow
imageA = Image.open('Moth.png')
ImageShow.show(imageA)
print("Done")
It didn't work, no error code, no nothing, it just skipped the code entirely它不起作用,没有错误代码,什么都没有,它只是完全跳过了代码
I digged a bit around and found out that maybe there could be an issue with the image displayer, so I tried adding it in like this:我仔细研究了一下,发现图像显示器可能有问题,所以我尝试像这样添加它:
from PIL import Image
from PIL import ImageShow
imageA = Image.open('Moth.png')
ImageShow.show(imageA,title=None,command='GPicView')
print("Done")
It didn't work either, I also tried installing fim and using它也没有用,我也尝试安装 fim 并使用
ImageShow.show(imageA,title=None,command='fim')
like one of the answers on this site suggested, but that didn't work either就像这个网站上的一个答案所建议的那样,但这也没有用
I made sure that fim has been installed correctly, so there must be something wrong with the code, but I can't understand what, maybe I didn't import ImageShow the right way?我确定fim已经正确安装了,所以代码肯定有问题,但是我不明白是什么,也许我没有以正确的方式导入ImageShow?
I also tried using我也试过使用
imageA.show(command='fim')
But it yields the same results但它产生相同的结果
I'm new to coding with Python (and in general), so maybe I'm just doing something stupid without realizing it我是 Python 编码的新手(一般而言),所以也许我只是在做一些愚蠢的事情而没有意识到
Updated Answer更新的答案
I think newer versions of PIL/Pillow use the xdg-open
command to display images.我认为较新版本的 PIL/Pillow 使用xdg-open
命令来显示图像。 Internally, PIL/Pillow saves your in-memory image as a PNG file on disk and calls the OS's viewer to view that on-disk PNG.在内部,PIL/Pillow 将内存中的图像保存为磁盘上的 PNG 文件,并调用操作系统的查看器来查看磁盘上的 PNG。 So, I presume there must be a way to set the default viewer for MIME-type "image/png" to be the viewer of your choice, but as a Mac user, I am unsure how you would do that - I guess it is possible with the xdg-mime
command.所以,我认为必须有一种方法可以将 MIME 类型“image/png”的默认查看器设置为您选择的查看器,但作为 Mac 用户,我不确定您会怎么做 - 我猜是可以使用xdg-mime
命令。
Original Answer原始答案
I think PIL/Pillow works something like this when displaying on Unix/Linux systems:我认为 PIL/Pillow 在 Unix/Linux 系统上显示时是这样工作的:
display
which is part of ImageMagick它期望并希望找到属于ImageMagick一部分的display
eog
"Eye of Gnome" if it finds it如果找到它,它将使用eog
“侏儒之眼”xv
它会回落到xv
So, there are a number of possibilities depending on your skill-set, patience, disk-space, desire to use a specific viewer.因此,根据您的技能组合、耐心、磁盘空间、使用特定查看器的愿望,有多种可能性。 I don't know those parameters, so here are some possibilities:我不知道这些参数,所以这里有一些可能性:
Option: Install ImageMagick with:选项:使用以下命令安装ImageMagick :
sudo apt install imagemagick
Option: Install eog with:选项:使用以下命令安装eog :
sudo apt install eog
Option: Install xv - I don't have the exact command to hand选项:安装xv - 我手头没有确切的命令
Option: Install feh or some other viewer and symlink it to display
so PIL/Pillow thinks it is using ImageMagick display
选项:安装feh或其他一些查看器并将其符号链接以display
,以便 PIL/Pillow 认为它正在使用ImageMagick display
sudo apt install feh
sudo ln -s /usr/bin/feh /usr/bin/display
Another option might be to create a custom viewer as a derived class from PIL's UnixViewer
that places itself at the top of the list of viewers so it is used first.另一种选择可能是将自定义查看器创建为 PIL 的UnixViewer
的派生类,将其自身置于查看器列表的顶部,以便首先使用它。
So, create a file called "CustomViewer.py"
that looks like this - mine uses the feh
viewer but you can use any application you like:因此,创建一个名为"CustomViewer.py"
的文件,如下所示 - 我使用的是feh
查看器,但您可以使用任何您喜欢的应用程序:
#!/usr/bin/env python3
import shutil
import sys
from PIL import Image, ImageShow
class CustomViewer(ImageShow.UnixViewer):
format = "PNG"
options = {"compress_level": 1}
def get_command_ex(self, file, **options):
command = executable = "feh"
return command, executable
if shutil.which("feh"):
print(f'Registering custom viewer for PIL')
ImageShow.register(CustomViewer, order=-1) # Insert as primary viewer
Then, in your regular Python code where you want to use your custom viewer, just add:然后,在您要使用自定义查看器的常规 Python 代码中,只需添加:
import CustomViewer
and it will output a message saying it is loaded and any subsequent calls to show()
will use your custom viewer.并且它会输出一条消息,说明它已加载,并且对show()
的任何后续调用都将使用您的自定义查看器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.