Ausgabe
Ich versuche, einen Photoshop-Kontaktabzug mit der MakeContactSheet
in der Photoshop-API enthaltenen Funktion zu erstellen. Der Zugriff erfolgt über ‘win32com.client’.
Meine erste Zeile:
psApp = Dispatch("Photoshop.Application")
Erstellt ein win32com.gen_py.E891EE9A-D0AE-4CB4-8871-F92C0109F18Ex0x1x0._Application._Application
Objekt.
Die Klasse dieses Objekts scheint alle verfügbaren Funktionen zu haben, die in der Dokumentation aufgeführt sind .
Anschließend erstelle ich mit os.walk eine Liste von Strings.
CSInputFiles = [path.join(r, f) for r, sd, fs in walk('C:\\Users\\chris\\Desktop\\1A') for f in fs]
Dann eine gemischte Auswahl an Optionen:
CSoptions = [True, psApp, False, False, 6, True, None, 0, 7, 4, 3, 300, 3, None, True]
Schließlich übergebe ich diese Argumente:
psApp.MakeContactSheet(CSInpuFiles, CSoptions)
Was angesichts der Funktionsdefinition in richtig zu sein scheint _Application
:
def MakeContactSheet(self, InputFiles=defaultNamedNotOptArg, Options=defaultNamedOptArg):
'create a contact sheet from multiple files'
# Result is a Unicode object
return self._oleobj_.InvokeTypes(1129599816, LCID, 1, (8, 0), ((12, 1), (12, 17)),InputFiles
, Options)
Leider bekomme ich folgenden Fehler:
Traceback (most recent call last):
File "C:\Users\chris\Desktop\test.py", line 17, in <module>
psApp.MakeContactSheet(CSInputFiles, CSoptions)
File "C:\Users\chris\AppData\Local\Temp\gen_py\3.9\E891EE9A-D0AE-4CB4-8871-F92C0109F18Ex0x1x0\_Application.py", line 97, in MakeContactSheet
return self._oleobj_.InvokeTypes(1129599816, LCID, 1, (8, 0), ((12, 1), (12, 17)),InputFiles
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Adobe Photoshop', 'Illegal argument - argument 2\n- Object expected', None, 0, -2147220261), None)
Mein anfänglicher Instinkt war, den gesamten String in meinem csInputFiles-Array mit pathlib in Pfadobjekte umzuwandeln.
from pathlib import Path
CSInputFiles = [Path(path.join(r, f)) for r, sd, fs in walk('C:\\Users\\chris\\Desktop\\1A') for f in fs]
Was zu diesem obskuren Stück Müll führte, als ich das Array an die Funktion übergab:
psApp.MakeContactSheet(CSInputFiles, CSoptions)
#RUN!
File "C:\Users\chris\Desktop\test.py", line 17, in <module>
psApp.MakeContactSheet(CSInputFiles, CSoptions)
File "C:\Users\chris\AppData\Local\Temp\gen_py\3.9\E891EE9A-D0AE-4CB4-8871-F92C0109F18Ex0x1x0\_Application.py", line 97, in MakeContactSheet
return self._oleobj_.InvokeTypes(1129599816, LCID, 1, (8, 0), ((12, 1), (12, 17)),InputFiles
TypeError: must be real number, not WindowsPath
Was überhaupt keinen Sinn macht! Wie könnte es eine reelle Zahl erwarten? Dies soll ein Array von Eingabedateien sein!
Lösung
Der in der Frage angegebene Fehler wurde dadurch verursacht, dass ich das Optionsargument zu einer Liste und nicht zu einem Objekt gemacht habe. Informationen zum Aufbau des Objekts finden Sie hier .
Nach einigem Herumfragen auf GitHub habe ich jedoch festgestellt, dass MakeContactSheet
es sich um eine veraltete Funktion handelt, wie auf Seite 19 dieses Dokuments angegeben.
Dies veranlasste mich, mithilfe der Kissenbibliothek eine Art Ersatz zu entwickeln.
Die Lösung besteht darin , einfach ein leeres Formular zu erstellen png
und dann alle Bilder , die Sie auf Ihren Kontaktabzug stellen möchten , darauf einzufügen png
.
Ich werde eine vereinfachte Version dieses Programms einfügen, um das Problem hier zu lösen.
from PIL import Image
from os import walk, path
#rows = amount of rows (int)
#columns = amount of clomns (int)
#sheetWidth = the width of the contact sheet in inch (int)
#sheetHeight = the height of the contact sheet in inch (int)
#aspectRatio = the simplified height to width ratio of the images on the contact sheet (height/width) (float)
#ppi = pixels per inch
#path = the folder containing all the images you wish to make a contact sheet of (string)
def make_contact_sheet(rows, columns, sheetWidth, sheetHeight, aspectRatio, ppi, filePath):
#convert inches to pixels
sheetWidth *= ppi
sheetHeight *= ppi
#Calculate the size that images will be on the sheet
imageHeight = sheetHeight // columns
imageWidth = int(imageHeight * aspectRatio)
#Calculate how far apart the images will be spaced (I call this padding)
xRemSpace = sheetWidth - (imageWidth * columns)
xPad = xRemSpace // (columns-1)
yRemSpace = sheetHeight - (imageHeight * rows)
yPad = yRemSpace // (rows+1)
#Now make the empty png
sheet = Image.new("RGBA", (sheetWidth, sheetHeight), color=(0,0,0,255))
#Loop through paths of the images and paste the images one by one
x, y = 0, yPad
images = [Image.open(path.join(r,f)) for r,sd,fs in walk(filePath) for f in fs]
for i in range(len(images)):
#Resize the image
resized = images[i].resize((imageWidth, imageHeight))
#Paste the images
if i == 0:
sheet.paste(resized, (x,y))
elif i % columns == 0:
x = 0
y += imageHeight + yPad
sheet.paste(resized, (x,y))
else:
x += imageWidth + xPad
sheet.paste(resized, (x,y))
return sheet
if __name__ == "__main__":
sheet = make_contact_sheet(3, 5, 8, 5, (2/3), 300, r"Some path")
sheet.show()
Wie oben erwähnt, ist dies ein sehr grobes Beispiel und basiert auf einer ausgefeilteren Version des endgültigen Programms, das ich geschrieben habe. Aber ich habe die Grenzen der Relevanz bereits überschritten.
Beantwortet von – Christopher
Antwort geprüft von – Katrina (FixError Volunteer)