【问题标题】:Converting .jpg images to .png将 .jpg 图像转换为 .png
【发布时间】:2012-06-01 07:31:45
【问题描述】:

我环顾四周并阅读了文档,但没有找到任何方法或解决方案,所以我在这里问。是否有任何包可用于使用 Python 将 JPG 图像转换为 PNG 图像?

【问题讨论】:

  • 似乎有 python 绑定到 ImageMagick:imagemagick.org/download/python。我没有用过它们,但我用过 ImageMagick,它会做你想做的事。

标签: python python-2.7


【解决方案1】:

您始终可以为此使用Python Image Library (PIL)。可能还有其他包/库,但我以前用它来转换格式。

这适用于 Windows 下的 Python 2.7 (Python Imaging Library 1.1.7 for Python 2.7),我在 2.7.1 和 2.7.2 中使用它

from PIL import Image

im = Image.open('Foto.jpg')
im.save('Foto.png')

请注意,您最初的问题并未提及您使用的 Python 版本或操作系统。当然,这可能会有所作为:)

【讨论】:

  • 嗯,它说需要 Python 2.7,而我有 2.7.3,所以它不会让我安装。我错过了什么吗?
  • @user1417933 如果你是 Windows,你需要一种与 Python 兼容的 PIL(32 或 64)。
  • 当我在 IrfanView 中打开生成的图像时,我收到警告说它是 png 扩展名中的 JPG 图像。结果最初是png吗? 64 位 Windows 机器上的 Python 2.7
  • PIL 已死。现在使用枕头。看这里安装:stackoverflow.com/a/20061019/4561887
  • 但是请注意,这些说明仍然可以正常工作,因为 Pillow 是 PIL 的一个分支,在这种情况下,导入和命令是相同的。
【解决方案2】:

Python 图像库:http://www.pythonware.com/products/pil/

发件人:http://effbot.org/imagingbook/image.htm

import Image
im = Image.open("file.png")
im.save("file.jpg", "JPEG")

保存

im.save(outfile, options...)

im.save(输出文件、格式​​、选项...)

以给定的文件名保存图像。如果省略格式,则 如果可能,格式由文件扩展名确定。这 方法返回无。

关键字选项可用于向 作家。如果作家不承认一个选项,它会默默地 忽略。本手册稍后将介绍可用的选项。

您可以使用文件对象而不是文件名。在这种情况下,您 必须始终指定格式。文件对象必须实现 seek、tell 和 write 方法,并以二进制模式打开。

如果保存失败,由于某种原因,该方法将引发异常 (通常是 IOError 异常)。如果发生这种情况,该方法可能有 创建了文件,并且可能已向其中写入数据。这取决于你的 必要时删除不完整文件的应用程序。

【讨论】:

    【解决方案3】:

    当我搜索单个目录中文件的快速转换器时,我想分享这个简短的 sn-p,它将当前目录中的任何文件转换为 .png 或您指定的任何目标。

    from PIL import Image
    from os import listdir
    from os.path import splitext
    
    target_directory = '.'
    target = '.png'
    
    for file in listdir(target_directory):
        filename, extension = splitext(file)
        try:
            if extension not in ['.py', target]:
                im = Image.open(filename + extension)
                im.save(filename + target)
        except OSError:
            print('Cannot convert %s' % file)
    

    【讨论】:

      【解决方案4】:
      from glob import glob                                                           
      import cv2 
      pngs = glob('./*.png')
      
      for j in pngs:
          img = cv2.imread(j)
          cv2.imwrite(j[:-3] + 'jpg', img)
      

      这个网址:https://gist.github.com/qingswu/1a58c9d66dfc0a6aaac45528bbe01b82

      【讨论】:

        【解决方案5】:
        import cv2
        
        image =cv2.imread("test_image.jpg", 1)
        
        cv2.imwrite("test_image.png", image)
        

        【讨论】:

          【解决方案6】:

          我自己不使用 python,但尝试查看: http://www.pythonware.com/products/pil/

          import Image
          im = Image.open("infile.png")
          im.save("outfile.jpg")
          

          (取自http://mail.python.org/pipermail/python-list/2001-April/700256.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-05-10
            • 1970-01-01
            • 2013-01-08
            • 1970-01-01
            • 1970-01-01
            • 2018-12-05
            • 2023-03-05
            • 2012-01-22
            相关资源
            最近更新 更多