【问题标题】:emf to jpeg conversion using PIL works in python but not pyinstaller packaged exe使用 PIL 的 emf 到 jpeg 转换适用于 python 但不适用于 pyinstaller 打包的 exe
【发布时间】:2014-08-02 22:09:18
【问题描述】:

我在 EMF 图像格式、python PIL(以及 Pillow)图像库和用于将 Python 打包成 Windows 可执行文件的 Pyinstaller 程序的交集处遇到了一个特殊问题。

我有一个使用 PIL/Pillow 将 EMF 文件转换为 JPEG 的脚本。当我在 python 中运行 python 脚本时,这可以正常工作。但是,当我使用 Pyinstaller.exe -F 将其打包成 EXE 时,它不起作用。

使用 Pillow 版本时,我收到一个简单的错误提示

“无法转换 image1.emf”。

使用 PIL 版本,我收到一条更长的消息:

Traceback(最近一次调用最后一次): 文件“”,第 38 行,在 文件“”,第 27 行,在 convertImageFile 文件“C:\Embibe\Git\content-ingestion\src\build\convertImage\out00-PYZ.pyz\PIL .Image”,第 2126 行,打开 IOError:无法识别图像文件'image1.emf'

有没有其他人遇到过这个问题并找到了可行的解决方案?

下面是血淋淋的细节,如果你需要的话……:-)

操作系统:Windows 7 64 位(但所有软件都是 32 位)

软件:: Python:2.7.5, Pyinstaller:2.1, PIL: 内置 Python, Pillow: 2.4.0

Python 脚本convImg.py:

from __future__ import print_function
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext(infile)
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            Image.open(infile).convert('RGB').save(outfile)
        except IOError:
            print("cannot convert", infile)

运行为:convImg.py image1.emf 正常工作并生成 image1.jpg。

当使用 \python27\scripts\pyinstaller.exe -F convImg.py 打包到 exe 并以 convImg.exe image1 运行时,会出现上面列出的 Pillow 和 PIL 版本的错误。

我在这里找到了一个相关的帖子,Pyinstaller troubles with Pillow,但它的解决方案,即使用 py2app 而不是 pyinstaller 对我来说不是一个选项,因为它适用于 MacOS,我需要 Windows。我考虑为 windows、py2exe 和 cx_freeze 使用类似的替代方案,但它们不会像 pyinstaller 那样创建一个独立的 exe。

谢谢, 阿米特

【问题讨论】:

  • 更新:我在这里发现了另一个可能相关的问题,需要详细了解并尝试一下:stackoverflow.com/questions/10453858/…
  • 更新:py2exe 文档中的另一个可能线索:py2exe.org/index.cgi/py2exeAndPIL
  • 欢迎来到 SO。提出一个奇妙的问题并在询问后进行研究的方式! +1。您的第二条评论中的链接是我刚要建议的 - 这是我在编译的应用程序中使用 PIL 的方法(我使用 py2exe,而不是 pyinstaller,但修复可能相同)。干得好,朋友。
  • 谢谢,g.d.d.c :) 只是在学习我的方式,所以......多么棒的资源!我意识到我可能应该用两个更新来编辑我的帖子,而不是将它们添加为 cmets:P 是的,将尝试第二个选项并在此处更新...

标签: python python-imaging-library pyinstaller .emf


【解决方案1】:

好的,我在http://www.py2exe.org/index.cgi/py2exeAndPIL找到了我自己问题的答案

问题是PIL依赖动态加载很多图片插件,使用pyinstaller或py2exe打包时,找不到这些插件。所以关键是 一种。显式导入代码中的所有插件 湾。将 Image 类状态标记为已初始化 C。明确指定保存命令的目标格式

所以,我将convImag.py 修改为:

from __future__ import print_function
import os, sys
from PIL import Image
from PIL import BmpImagePlugin,GifImagePlugin,Jpeg2KImagePlugin,JpegImagePlugin,PngImagePlugin,TiffImagePlugin,WmfImagePlugin # added this line

Image._initialized=2 # added this line

for infile in sys.argv[1:]:
    f, e = os.path.splitext(infile)
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            Image.open(infile).convert('RGB').save(outfile,"JPEG") # added "JPEG"
        except IOError:
            print("cannot convert", infile)

在此之后,pyinstaller 工具就像一个魅力一样工作,并且打包的 exe 可以正常运行 :-) 感谢 g.d.d.c.确认我的解决方案是正确的!

【讨论】:

    猜你喜欢
    • 2013-11-10
    • 2021-09-20
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2017-06-20
    • 2021-08-08
    • 1970-01-01
    相关资源
    最近更新 更多