【问题标题】:.emf to .jpg conversion issues.emf 到 .jpg 的转换问题
【发布时间】:2023-01-21 16:53:13
【问题描述】:

我正在使用以下代码将 .emf 文件转换为 .jpg。

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)

它会创建扩展名为 .jpg 的新文件,但该文件似乎是空白的。有什么建议么? 谢谢

我找到了另一篇建议添加以下内容的帖子:

从 PIL 导入 BmpImagePlugin、GifImagePlugin、Jpeg2KImagePlugin、JpegImagePlugin、PngImagePlugin、TiffImagePlugin、WmfImagePlugin

但是该文件似乎仍然是空白的。

【问题讨论】:

  • 您使用的是 Windows 吗?您可以通过 Google 云端硬盘或类似工具共享 EMF 文件吗?
  • 感谢您的回复。我正在使用 Windows。我实际上是通过使用“魔杖”来绕过它的,我相信它使用了 ImageMagick。
  • 酷 - 考虑将您的代码作为答案,以便其他人可以看到您是如何做到的。

标签: python python-imaging-library file-conversion


【解决方案1】:

这就是我解决问题的方法:

from wand.image import Image
import os,sys

infolder = 'C:/Original'
outfolder = 'C:/Converted'

for filename in os.listdir(infolder):
    infilename = os.path.join(infolder, filename)
    outfilename = os.path.join(outfolder, filename.replace('.emf', '.jpg'))
    if not os.path.isfile(infilename): continue
    if not os.path.getsize(infilename) > 999: continue
    oldbase = os.path.splitext(filename)
    with Image(filename =infilename) as img:
        with img.convert('jpg') as converted:
            converted.save(filename =outfilename)

【讨论】:

    猜你喜欢
    • 2012-12-15
    • 2012-03-26
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2013-02-21
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多