Ausgabe
Ich habe dieses Python-Skript erstellt, um Bilder in einer PSD-Datei zu ersetzen, aber ich habe das Gefühl, dass ich denselben Code wiederhole und ihn vereinfachen wollte, da ich mehr Instanzen von “Gruppen” habe, habe ich hier nur 2 eingefügt, um es kürzer zu machen. Hier ist der Screenshot der PS-Datei.
Screenshot der Photoshop-Datei
Vielen Dank für Ihre Zeit.
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
Lösung
Ich habe deine Zweige in eine Vergleichssoftware eingefügt, und sie sind wirklich fast identisch.
Die einzigen Unterschiede sind:
Was leicht in zwei Variablen gespeichert werden kann, nennen wir sie texture_name
und ending_name
. Beide werden eine Zeichenfolge sein.
Hier ist also, wie ich Ihren Code umgestalten würde:
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):
Sehen Sie, wie wir die Variablen in Ihrem großen Codeabschnitt verwendet haben? Aber wir schreiben es nur einmal!
Beantwortet von – Fra93
Antwort geprüft von – Pedro (FixError Volunteer)