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

发布于 2024-11-28 07:21:52 字数 2071 浏览 3 评论 0原文

我编写了一个脚本,使用 rb-appscript 将 PSD 导出为 PNG。这很好,但我似乎无法在 py-appscript 中实现它。

这是 ruby​​ 代码:

#!/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 中实现这一点。提前感谢互联网。

I wrote a script to export PSDs as PNGs with rb-appscript. That's fine and dandy, but I can't seem to pull it off in py-appscript.

Here's the ruby code:

#!/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

And here's the apparently nonequivalent python code:

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})

The only part of the python script that doesn't work is the saving. I've gotten all sorts of errors trying different export options and stuff:

Connection is invalid... General Photoshop error occurred. This functionality may not be available in this version of Photoshop... Can't make some data into the expected type...

I can live with just the ruby script, but all of our other scripts are in python, so it would be nice to be able to pull this off in python. Thanks in advance internet.

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

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

发布评论

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

评论(1

世态炎凉 2024-12-05 07:21:52

伯特,

我想我已经为你找到了一个解决方案——尽管是几个月后的事情。请注意,我是一个 Python 菜鸟,所以这绝不是优雅的。

当我尝试你的脚本时,它也一直崩溃。然而,通过删除导出调用中的“别名”,似乎没问题:

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 事件。

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

伊恩

Bert,

I think I have a solution for you – albeit several months later. Note that I'm a Python noob, so this is by no means elegant.

When I tried out your script, it kept crashing for me too. However by removing the "Alias" in the export call, it seems to be OK:

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})

I noticed, too, that you iterate through all the layers to toggle their visibility. You can actually just issue them all a command at once – my understanding is that it's faster, since it doesn't have to keep issuing Apple Events.

The logic of my script isn't correct (with regards to turning layers on and off), but you get the idea.

Ian

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