无法使用PIL在Mac上打开图像

无法使用PIL在Mac上打开图像

问题描述:

因此,我有一个简短的python脚本,该脚本接受表示图像的base64字符串,然后打开该图像的预览.

So I have a short python script that takes in a base64 string that represents an image, and then opens a preview of that image.

这里是我的脚本:

#!/usr/local/bin/python3

from PIL import Image
import sys
import base64

IMAGE_NAME = "temp.png"

def do_some_stuff(args):
    if len(args) != 2:
        return

    with open(IMAGE_NAME, "wb") as image_file:
        image_file.write(base64.decodebytes(args[1].encode('ascii')))

    image = Image.open(IMAGE_NAME)
    image.show()


if __name__ == '__main__':
    do_some_stuff(sys.argv)

似乎可以正常工作,除了有时间显示我因这个错误而受到欢迎的图像时

It seems to work okay, except for when it gets time to show the image I'm greeted with this error:

FSPathMakeRef(/Applications/Preview.app) failed with error -43.

有人知道为什么会发生此错误或如何解决该错误吗?

Does anyone know why this error is happening or how to fix it?

多挖掘一点之后,看来来自FSPathMakeRef的此错误意味着未找到该文件.因此,我查看了我的应用程序文件夹,然后预览就在其中!

After digging a bit more, it seems this error from FSPathMakeRef means that the file was not found. So I looked inside my applications folder, and Preview was right there!

当我单击获取信息时,我注意到它位于/System/Applications/文件夹中,而不位于/Applications中,因此PIL似​​乎指向了错误的位置.

When i clicked get info i noticed it was in the /System/Applications/ folder and not in /Applications, so it seems PIL is referencing the wrong location.

我的猜测是该应用程序已在Catalina中移动,而PIL尚未更新.

My guess is that the application was moved in Catalina and PIL just hasn't been updated yet.

无论如何要修复它,我只是在PIL看起来像这样的地方做了一个符号链接:

Anyway to fix it I just made a symbolic link where PIL was looking like this:

ln -s /System/Applications/Preview.app /Applications/Preview.app

它像一种魅力一样起作用!

and it worked like a charm!