如何使用py-appscript将PSD导出为PNG?

时间:2011-08-04 14:25:45

标签: photoshop rb-appscript py-appscript

我写了一个脚本,用PSD导出PSD作为PNG与rb-appscript。那很好,花花公子,但我似乎无法在py-appscript中取消它。

这是红宝石代码:

#!/usr/bin/env ruby

require 'rubygems'
require 'optparse'
require 'appscript'

ps = Appscript.app('Adobe Photoshop CS5.app')
finder = Appscript.app("Finder.app")

path = "/Users/nbaker/Desktop/"
ps.activate
ps.open(MacTypes::Alias.path("#{path}guy.psd"))

layerSets = ps.current_document.layer_sets.get

# iterate through all layers and hide them
ps.current_document.layers.get.each do |layer|
    layer.visible.set false 
end 

layerSets.each do |layerSet|
    layerSet.visible.set false
end

# iterate through all layerSets, make them visible, and create a PNG for them
layerSets.each do |layerSet|
    name = layerSet.name.get
    layerSet.visible.set true
    ps.current_document.get.export(:in => "#{path}#{name}.png", :as => :save_for_web, 
                             :with_options => {:web_format => :PNG, :png_eight => false})
    layerSet.visible.set false
end

这是显然不等价的python代码:

from appscript import *
from mactypes import *

# fire up photoshop
ps = app("Adobe Photoshop CS5.app")
ps.activate()

# open the file for editing
path = "/Users/nbaker/Desktop/"
f = Alias(path + "guy.psd")
ps.open(f)

layerSets = ps.current_document.layer_sets()

# iterate through all layers and hide them
for layer in ps.current_document.layers():
    layer.visible.set(False)  

#... and layerSets  
for layerSet in layerSets:  
    layerSet.visible.set(False)

# iterate through all layerSets, make them visible, and create a PNG for them
for layerSet in layerSets:
    name = layerSet.name()
    layerSet.Visible = True
    ps.current_document.get().export(in_=Alias(path + name + ".png"), as_=k.save_for_web,
                 with_options={"web_format":k.PNG, "png_eight":False})

python脚本中唯一不起作用的部分是保存。我在尝试不同的导出选项和内容时遇到了各种各样的错误:

连接无效...发生一般Photoshop错误。此版本的Photoshop可能无法使用此功能...无法将某些数据转换为预期的类型...

我可以只使用ruby脚本,但是我们所有其他脚本都在python中,所以能够在python中解决这个问题会很好。在此先感谢互联网。

1 个答案:

答案 0 :(得分:1)

伯特,

我想我有一个解决方案 - 尽管几个月后。请注意,我是一个Python noob,所以这绝不是优雅的。

当我试用你的剧本时,它也一直在为我崩溃。但是,通过删除导出调用中的“别名”,它似乎没问题:

from appscript import *
from mactypes import *

# fire up photoshop
ps = app("Adobe Photoshop CS5.1.app")
ps.activate()

# open the file for editing
path = "/Users/ian/Desktop/"
f = Alias(path + "Screen Shot 2011-10-13 at 11.48.51 AM.psd")
ps.open(f)

layerSets = ps.current_document.layer_sets()

# no need to iterate
ps.current_document.layers.visible.set(False)
ps.current_document.layer_sets.visible.set(False)

# iterate through all layerSets, make them visible, and create a PNG for them
for l in layerSets:
    name = l.name()
    # print l.name()
    l.visible.set(True)
    # make its layers visible too
    l.layers.visible.set(True)
    # f = open(path + name + ".png", "w").close()

    ps.current_document.get().export(in_=(path + name + ".png"), as_=k.save_for_web,
                 with_options={"web_format":k.PNG, "png_eight":False})

我也注意到,您遍历所有图层以切换其可见性。实际上,您可以立即向他们发出所有命令 - 我的理解是它更快,因为它不必继续发布Apple事件。

我的剧本逻辑不正确(关于打开和关闭图层),但你明白了。

伊恩