Python 脚本简化
Python script simplification
我创建了这个 python 脚本来替换 psd 文件中的图像,但我觉得我在重复相同的代码并希望简化它,因为我有更多的实例“groups”,我只是在这里粘贴了 2 以使其更短。这是 ps 文件截图。
photoshop file screenshot
感谢您的宝贵时间。
import win32com.client
#Open Adobe Photoshop App programmatically and locate PSD file
ps = win32com.client.Dispatch("Photoshop.Application")
mainPsd = ps.Open("C:/Users/Admin/Documents/images/edit_tiling_wall.psd")
#group 5 copy
mainGrp = mainPsd.layerSets[0]
#all grps in main group
allLayerSets = mainPsd.layerSets[0].layerSets
#function to resize
def resize(layer,w,h):
w_max = w
h_max = h
bounds = layer.bounds
w = bounds[2] - bounds[0]
h = bounds[3] - bounds[1]
#print(w,h)
diffw = w_max - w
diffh = h_max - h
#print(diffw,diffh)
aumw = diffw + w
aumh = diffh + h
#print(aumw,aumh)
moltw = 100*aumw/w
molth = 100*aumh/h
#print(moltw,molth)
layer.Resize(moltw,molth)
bounds = layer.bounds
w = bounds[2] - bounds[0]
h = bounds[3] - bounds[1]
return w,h
def translate(doc,layer,goalX,goalY):
bounds = layer.bounds
w = bounds[2] - bounds[0]
h = bounds[3] - bounds[1]
posX = doc.width - round(doc.width - (bounds[0] + w / 2))
posY = doc.height - round(doc.height - (bounds[1] + h / 2))
layer.Translate(goalX-posX,goalY-posY)
bounds = layer.bounds
w = bounds[2] - bounds[0]
h = bounds[3] - bounds[1]
posX = doc.width - round(doc.width - (bounds[0] + w / 2))
posY = doc.height - round(doc.height - (bounds[1] + h / 2))
return posX, posY
for grp in allLayerSets:
# filter first type of texture
if grp.name.startswith('Group 1'):
print ( grp.name )
print ( grp.artLayers[-1].name ) #base layer name
# base layer
baseLayer = grp.artLayers[-1]
#get width and height of base layer
boundsLayer = baseLayer.bounds
w = boundsLayer[2] - boundsLayer[0]
h = boundsLayer[3] - boundsLayer[1]
print(w,h)
#get position(center of layer img) of base layer
posX = mainPsd.width - round(mainPsd.width - (boundsLayer[0] + w / 2))
posY = mainPsd.height - round(mainPsd.height - (boundsLayer[1] + h / 2))
print( posX, posY )
# new image to add
newTexture = "C:/CG_CONTENT/TEXTURES/Wall_v2_height.exr"
newHeightMap = ps.Open(newTexture)
newHeightMap_layer = newHeightMap.ArtLayers.Item(1)
newHeightMap_layer.Copy()
newHeightMap.Close(2)
# Set as active image in Photoshop
ps.ActiveDocument = mainPsd
# Paste 'newHeightMap' image from clipboard
pasted = mainPsd.Paste()
currentLayer = mainPsd.activeLayer
# Move the pasted layer to the correct grp
currentLayer.Move(grp, 1)
# resize in pixels the new texture
resize(currentLayer, w, h)
# IS IT FLIPPED HORIZONTALLY OR VERTICALLY? MAYBE ADD A TAG TO FLIPPED LAYERS?
if baseLayer.name.endswith('_x'):
# flip horizontal
currentLayer.Resize(-100,100)
elif baseLayer.name.endswith('_y'):
# flip vertical
currentLayer.Resize(100,-100)
elif baseLayer.name.endswith('_x_n_y'):
# flip horizontal and vertical
currentLayer.Resize(-100,-100)
#translate the texture to position
translate(mainPsd,currentLayer,posX,posY)
baseLayer.visible = False
# filter first type of texture
elif grp.name.startswith('Group 2'):
print ( grp.name )
print ( grp.artLayers[-1].name ) #base layer name
# base layer
baseLayer = grp.artLayers[-1]
#get width and height of base layer
boundsLayer = baseLayer.bounds
w = boundsLayer[2] - boundsLayer[0]
h = boundsLayer[3] - boundsLayer[1]
print(w,h)
#get position(center of layer img) of base layer
posX = mainPsd.width - round(mainPsd.width - (boundsLayer[0] + w / 2))
posY = mainPsd.height - round(mainPsd.height - (boundsLayer[1] + h / 2))
print( posX, posY )
# new image to add
newTexture = "C:/CG_CONTENT/TEXTURES/Wall_v3_height.exr"
newHeightMap = ps.Open(newTexture)
newHeightMap_layer = newHeightMap.ArtLayers.Item(1)
newHeightMap_layer.Copy()
newHeightMap.Close(2)
# Set as active image in Photoshop
ps.ActiveDocument = mainPsd
# Paste 'newHeightMap' image from clipboard
pasted = mainPsd.Paste()
currentLayer = mainPsd.activeLayer
# Move the pasted layer to the correct grp
currentLayer.Move(grp, 1)
# resize in pixels the new texture
resize(currentLayer, w, h)
# IS IT FLIPPED HORIZONTALLY OR VERTICALLY? MAYBE ADD A TAG TO FLIPPED LAYERS?
if baseLayer.name.endswith('_x'):
# flip horizontal
currentLayer.Resize(-100,100)
elif baseLayer.name.endswith('_y'):
# flip vertical
currentLayer.Resize(100,-100)
elif baseLayer.name.endswith('_both'):
# flip horizontal and vertical
currentLayer.Resize(-100,-100)
#translate the texture to position
translate(mainPsd,currentLayer,posX,posY)
baseLayer.visible = False
我把你的分支贴在了一个比较软件里,真的几乎一模一样
唯一的区别是:
可以很容易地存储在两个变量中,我们称它们为texture_name
和ending_name
。两者都是字符串。
因此我将如何重构您的代码:
for grp in allLayerSets:
#[ ... ]
texture_name = ""
ending_name = ""
if grp.name.startswith('Group 1'):
texture_name = "Wall_v2_height.exr"
ending_name = "_x_n_y"
elif grp.name.startswith('Group 2'):
texture_name = "Wall_v3_height.exr"
ending_name = "_both"
else:
print("Deal with this problem")
# Now the code will be just one.
# [ ... ]
newTexture = "C:/CG_CONTENT/TEXTURES/" + texture_name
# [ ... ]
elif baseLayer.name.endswith(ending_name):
看到我们如何在您的大块代码中使用变量了吗?但是我们只写一次!
我创建了这个 python 脚本来替换 psd 文件中的图像,但我觉得我在重复相同的代码并希望简化它,因为我有更多的实例“groups”,我只是在这里粘贴了 2 以使其更短。这是 ps 文件截图。
photoshop file screenshot
感谢您的宝贵时间。
import win32com.client
#Open Adobe Photoshop App programmatically and locate PSD file
ps = win32com.client.Dispatch("Photoshop.Application")
mainPsd = ps.Open("C:/Users/Admin/Documents/images/edit_tiling_wall.psd")
#group 5 copy
mainGrp = mainPsd.layerSets[0]
#all grps in main group
allLayerSets = mainPsd.layerSets[0].layerSets
#function to resize
def resize(layer,w,h):
w_max = w
h_max = h
bounds = layer.bounds
w = bounds[2] - bounds[0]
h = bounds[3] - bounds[1]
#print(w,h)
diffw = w_max - w
diffh = h_max - h
#print(diffw,diffh)
aumw = diffw + w
aumh = diffh + h
#print(aumw,aumh)
moltw = 100*aumw/w
molth = 100*aumh/h
#print(moltw,molth)
layer.Resize(moltw,molth)
bounds = layer.bounds
w = bounds[2] - bounds[0]
h = bounds[3] - bounds[1]
return w,h
def translate(doc,layer,goalX,goalY):
bounds = layer.bounds
w = bounds[2] - bounds[0]
h = bounds[3] - bounds[1]
posX = doc.width - round(doc.width - (bounds[0] + w / 2))
posY = doc.height - round(doc.height - (bounds[1] + h / 2))
layer.Translate(goalX-posX,goalY-posY)
bounds = layer.bounds
w = bounds[2] - bounds[0]
h = bounds[3] - bounds[1]
posX = doc.width - round(doc.width - (bounds[0] + w / 2))
posY = doc.height - round(doc.height - (bounds[1] + h / 2))
return posX, posY
for grp in allLayerSets:
# filter first type of texture
if grp.name.startswith('Group 1'):
print ( grp.name )
print ( grp.artLayers[-1].name ) #base layer name
# base layer
baseLayer = grp.artLayers[-1]
#get width and height of base layer
boundsLayer = baseLayer.bounds
w = boundsLayer[2] - boundsLayer[0]
h = boundsLayer[3] - boundsLayer[1]
print(w,h)
#get position(center of layer img) of base layer
posX = mainPsd.width - round(mainPsd.width - (boundsLayer[0] + w / 2))
posY = mainPsd.height - round(mainPsd.height - (boundsLayer[1] + h / 2))
print( posX, posY )
# new image to add
newTexture = "C:/CG_CONTENT/TEXTURES/Wall_v2_height.exr"
newHeightMap = ps.Open(newTexture)
newHeightMap_layer = newHeightMap.ArtLayers.Item(1)
newHeightMap_layer.Copy()
newHeightMap.Close(2)
# Set as active image in Photoshop
ps.ActiveDocument = mainPsd
# Paste 'newHeightMap' image from clipboard
pasted = mainPsd.Paste()
currentLayer = mainPsd.activeLayer
# Move the pasted layer to the correct grp
currentLayer.Move(grp, 1)
# resize in pixels the new texture
resize(currentLayer, w, h)
# IS IT FLIPPED HORIZONTALLY OR VERTICALLY? MAYBE ADD A TAG TO FLIPPED LAYERS?
if baseLayer.name.endswith('_x'):
# flip horizontal
currentLayer.Resize(-100,100)
elif baseLayer.name.endswith('_y'):
# flip vertical
currentLayer.Resize(100,-100)
elif baseLayer.name.endswith('_x_n_y'):
# flip horizontal and vertical
currentLayer.Resize(-100,-100)
#translate the texture to position
translate(mainPsd,currentLayer,posX,posY)
baseLayer.visible = False
# filter first type of texture
elif grp.name.startswith('Group 2'):
print ( grp.name )
print ( grp.artLayers[-1].name ) #base layer name
# base layer
baseLayer = grp.artLayers[-1]
#get width and height of base layer
boundsLayer = baseLayer.bounds
w = boundsLayer[2] - boundsLayer[0]
h = boundsLayer[3] - boundsLayer[1]
print(w,h)
#get position(center of layer img) of base layer
posX = mainPsd.width - round(mainPsd.width - (boundsLayer[0] + w / 2))
posY = mainPsd.height - round(mainPsd.height - (boundsLayer[1] + h / 2))
print( posX, posY )
# new image to add
newTexture = "C:/CG_CONTENT/TEXTURES/Wall_v3_height.exr"
newHeightMap = ps.Open(newTexture)
newHeightMap_layer = newHeightMap.ArtLayers.Item(1)
newHeightMap_layer.Copy()
newHeightMap.Close(2)
# Set as active image in Photoshop
ps.ActiveDocument = mainPsd
# Paste 'newHeightMap' image from clipboard
pasted = mainPsd.Paste()
currentLayer = mainPsd.activeLayer
# Move the pasted layer to the correct grp
currentLayer.Move(grp, 1)
# resize in pixels the new texture
resize(currentLayer, w, h)
# IS IT FLIPPED HORIZONTALLY OR VERTICALLY? MAYBE ADD A TAG TO FLIPPED LAYERS?
if baseLayer.name.endswith('_x'):
# flip horizontal
currentLayer.Resize(-100,100)
elif baseLayer.name.endswith('_y'):
# flip vertical
currentLayer.Resize(100,-100)
elif baseLayer.name.endswith('_both'):
# flip horizontal and vertical
currentLayer.Resize(-100,-100)
#translate the texture to position
translate(mainPsd,currentLayer,posX,posY)
baseLayer.visible = False
我把你的分支贴在了一个比较软件里,真的几乎一模一样
唯一的区别是:
可以很容易地存储在两个变量中,我们称它们为texture_name
和ending_name
。两者都是字符串。
因此我将如何重构您的代码:
for grp in allLayerSets:
#[ ... ]
texture_name = ""
ending_name = ""
if grp.name.startswith('Group 1'):
texture_name = "Wall_v2_height.exr"
ending_name = "_x_n_y"
elif grp.name.startswith('Group 2'):
texture_name = "Wall_v3_height.exr"
ending_name = "_both"
else:
print("Deal with this problem")
# Now the code will be just one.
# [ ... ]
newTexture = "C:/CG_CONTENT/TEXTURES/" + texture_name
# [ ... ]
elif baseLayer.name.endswith(ending_name):
看到我们如何在您的大块代码中使用变量了吗?但是我们只写一次!