Python PSD 图层?

发布于 2024-11-24 22:31:57 字数 145 浏览 4 评论 0原文

我需要编写一个Python程序来加载PSD Photoshop图像,该图像具有多层并吐出png文件(每层一个)。 你能用 Python 做到这一点吗?我尝试过 PIL,但似乎没有任何访问图层的方法。帮助。 附言。事实证明,编写我自己的 PSD 加载器和 png 编写器太慢了。

I need to write a Python program for loading a PSD photoshop image, which has multiple layers and spit out png files (one for each layer).
Can you do that in Python? I've tried PIL, but there doesn't seem to be any method for accessing layers. Help.
PS. Writing my own PSD loader and png writer has shown to be way too slow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

欢你一世 2024-12-01 22:31:58

在 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")

Use psd_tools in Python

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")
白衬杉格子梦 2024-12-01 22:31:58

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

  1. 加载 PSD 文件
  2. 收集所有图层并使所有图层 VISIBLE=OFF
  3. 将一层接着一层,将它们标记为 VISIBLE=ON 并导出为 PNG
    import win32com.client
    pApp = win32com.client.Dispatch('Photoshop.Application')

    def makeAllLayerInvisible(lyrs):
        for ly in lyrs:
            ly.Visible = False

    def makeEachLayerVisibleAndExportToPNG(lyrs):
        for ly in lyrs:
            ly.Visible = True
            options = win32com.client.Dispatch('Photoshop.PNGSaveOptions')
            options.Interlaced = False
            tf = 'PNG file name with path'
            doc.SaveAs(SaveIn=tf,Options=options)
            ly.Visible = False

    #pApp.Open(PSD file)
    doc = pApp.ActiveDocument
    makeAllLayerInvisible(doc.Layers)
    makeEachLayerVisibleAndExportToPNG(doc.Layers)

You can use the win32com for accessing the Photoshop with Python.
Possible pseudo code for your work:

  1. Load the PSD file
  2. Collect all layers and make all layers VISIBLE=OFF
  3. Turn one layer after another, mark them VISIBLE=ON and export to PNG
    import win32com.client
    pApp = win32com.client.Dispatch('Photoshop.Application')

    def makeAllLayerInvisible(lyrs):
        for ly in lyrs:
            ly.Visible = False

    def makeEachLayerVisibleAndExportToPNG(lyrs):
        for ly in lyrs:
            ly.Visible = True
            options = win32com.client.Dispatch('Photoshop.PNGSaveOptions')
            options.Interlaced = False
            tf = 'PNG file name with path'
            doc.SaveAs(SaveIn=tf,Options=options)
            ly.Visible = False

    #pApp.Open(PSD file)
    doc = pApp.ActiveDocument
    makeAllLayerInvisible(doc.Layers)
    makeEachLayerVisibleAndExportToPNG(doc.Layers)

轻拂→两袖风尘 2024-12-01 22:31:58

使用 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

Using the win32com plugin for python (available here: http://python.net/crew/mhammond/win32/) You can access photoshop and easily go through your layers and export them.

Here is a code sample that works on the layers within the currently active Photoshop document, and exports them into the folder defined in '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
时光磨忆 2024-12-01 22:31:58

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

There are also https://code.google.com/p/pypsd/ and https://github.com/kmike/psd-tools Python packages for reading PSD files.

紫南 2024-12-01 22:31:58

https://github.com/antipalindrome/Photoshop-Export-Layers- to-Files-Fast

这不是用 python 编写的,但这里建议的解决方案对我来说比上面的脚本更具挑战性。如果您想要的功能不仅仅是将图层导出到单个文件,那么您最好使用此处的其他选项。但是,如果您只关心将图层导出到文件,则此脚本可以轻松快速地运行。

https://github.com/antipalindrome/Photoshop-Export-Layers-to-Files-Fast

This is not in python, but the suggested solutions here proved more challenging for me than the above script. If you want the capacity to do more than just export layers to individual files, you're probably better off using the other options here. But if you only care about exporting layers to files, this script works easily and very quickly.

提赋 2024-12-01 22:31:57

使用 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 的东西。

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

You don't need Photoshop that way, and it should work on any platform that runs Gimp and Python. It's a large dependency, but a free one.

For doing it in PIL:

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

Edit: OK, found the solution: https://github.com/jerem/psdparse

This will allow you to extract layers from a psd file with python without any non-python stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文