Hi, Save the following in a .psjs file and File > Scripts > Browse... It should output a selection of the clusters you need. Caveat: it doesn't work on Bitmap, please convert the image to Grayscale first. const photoshop = require("photoshop");
const { core, imaging, app } = photoshop;
async function getDocumentPixelData(documentID) {
const document = app.activeDocument;
const width = document.width;
const height = document.height;
try {
// Get the pixel data of the document
const imageObj = await imaging.getPixels({
documentID: document.id,
colorSpace: "Grayscale",
});
return imageObj.imageData;
} catch (error) {
console.error("Error getting document pixel data:", error);
throw error;
}
}
async function findWhiteSegments(imageData, width, height, minLength) {
try {
const pixelData = await imageData.getData();
const whiteSegments = [];
const components = imageData.components;
// Scan columns
for (let x = 0; x < width; x++) {
let whiteLength = 0;
let startY = -1;
for (let y = 0; y < height; y++) {
const index = (y * width + x) * components;
const gray = pixelData[index];
if (gray === 255) {
if (whiteLength === 0) startY = y;
whiteLength++;
} else {
if (whiteLength >= minLength) {
whiteSegments.push({ x, startY, length: whiteLength });
}
whiteLength = 0;
startY = -1;
}
}
if (whiteLength >= minLength) {
whiteSegments.push({ x, startY, length: whiteLength });
}
}
return whiteSegments;
} catch (error) {
console.error("Error finding white segments:", error);
throw error;
}
}
async function createSelectionFromSegments(segments, width, height) {
try {
const selectionData = new Uint8Array(width * height).fill(0);
segments.forEach((segment) => {
for (let i = 0; i < segment.length; i++) {
selectionData[(segment.startY + i) * width + segment.x] = 255;
}
});
const selectionBuffer = {
typedArray: selectionData,
width,
height,
components: 1,
colorSpace: "Grayscale",
colorProfile: "Gray Gamma 2.2",
};
return selectionBuffer;
} catch (error) {
console.error("Error creating selection from segments:", error);
throw error;
}
}
async function putSelectionFromBuffer(documentID, selectionBuffer) {
try {
console.log("Creating image data from buffer...");
const imageData = await imaging.createImageDataFromBuffer(
selectionBuffer.typedArray,
{
width: selectionBuffer.width,
height: selectionBuffer.height,
components: selectionBuffer.components,
colorSpace: selectionBuffer.colorSpace,
// colorProfile: selectionBuffer.colorProfile,
chunky: true,
}
);
console.log("Putting selection...");
try {
await imaging.putSelection({
imageData,
});
} catch (error) {
console.error("Error putting selection:", error);
throw error;
}
console.log("Selection added successfully.");
} catch (error) {
console.error("Error putting selection from buffer:", error);
throw error;
}
}
async function main() {
try {
await core.executeAsModal(
async () => {
const documentID = app.activeDocument.id;
const imageData = await getDocumentPixelData(documentID);
const width = imageData.width;
const height = imageData.height;
const minLength = 800;
const whiteSegments = await findWhiteSegments(
imageData,
width,
height,
minLength
);
const selectionBuffer = await createSelectionFromSegments(
whiteSegments,
width,
height
);
await putSelectionFromBuffer(documentID, selectionBuffer);
},
{ commandName: "Get Document Pixels" }
);
} catch (error) {
console.error("Error in main function:", error);
}
}
main().catch(console.error); It should give you a good starting point. Best,
... View more