• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
4

Make image words rectangle selection automatically

Explorer ,
May 12, 2024 May 12, 2024

Copy link to clipboard

Copied

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

20080 00115.jpg 

 

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

3a4dd3ff-55c6-4135-b5d7-866d0f0efdde.gif 

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! 

TOPICS
Actions and scripting , Windows

Views

92

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , May 12, 2024 May 12, 2024

Votes

Translate

Translate
Adobe
People's Champ ,
May 12, 2024 May 12, 2024

Copy link to clipboard

Copied

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 12, 2024 May 12, 2024

Copy link to clipboard

Copied

LATEST

TNQ - it working good

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines