Skip to main content
Inspiring
May 12, 2024
Answered

Make image words rectangle selection automatically

  • May 12, 2024
  • 1 reply
  • 522 views

I have high number of images that are like following sample: 

 

 

Now I want to make rectangle selection each image words automatically like following: 

 

I wrote following script that convert subpath to rectangle selection but If number of subpath is high then this script working very slow! - If you view above video you understand that I clone words to left side multiple times to prevent from high number of subpath! 

 

var activeDocument = app.activeDocument;
var workPathIndex = findWorkPathIndex(activeDocument, "Path 1");

if (workPathIndex >= 0) {
    var workPath = activeDocument.pathItems[workPathIndex];

    var pathInfo = getHighLowPointsInfo(workPath);
    createGuidesFromPoints(pathInfo);

    clearVerticalGuides();

    selectBetweenGuides();
} else {
    alert("Work path 'Path 1' not found.");
}

function findWorkPathIndex(document, workPathName) {
    for (var i = 0, len = document.pathItems.length; i < len; i++) {
        if (document.pathItems[i].name === workPathName) {
            return i;
        }
    }
    return -1;
}

function getHighLowPointsInfo(workPath) {
    var pathInfo = "";

    for (var i = 0, len = workPath.subPathItems.length; i < len; i++) {
        var subPath = workPath.subPathItems[i];
        var points = subPath.pathPoints;
        var pointsCount = points.length;

        pathInfo += "Subpath " + (i + 1) + ":\n";

        if (pointsCount > 0) {
            var highestPoint = points[0].anchor;
            var lowestPoint = points[0].anchor;

            for (var j = 1; j < pointsCount; j++) {
                var point = points[j].anchor;
                var y = point[1];

                if (y > highestPoint[1]) {
                    highestPoint = point;
                }
                if (y < lowestPoint[1]) {
                    lowestPoint = point;
                }
            }

            pathInfo += "   Highest Point: (" + highestPoint[0] + ", " + highestPoint[1] + ")\n";
            pathInfo += "   Lowest Point: (" + lowestPoint[0] + ", " + lowestPoint[1] + ")\n";
        } else {
            pathInfo += "   No points in this subpath.\n";
        }
    }

    return pathInfo;
}

function createGuidesFromPoints(pathInfo) {
    var lines = pathInfo.match(/(?:Highest Point: \((\d+), (\d+)\)|Lowest Point: \((\d+), (\d+)\))/g);
    var guideCoordinates = [];

    for (var i = 0; i < lines.length; i += 2) {
        var match1 = lines[i].match(/\((\d+), (\d+)\)/);
        var match2 = lines[i + 1].match(/\((\d+), (\d+)\)/);
        var x1 = parseInt(match1[1]);
        var y1 = parseInt(match1[2]);
        var x2 = parseInt(match2[1]);
        var y2 = parseInt(match2[2]);

        guideCoordinates.push([x1, y1], [x2, y2]);
    }

    var doc = app.activeDocument;

    // Loop through the guide coordinates and create guides
    for (var i = 0; i < guideCoordinates.length; i++) {
        var coordinate = guideCoordinates[i];
        var x = coordinate[0];
        var y = coordinate[1];
        
        // Create vertical guide
        var verticalGuide = doc.guides.add(Direction.VERTICAL, UnitValue(x, "px"));
        
        // Create horizontal guide
        var horizontalGuide = doc.guides.add(Direction.HORIZONTAL, UnitValue(y, "px"));
    }
}

function clearVerticalGuides() {
    var doc = app.activeDocument;
    var guides = doc.guides;

    for (var i = guides.length - 1; i >= 0; i--) {
        var guide = guides[i];
        
        if (guide.direction === Direction.VERTICAL) {
            guide.remove();
        }
    }
}

function selectBetweenGuides() {
    var doc = app.activeDocument;
    var guides = doc.guides;
    var selectionArray = [];

    for (var i = 0; i < guides.length; i += 2) {
        var startY = guides[i].coordinate.as("px") - 1;
        var endY = guides[i + 1].coordinate.as("px") + 1;

        selectionArray.push(
            [0, startY],
            [doc.width, startY],
            [doc.width, endY],
            [0, endY]
        );
    }

    doc.selection.select(selectionArray);
}

 

 

If anyone have better idea for my problem please provide here - for example need idea for prevent from clone words to left side 

note that I can't expand color range selection because this can merge words selections together - each word should have specific selection! 

This topic has been closed for replies.
Correct answer r-bin
Record_2024_05_12_23_34_50_867.mp4

1 reply

r-binCorrect answer
Legend
May 12, 2024
Record_2024_05_12_23_34_50_867.mp4
Inspiring
May 13, 2024

TNQ - it working good