Search code examples
phpimage-processingapplescriptphotoshopphotoshop-script

Script to export non-transparent extremities of PNG files


This is a bit of a complicated question because I don't really know where to start or what technology to use to arrive at the desired result. It may be possible with Photoshop Extended but a PHP or AppleScript script might also to the trick.

I need a script with the following input, process and output.

Input :

Hundreds of transparent PNG images on which I have varying quadrilateral shapes. All the images will have the same canvas size but the shape and position of the quadrilaterals will vary. Here is an examples of what one of these images with look like.

Quadrilateral image

Processing :

The script would need to loop through these images and detect the approximate extremities of the shapes.

Coordinates

Output :

The output would need to be a text file in which each line would contain the name of the image file and its associated coordinates. For example, a line in this export might look like this.

image-18473003783.png; 27,144; 406,241; 116,297; 468,386

You may have guessed that we want to create image maps for all of these images and wish to automate the process. The data will be inserted into a MySQL database.

Again I am open to suggestions as to the methodology and language used to achieve the desired result.


Solution

  • What you're after is layer bounds. Here's what you need in javascript - you might be able to convert the info here into applescript or tailor it to what you need.

    //pref pixels
    app.preferences.rulerUnits = Units.PIXELS;
    
    // call the source document
    var srcDoc = app.activeDocument;
    var myDocName = srcDoc.name;
    
    // set current width values
    var W = srcDoc.width.value;
    var H = srcDoc.height.value;
    
    var x = srcDoc.activeLayer.bounds[0]
    var y = srcDoc.activeLayer.bounds[1]
    var x1 = srcDoc.activeLayer.bounds[2]
    var y1 = srcDoc.activeLayer.bounds[3]
    
    alert(myDocName + " "+ x + ", " + y + ", " + x1 + ", " + y1)