19 Replies Latest reply: Oct 4, 2011 8:13 AM by doane2u RSS

    Squaring up layered objects from photos?

    doane2u Community Member

      I frequently work with photos of display sample boards in our store that I take photos of and then edit in CS5 to delete the background, square up the image, match the color and lighting to the original, etc. I use the select tool to go around the board, then cut it and paste it into a layer, I then fill in the base layer with white (ctrl-delete).  I then use the ruler tool to rotate one side of the board in the image so that it is straight/horizontal. I then pull in guide lines to the vertical and horizontal sides and then I use the transform function with the ctrl key and the guides to get the rest of the edges vertical/horizontal and square.  Is there some automated way of getting the layered rectangle to square up on all corners? Hope this is making sense and thanks for any help or ideas.

        • 1. Re: Squaring up layered objects from photos?
          JJMack MVP

          doane2u wrote:

           

          Is there some automated way of getting the layered rectangle to square up on all corners?

          There is a basic problem with the above question.  A layer can have any shape.  All layers bounds are square with respect to the canvas. You can transform a layers boundaries to canvas size however that will not change the layers shape other then distortion. A circle may become oval a cutout person may become fat, thin, tall or short etc.

           

           

          You need to use straiten. Or Rotate arbitrary

          • 2. Re: Squaring up layered objects from photos?
            doane2u Community Member

            So, If I have a shape on a layer, and it is selected, there is no way to automatically square it up vertically and horizontally? I use the ruler with straighten and then I use the rotate and transform with ctrl to distort it so that it lines up with vertical and horizontal guides I placed first, but I was hoping there might be some way of automating this as I have to do this quite often in my work. 

            I have also used the lens distortion tool to correct for horizontal and vertical out of square in combination with the placed grid, but it also is a sort of slow process.

            Thanks,

            • 3. Re: Squaring up layered objects from photos?
              Noel Carboni Community Member

              One way to achieve some accuracy (but without much automation) might be to use the ruler to "measure" what should be a horizontal line, then note the angle.  Now use Edit - Free Transform and type in the angle.

               

              -Noel

              • 4. Re: Squaring up layered objects from photos?
                doane2u Community Member

                Hi, Noel: In CS5 you can use the ruler and make a line along one edge of an object and then click the straighten button to make it horizontal, but that doesn't fix the other three sides that may be off because of the distortions in the photo.

                • 5. Re: Squaring up layered objects from photos?
                  c.pfaffenbichler Community Member

                  If the OP would use the Pen Tool instead of whatever they mean by »select tool« it would be easy to get the fout points’ coordinates.

                  But I’m afraid the math to calculate the necessary perspectival transformation to achieve a square from that is not clear to me, possibly someone over at the Photoshop Scripting Forum has an idea.

                  • 6. Re: Squaring up layered objects from photos?
                    Noel Carboni Community Member

                    doane2u wrote:

                     

                    Hi, Noel: In CS5 you can use the ruler and make a line along one edge of an object and then click the straighten button to make it horizontal, but that doesn't fix the other three sides that may be off because of the distortions in the photo.

                     

                    Straighten turns your entire canvas, while Transform will turn an individual selected element, but am I to understand the more difficult problem you're trying to solve is that you're trying to "undistort" complex shapes?

                     

                    Where are these shapes coming from?

                     

                    Could you use a Perspective Crop on a complex shape in a separate document to get the job done?  Only thing is the Crop Tool set to Perspective tends to change aspect ratios if you move a lot of corners.

                     

                    -Noel

                    • 7. Re: Squaring up layered objects from photos?
                      c.pfaffenbichler Community Member

                      Dang, the Pespectival Crop could be Scripted rather neatly.

                      • 8. Re: Squaring up layered objects from photos?
                        c.pfaffenbichler Community Member

                        doane2u, if you want to give it a try, paste the following text into a new file in ExtendScript Toolkit (part of Photoshop’s installation, /Applications/Utilities/Adobe Utilities-CS5/ExtendScript Toolkit CS5) and save it as a jsx-file into Photoshop’s Presets/Scripts-folder.

                        After restarting Photoshop the Script should be available under File > Scripts and can be assigned a Keyboard Shortcut directly, recorded into an Action, be used in a Configurator-Panel or started from ExtendScript Toolkit directly.

                         

                        Edit: The Script uses the last Path in the Paths Panel as basis for the crop, it needs to have only one subPathItems and that must have four points.

                         

                        // perspectival crop according to the last path’s four points;
                        // 2011, use it at your own risk;
                        #target photoshop;
                        if (app.documents.length > 0 && app.activeDocument.pathItems.length > 0) {
                        var myDocument = app.activeDocument;
                        // set to pixels;
                        var originalRulerUnits = app.preferences.rulerUnits;
                        app.preferences.rulerUnits = Units.POINTS;
                        // get pathpoints;
                        var thePath = myDocument.pathItems[myDocument.pathItems.length - 1];
                        if (thePath.subPathItems.length == 1 && thePath.subPathItems[0].pathPoints.length == 4) {
                        var point1 = thePath.subPathItems[0].pathPoints[0].anchor;
                        var point2 = thePath.subPathItems[0].pathPoints[1].anchor;
                        var point3 = thePath.subPathItems[0].pathPoints[2].anchor;
                        var point4 = thePath.subPathItems[0].pathPoints[3].anchor;
                        // duplicate;
                        var theCopy = myDocument.duplicate("copy", true);
                        // crop;
                        perspectivalCrop (point1, point2, point3, point4);
                        if (theCopy.width > theCopy.height) {var theSize = theCopy.height}
                        else {var theSize = theCopy.width};
                        theCopy.resizeImage(theSize, theSize);
                        theCopy.layers[0].duplicate(myDocument, ElementPlacement.PLACEATBEGINNING);
                        theCopy.close(SaveOptions.DONOTSAVECHANGES);
                        }
                        else {alert ("last path does not fit")};
                        // reset;
                        app.preferences.rulerUnits = originalRulerUnits;
                        }
                        else {alert ("please select a document with a four point path as the last one")};
                        ////// crop perspectivally //////
                        function perspectivalCrop (point1, point2, point3, point4) {
                        // =======================================================
                        var idCrop = charIDToTypeID( "Crop" );
                            var desc3 = new ActionDescriptor();
                            var idT = charIDToTypeID( "T   " );
                                var desc4 = new ActionDescriptor();
                                var idCntr = charIDToTypeID( "Cntr" );
                                    var desc5 = new ActionDescriptor();
                                    var idHrzn = charIDToTypeID( "Hrzn" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc5.putUnitDouble( idHrzn, idRlt, 240.000000 );
                                    var idVrtc = charIDToTypeID( "Vrtc" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc5.putUnitDouble( idVrtc, idRlt, 240.000000 );
                                var idPnt = charIDToTypeID( "Pnt " );
                                desc4.putObject( idCntr, idPnt, desc5 );
                                var idquadCornerzero = stringIDToTypeID( "quadCorner0" );
                                    var desc6 = new ActionDescriptor();
                                    var idHrzn = charIDToTypeID( "Hrzn" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc6.putUnitDouble( idHrzn, idRlt, point1[0] );
                                    var idVrtc = charIDToTypeID( "Vrtc" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc6.putUnitDouble( idVrtc, idRlt, point1[1] );
                                var idOfst = charIDToTypeID( "Ofst" );
                                desc4.putObject( idquadCornerzero, idOfst, desc6 );
                                var idquadCornerone = stringIDToTypeID( "quadCorner1" );
                                    var desc7 = new ActionDescriptor();
                                    var idHrzn = charIDToTypeID( "Hrzn" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc7.putUnitDouble( idHrzn, idRlt, point2[0] );
                                    var idVrtc = charIDToTypeID( "Vrtc" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc7.putUnitDouble( idVrtc, idRlt, point2[1] );
                                var idOfst = charIDToTypeID( "Ofst" );
                                desc4.putObject( idquadCornerone, idOfst, desc7 );
                                var idquadCornertwo = stringIDToTypeID( "quadCorner2" );
                                    var desc8 = new ActionDescriptor();
                                    var idHrzn = charIDToTypeID( "Hrzn" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc8.putUnitDouble( idHrzn, idRlt, point3[0] );
                                    var idVrtc = charIDToTypeID( "Vrtc" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc8.putUnitDouble( idVrtc, idRlt, point3[1] );
                                var idOfst = charIDToTypeID( "Ofst" );
                                desc4.putObject( idquadCornertwo, idOfst, desc8 );
                                var idquadCornerthree = stringIDToTypeID( "quadCorner3" );
                                    var desc9 = new ActionDescriptor();
                                    var idHrzn = charIDToTypeID( "Hrzn" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc9.putUnitDouble( idHrzn, idRlt, point4[0] );
                                    var idVrtc = charIDToTypeID( "Vrtc" );
                                    var idRlt = charIDToTypeID( "#Rlt" );
                                    desc9.putUnitDouble( idVrtc, idRlt, point4[1] );
                                var idOfst = charIDToTypeID( "Ofst" );
                                desc4.putObject( idquadCornerthree, idOfst, desc9 );
                            var idquadrilateral = stringIDToTypeID( "quadrilateral" );
                            desc3.putObject( idT, idquadrilateral, desc4 );
                            var idWdth = charIDToTypeID( "Wdth" );
                            var idPxl = charIDToTypeID( "#Pxl" );
                            desc3.putUnitDouble( idWdth, idPxl, 0.000000 );
                            var idHght = charIDToTypeID( "Hght" );
                            var idPxl = charIDToTypeID( "#Pxl" );
                            desc3.putUnitDouble( idHght, idPxl, 0.000000 );
                            var idRslt = charIDToTypeID( "Rslt" );
                            var idRsl = charIDToTypeID( "#Rsl" );
                            desc3.putUnitDouble( idRslt, idRsl, 0.000000 );
                        executeAction( idCrop, desc3, DialogModes.NO );
                        };
                        
                        • 9. Re: Squaring up layered objects from photos?
                          doane2u Community Member

                          Thanks everyone.. got to go to work but I will give the script a try, hopefully tonight if I can get off early enough.. have never actually used scripts so that will be a new experience for me.

                           

                          Sorry everyone that I haven't been explaining things better.. I'm so used to using short cut keys and key assignments that I have set up for functions I use a lot that I have forgotton what some of them are called. Let me try this again with an example that might make more sense. I do drawings and I do all the advertising for our company (carmelimports.com) Let's say that I have a display panel 24"x36" with decorative tiles glued to it that I want to place on our website and to send to stores that buy from us and maybe to make some tear sheets to be printed  for a advertising folder.

                           

                          I have no fancy equipment, just a hand held digital camera so I use a lot of blend masks to even out lighting on objects I shoot and I often stitch multiple shots together to make higher res ones for printing when I need to. Anyway, I take a photo(s) of the panel hanging on the wall.

                           

                          In CS5 I use the polygon lasso tool (L) to go around the edges of the panel, and I cut it out and paste it on a new layer.

                          Then I fill the base layer with white

                           

                          On the layer with the pasted object I then use the ruler tool and draw a line across the top edge of the object panel and then I straighten it to have one edge as a horizontal reference edge that is true (I know it turns the whole image but that's OK as there is nothing but the layer I am working with). (Using this ruler/strighten function is something that I have only just started using and probably doesn't speed things up as I used to use the following for all the edges:

                           

                          I then drag guides in for the vertical and bottom horizontal edge with the scale dragged over to the top left corner of the panel so that I can place the guides at the right porportional scaled distance of the actual object if I need to.

                          Next, with snap on, I use ctrl+t Transform in combination with the ctrl key to drag the corners so that I can true up the vertical and bottom horizontal edges so the object has square edges and is the right proportional size.

                           

                          Hope that makes more sense. I guess what I was asking is if there is some way to automate squaring up the edges so that they are all perfictally vertical and horizontal? I might have to then go back and transform adjust for the scaling of width and height, but getting the edges true is what I am trying to speed up.

                           

                          Thanks again.

                          • 10. Re: Squaring up layered objects from photos?
                            c.pfaffenbichler Community Member

                            »In CS5 I use the polygon lasso tool (L) to go around the edges of the panel, and I cut it out and paste it on a new layer.«

                            Like I mentioned, if you want to use the Script you’ll need to use the Pen Tool instead and create a four-point-path – and the current Script creates a square, but one could include a dialog to enter the correct width/height-relation.

                            • 11. Re: Squaring up layered objects from photos?
                              Noel Carboni Community Member

                              I believe I've already given you the answer.

                               

                              Instead of lassoing the image, try surrounding it with the Crop Tool, then selecting the [  ] Perspective option, then moving the corner points to the corners of the object.  Complete the crop and the edges will be squared.  Now select all and copy/paste.

                               

                              For example:

                               

                              1.  Starting, say, with this perspective-distorted image of a print on the wall:

                               

                              PrintOnWall.jpg

                               

                               

                              2.  Crop as follows:

                               

                              PerspectiveCrop.jpg

                               

                               

                              3.  Complete the crop and voila, it's squared-up:

                               

                              SquaredUp.jpg

                               

                               

                              Only thing is that, as I mentioned above, if you move a lot of corners sometimes the Perspective Crop tool can leave the image a little too tall or too wide, and you might have to scale it in one dimension to make it look just right.

                               

                              -Noel

                              • 12. Re: Squaring up layered objects from photos?
                                Semaphoric Community Member

                                Can't believe no one has said View > Show > Grid! It's a big help for this sort of thing.

                                 

                                I find it easiest to use the Perspective thing in Free Transform while holding the Shift key, to constrain the movement to horizontal or vertical, first for one direction, then the other. If the vertical or horizontal scaling gwets thrown off, drag one of the middle handles.

                                 

                                Noels-Pic.jpg

                                • 13. Re: Squaring up layered objects from photos?
                                  JJMack MVP

                                  IMO Noel suggestion is best. If your poto is always of a rectangle product board the perspective crop tool is what you want to use not perspective free transform grid or no grid. Yout photo could be way off square using the perspective crop tool would be easy and results would be good  I did a quick search for a high resolution sample image using google images. Found this image http://interaction.dundee.ac.uk/~mvanis/wordpress/wp-content/uploads/2011/04/THIS-ONE.jpg  here are two screen captures in Photoshop Using Crop tool (best viewed in new tab or window)

                                  pct1.jpg

                                   

                                  Crop

                                  pct2.jpg

                                  • 14. Re: Squaring up layered objects from photos?
                                    doane2u Community Member

                                    I really appreciate everyone's help.

                                     

                                    I'm going to try the script out that c.pfaffenbich]ier posted, thanks!

                                     

                                    Also, thanks Noel, for your suggestion, that actually works really well, had never thought of using the crop tool. And thanks JJMack for the further explanation/clarification.

                                     

                                    Semaphoric, yes, I have used the grid and it works well, only problem is that it involves going in and setting up the grid for the right spacing that will fit the image and give measurement, but it does work.

                                     

                                    Again, very helpful advice from everyone, thanks.

                                    • 15. Re: Squaring up layered objects from photos?
                                      doane2u Community Member

                                      c.paffenbichier:

                                       

                                      I attempted to get the script to work but ran into some snags. I found the extended toolkit 2 exe under adobe utilities in the programs files (x86) under adobe and it ran and I pasted your script in and then attempted to save it as SquareUp.jsx under the scripts folder under the programs folder of adobe cs5 but I got an error pop up that "I don't have permission to save in this folder and do I want to save under my documents" so I saved there. I then started CS5 and did a test with a truncated shape on the base layer.. used the pen to draw 4 points on its corners then started the script program under file and there was no listing of my script. So from there I browsed and was able to load it from the saved location in my docs. It works but it creates a perfect square on a new layer. Is there some way of having it just square up the shape with out making it square with equal sides?

                                      I have admin privileges so don't know why I couldn't save it in the CS5 scripts folder under programs.. maybe a limitation of Windows 7?

                                      Thanks,

                                      • 16. Re: Squaring up layered objects from photos?
                                        c.pfaffenbichler Community Member

                                        It works but it creates a perfect square on a new layer. Is there some way of having it just square up the shape with out making it square with equal sides?

                                        Delete the lines

                                        if (theCopy.width > theCopy.height) {var theSize = theCopy.height}
                                        else {var theSize = theCopy.width};
                                        theCopy.resizeImage(theSize, theSize);
                                        

                                        or make them inactive by entering two slashes (//) at the start of each line.

                                         

                                        I work on a Mac, so I have no idea why you are not permitted from saving the jsx-file into the Scripts-Folder.

                                        You can also start it from ExtendScript Toolkit, by the way – just hit the Play-button icon in the Script’s window.

                                         

                                        Edit: But the Script actually offers no real advantage over Noel’s recommendation on the Crop Tool.

                                        • 17. Re: Squaring up layered objects from photos?
                                          MTSTUNER Community Member

                                          Do you have multiple versions of photoshop installed because the path for the cs5 should be:

                                          C:\Program Files (x86)\Adobe\Adobe Utilities - CS5\ExtendScript Toolkit CS5

                                           

                                           

                                          You probably need to right click on the ExtendScript Toolkit.exe and choose Run as Administrator in order to save the script to the photoshop cs5 scripts folder, then

                                          the script will be listed under File>Scripts.

                                           

                                           

                                          Untitled-1.png

                                           

                                          Also there should be a shortcut to the ExtendScript Toolkit.exe under the Start Menu>All Programs>Adobe and right click on that shortcut and choose Run As Administrator.

                                           

                                           

                                           

                                          MTSTUNER

                                          • 18. Re: Squaring up layered objects from photos?
                                            JJMack MVP

                                            On Win 7 64bit system there are two versions of Photoshop installed Scripts cab ne installed any where on your system and run using browse.

                                             

                                            When creatinf the path for the perspective crop script be sure ro creath the path starting with the top left corner then top right bottom right bottom left back to top left. If not the result will seem strange. For example top left botton left bottom right top right then top left CCW. reseultccwpc.jpg

                                            • 19. Re: Squaring up layered objects from photos?
                                              doane2u Community Member

                                              c.pfaffenbichier:  Hmmm... I tried it with the section disabled, but I still get a square. Would be nice to get it to work because it doesn't crop the whole image like the crop tool does which then requires using the canvas size to add space around it again.

                                               

                                              Mtstuner: Apparently, CS5 creates a folder, "Adobe Scripts"  under My Docs the first time you run the scripts editor. I ended up putting the script in that folder. Then in PS, to run the script, I go to File-Scripts and Browse to that folder and it seems from then on it keeps that path in memory. I guess they want you to keep them under My Documents, which is fine with me if it's easy to get to, which it is when it memorizes the location.

                                               

                                              Cjmack: Thanks, yes, I am going from top left corner then clockwise around the section I want to use.