【问题标题】:Python PSD layers?Python PSD层?
【发布时间】:2011-07-20 09:06:58
【问题描述】:

我需要编写一个 Python 程序来加载 PSD photoshop 图像,该图像具有多个图层并输出 png 文件(每个图层一个)。 你能在 Python 中做到这一点吗?我试过 PIL,但似乎没有任何方法可以访问图层。帮助。 PS。编写我自己的 PSD 加载器和 png 编写器已被证明太慢了。

【问题讨论】:

    标签: python image python-imaging-library layer psd


    【解决方案1】:

    使用 Gimp-Python? http://www.gimp.org/docs/python/index.html

    您不需要 Photoshop,它应该可以在任何运行 Gimp 和 Python 的平台上运行。这是一个很大的依赖,但是是免费的。

    在 PIL 中这样做:

    from PIL import Image, ImageSequence
    im = Image.open("spam.psd")
    layers = [frame.copy() for frame in ImageSequence.Iterator(im)]
    

    编辑:好的,找到了解决方案:https://github.com/jerem/psdparse

    这将允许您使用 python 从 psd 文件中提取图层,而无需任何非 python 内容。

    【讨论】:

    • +1 为psdparse!似乎OP不必推出他/她自己的功能:)
    • 我相信我们已经用尽了所有的选择。你要么自己动手,要么使用 Gimp-Python。
    • 那太糟糕了。我也许可以编写自己的代码,但最终可能会太慢(Python)。有什么建议可以尝试吗?我猜 PIL 使用基于插件的方法,所以我可以尝试一下。有一个带有示例插件的回复,但它被删除了
    • 也许可以尝试与 psdparse 作者取得联系,或者至少以他的模块为基础。没有理由完全从头开始。如果我有帮助,请接受我的回答。
    • 这是个好主意。呃,怎么“接受”回答?我可以投票,但我还不能
    【解决方案2】:

    您可以使用 win32com 通过 Python 访问 Photoshop。 您的工作可能的伪代码:

    1. 加载PSD文件
    2. 收集所有图层并使所有图层 VISIBLE=OFF
    3. 一层一层地转动,将它们标记为 VISIBLE=ON 并导出为 PNG
    导入 win32com.client pApp = win32com.client.Dispatch('Photoshop.Application') def makeAllLayerInvisible(lyrs): lyrs 中的 ly: ly.Visible = 假 def makeEachLayerVisibleAndExportToPNG(lyrs): lyrs 中的 ly: ly.Visible = 真 options = win32com.client.Dispatch('Photoshop.PNGSaveOptions') options.Interlaced = False tf = '带路径的PNG文件名' doc.SaveAs(SaveIn=tf,Options=options) ly.Visible = 假 #pApp.Open(PSD 文件) doc = pApp.ActiveDocument makeAllLayerInvisible(doc.Layers) makeEachLayerVisibleAndExportToPNG(doc.Layers)

    【讨论】:

      【解决方案3】:

      在 Python 中使用 psd_tools

      from psd_tools import PSDImage
      
      psd_name = "your_name"
      x = 0
      psd = PSDImage.open('your_file.psd')
      
      for layer in psd:
          x+=1
          if layer.kind == "smartobject":
              image.conmpose().save(psd_name + str(x) + "png")
      

      【讨论】:

        【解决方案4】:

        使用适用于 python 的 win32com 插件(可在此处获得:http://python.net/crew/mhammond/win32/)您可以访问 Photoshop 并轻松浏览图层并导出它们。

        这是一个代码示例,它适用于当前活动的 Photoshop 文档中的图层,并将它们导出到“save_location”中定义的文件夹中。

        from win32com.client.dynamic import Dispatch
        
        #Save location
        save_location = 'c:\\temp\\'
        
        #call photoshop
        psApp = Dispatch('Photoshop.Application')
        
        options = Dispatch('Photoshop.ExportOptionsSaveForWeb')
        options.Format = 13   # PNG
        options.PNG8 = False  # Sets it to PNG-24 bit
        
        doc = psApp.activeDocument
        
        #Hide the layers so that they don't get in the way when exporting
        for layer in doc.layers:
            layer.Visible = False
        
        #Now go through one at a time and export each layer
        for layer in doc.layers:
        
            #build the filename
            savefile = save_location + layer.name + '.png'
        
            print 'Exporting', savefile
        
            #Set the current layer to be visible        
            layer.visible = True
        
            #Export the layer
            doc.Export(ExportIn=savefile, ExportAs=2, Options=options)
        
            #Set the layer to be invisible to make way for the next one
            layer.visible = False
        

        【讨论】:

          【解决方案5】:

          还有https://code.google.com/p/pypsd/https://github.com/kmike/psd-tools Python包用于读取PSD文件。

          【讨论】:

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