Skip to main content
Participating Frequently
February 14, 2018
Answered

Could I use two magic wand selections to straighten an image?

  • February 14, 2018
  • 2 replies
  • 2677 views

This is an odd query, but it relates to one I posted yesterday about trying to recognize the sprockets in a movie film frame.

If I have a vertical strip of sixteen movie film frames, which isn't perfectly straight-- what if I selected the top sprocket hole with the magic wand, and the bottom sprocket hole, and had a script which would take the coordinates of the two to straighten the image?

Thanks to anyone for whom this makes sense. I'm finding the crop-and-straighten command doesn't work.

This topic has been closed for replies.
Correct answer JJMack

Ah-- I've just seen your previous post, JJ. Thank you. I will see if I can understand that and learn from it.


The script you want may work like this one.

// Save the current preferences

var startRulerUnits = app.preferences.rulerUnits;

// Set Photoshop to use pixels

app.preferences.rulerUnits = Units.PIXELS;

Main();

// Return the app preferences

app.preferences.rulerUnits = startRulerUnits;

function Main() {

if (app.activeDocument.colorSamplers.length!=2) {

alert('two color sampler points are required');

return;

}

else {

point1 = [];

point2 = [];

for (var s=0,len=app.activeDocument.colorSamplers.length;s<len;s++) {

var colorSamplerRef = app.activeDocument.colorSamplers;

MagicWand( colorSamplerRef.position[0].value ,colorSamplerRef.position[1].value);

if (s==0) point1=TopLeft();

else point2=TopLeft();

app.activeDocument.selection.deselect();

   }

   Rwidth = point2[0]-point1[0];

   Rheight = point2[1]-point1[1];

   degrees = Math.atan(Rwidth/Rheight) * 180 / Math.PI;

   app.activeDocument.activeLayer.rotate(degrees);

}

}

function TopLeft() {

try{

TL=[];

var SB = app.activeDocument.selection.bounds;

TL[0]=SB[0].value;

TL[1]=SB[1].value;

return TL;

}

catch(e){}

}

function MagicWand(Xpoint,Ypoint) { 

// =======================================================

var idsetd = charIDToTypeID( "setd" );

    var desc121 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref33 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref33.putProperty( idChnl, idfsel );

    desc121.putReference( idnull, ref33 );

    var idT = charIDToTypeID( "T   " );

        var desc122 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc122.putUnitDouble( idHrzn, idPxl, Xpoint );

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc122.putUnitDouble( idVrtc, idPxl, Ypoint );

    var idPnt = charIDToTypeID( "Pnt " );

    desc121.putObject( idT, idPnt, desc122 );

    var idTlrn = charIDToTypeID( "Tlrn" );

    desc121.putInteger( idTlrn, 20 );

    var idAntA = charIDToTypeID( "AntA" );

    desc121.putBoolean( idAntA, true );

executeAction( idsetd, desc121, DialogModes.NO );

}

2 replies

JJMack
Community Expert
Community Expert
February 14, 2018

Please do not keep opening addition threads for the same subject. Sprocket holes may damaged.  But as chuck wrote if you do one sprocket at a time you can get the bounds of each.  Using the top left corner point of each you may be able to set a Photoshop ruler then use straighten. If that is possible I do not think it would matter what the distance between sprocket holes is any two holes could be used and Photoshop would calculate the rotation angle. Its a simple right triangle calculation. Your script could of course also do the math instead of Photoshop. However after the rotation the holes would move in an arc and now have different bounds

JJMack
Participating Frequently
February 15, 2018

OK, this is what I've got so far. With each image file I execute an action where I select the top sprocket hole with the magic wand and then the bottom sixteenth sprocket hole with the same. The script first subtracts the width of the sprocket (178 pixels) and then takes the difference in width to calculate the angle of rotation. Because the images are consistently the same length I don't really need a y coordinate to get good enough numbers to rotate. As well, using the action I can get close enough to the sprocket so that the magic wand takes care of the inexactness. So far this seems to work not badly.

#target photoshop

if (documents.length == 0) {

    alert("nothing opened");

} else {

    // start

    //setup

    var file = app.activeDocument;

    var selec =  file.selection;

    //run

    var bnds = selec.bounds; // get the bounds of current selection

    var // save the particular pixel values

       xLeft = bnds[0],

       yTop = bnds[1],

       xRight = bnds[2],

       yBottom = bnds[3];

   

    var newAngle1 = [ [xRight - 178 ] - xLeft ]; // remove sprocket width

    var newAngle2 = [ [newAngle1 /  200 ] ]; // set amount of rotate

    var Angle = parseFloat(newAngle2);

    //Angle = -1 * Angle;  

    //alert(Angle);  

}

Rotate();

function Rotate() {

var orig_ruler_units = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

// Rotate portrait to landscape orientation

if (activeDocument.height > activeDocument.width) activeDocument.rotateCanvas(Angle);

// Reset units to original settings

app.preferences.rulerUnits = orig_ruler_units;

}

Participating Frequently
February 15, 2018

Hello JJ

Thanks very much for your patience.

I don't know how to obtain two magic wand selections. The code I have so far only draws a rectangular box around the two sprockets at the top and bottom sixteen frames down. What I'm doing in the code in the sample is to take the rightmost x value and deduct the width of the sprocket (178 pixels). By subtracting the difference between the left-x and right-x I get newAngle1. Now, the next part is inexact, but I've just done trial and error (not knowing the triangulation well enough) to figure out that if I take newAngle1 and divide by 200 I get the right angle to rotate with (e.g. 654 pixels out / 200 = 3.27 degrees).

The problem I have just realized is that this script can't tell whether the film is leaning to the left or the right-- it just knows that the film is out of kilter and figures out how much.

I am out of my depth with the color measuring script, and am using the magic wand because it doesn't need to be very exact-- so long as the action clicks somewhere within the sprocket hole the wand will fill the rest.

Cheers.


This is what I'm seeing (greatly reduced in size). If I select the top sprocket and the bottom one, my code just makes a box. The width of the sprocket is taken out-- but the box just knows how wide it is, not which way the film is drifting. On the plus side, the magic wand does a very good job of finding each sprocket and isolating the film frame-- once the strip is finally vertically straightened.

http://keneckert.com/temp/kilter.jpg

Chuck Uebele
Community Expert
Community Expert
February 14, 2018

I suppose you could. You would need to select one sprocket hole, get the bounds of the selection, then deselect it and select the second hole to compare the values, then put that info into a transform routine.