Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
graphic:javascript:photoshop [2023/03/05 02:59] – [Doc operation code] ying | graphic:javascript:photoshop [2023/03/05 03:02] (current) – [Doc operation code] ying | ||
---|---|---|---|
Line 403: | Line 403: | ||
alert(" | alert(" | ||
} | } | ||
+ | </ | ||
+ | * split image vertically equally into N part, and stack into new doc, optionally crop border pixel <code javascript> | ||
+ | // | ||
+ | //# image split by n | ||
+ | // | ||
+ | var doc = app.activeDocument; | ||
+ | |||
+ | // Prompt the user for the number of parts | ||
+ | var n = parseInt(prompt(" | ||
+ | |||
+ | // Calculate the height of each part | ||
+ | var partHeight = Math.floor(doc.height / n); | ||
+ | |||
+ | // Create a new document with the width of the original image and the height of one part | ||
+ | var newDoc = app.documents.add(doc.width, | ||
+ | |||
+ | // Loop through each part of the image | ||
+ | for (var i = 0; i < n; i++) { | ||
+ | // Define the source rectangle for the part | ||
+ | var sourceRect = [ | ||
+ | [0, i * partHeight], | ||
+ | [doc.width, i * partHeight], | ||
+ | [doc.width, (i + 1) * partHeight], | ||
+ | [0, (i + 1) * partHeight] | ||
+ | ]; | ||
+ | |||
+ | // Activate the source document | ||
+ | app.activeDocument = doc; | ||
+ | // Deselect any active selection | ||
+ | doc.selection.deselect(); | ||
+ | // Create a new selection | ||
+ | doc.selection.select(sourceRect); | ||
+ | // Copy the part to the new document | ||
+ | doc.selection.copy(); | ||
+ | // Activate the new document | ||
+ | app.activeDocument = newDoc; | ||
+ | // Paste the part into the new document | ||
+ | newDoc.paste(); | ||
+ | // Move the selection down by the height of one part | ||
+ | app.activeDocument = doc; | ||
+ | doc.selection.translateBoundary(0, | ||
+ | } | ||
+ | // | ||
+ | //# trim border | ||
+ | // | ||
+ | app.activeDocument = newDoc; | ||
+ | newDoc.trim(TrimType.TOPLEFT, | ||
</ | </ | ||
===== Execute code ===== | ===== Execute code ===== |