12 Replies Latest reply on Aug 30, 2013 7:34 AM by Michael L Hale

    Find Visual Centre of an image

    Mattmcquiff Level 1

      Not sure if this is possible, maybe with some maths? So possibly via applescript.

       

      I have a layer that is this is neck piece that you see, and you can see that the extension change makes the centre, 'off centre' or at least visually?

       

      How might I achieve this?

       

      I need to expand the canvas afterwards to create a border where the image is framed correctly as part of an automation.

      Screen shot 2013-08-21 at 15.57.00.png

        • 1. Re: Find Visual Centre of an image
          Mattmcquiff Level 1

          the nearest I think I could get,

          Copy to clipboard the entire layer

           

          Crop the vertical by half, with anchor point bottom centre,

          Trim based on transparent pixels

          From this you should have the centre point of the bottom half of the image, then some how replace from the infomation in the clipboard.....

          • 2. Re: Find Visual Centre of an image
            Michael L Hale Level 5

            Is the part that throws off the centering always at the top of the image? Is the necklace on a layer with a mask?

            • 3. Re: Find Visual Centre of an image
              Mattmcquiff Level 1

              Yes it's always the top half of the image.

               

              In terms of of layers from top to bottom.

               

              Main Image

              BACKGROUND

              Original image (hidden)

               

              The main image holding the necklace isn't a masked layer but can easily be one if that makes it easier.

              • 4. Re: Find Visual Centre of an image
                timuric

                Can you post your psd? To understand better what are possible solutions.

                • 5. Re: Find Visual Centre of an image
                  Michael L Hale Level 5

                  The main image holding the necklace isn't a masked layer but can easily be one if that makes it easier.

                  If the layer doesn't have a mask how does the layer's transparency help center the image? Even if you trim the transparent areas where the black border would be the bottom of the image is still off center because of the extra white on one side.

                  • 6. Re: Find Visual Centre of an image
                    Mattmcquiff Level 1

                    The Background layer is filled white and I hide it along with the layer original image and then trim based on transparent pixels.

                    • 8. Re: Find Visual Centre of an image
                      Michael L Hale Level 5

                      This works with the sample image.

                       

                      if(app.documents.length>0){
                          var white = new SolidColor();
                          white.rgb.hexValue = 'FFFFFF';
                          var doc = app.activeDocument;
                          var defaultRulerUnits = app.preferences.rulerUnits;
                          app.preferences.rulerUnits = Units.PIXELS;
                          var docWidth = doc.width.value;
                          var docHeight = doc.height.value;
                          doc.activeLayer = doc.layers[0];
                          loadLayerTransparencyToSelection();
                          doc.selection.select([[0,docHeight/2],[docWidth,docHeight/2],[docWidth,docHeight],[0,docHeight]],SelectionType.INTERSECT);
                          var necklaceWidth = doc.selection.bounds[2]-doc.selection.bounds[0];
                          doc.selection.deselect();
                          doc.activeLayer.translate(Math.abs(docWidth-necklaceWidth),0);
                          doc.revealAll();
                          selectLayerBelow();
                          doc.selection.selectAll();
                          doc.selection.fill(white);
                          doc.selection.deselect();
                          app.preferences.rulerUnits = defaultRulerUnits;
                      }
                      function loadLayerTransparencyToSelection() {
                          var desc = new ActionDescriptor();
                              var ref = new ActionReference();
                              ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
                          desc.putReference( charIDToTypeID('null'), ref );
                              var ref = new ActionReference();
                              ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Trsp') );
                          desc.putReference( charIDToTypeID('T   '), ref );
                          executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
                      };
                      function selectLayerBelow(){
                          var desc = new ActionDescriptor();
                              var ref = new ActionReference();
                              ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Bckw" ) );
                          desc.putReference( charIDToTypeID( "null" ), ref );
                          desc.putBoolean( charIDToTypeID( "MkVs" ), false );
                          executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
                      }
                      
                      • 9. Re: Find Visual Centre of an image
                        Mattmcquiff Level 1

                        Sorry, just been in receipt of the new CC!

                        I've run the script on the same file and it works well, run on another not so well. I don't think there is substatial difference to the last one to cause it to work but here is the new sample.

                         

                        http://we.tl/gorX3jGeNo

                         

                        I made sure I had erased around it so that there were no un erased pixels. but no joy.

                         

                        Many Thanks

                         

                        Matt

                        • 10. Re: Find Visual Centre of an image
                          Michael L Hale Level 5

                          Yes, there is a difference between the images. The script I posted doesn't really find the visual center of the image. All it does is center the widest part of the bottom half of the layer. It excludes the top half because that area is not symmetrical. To 'visually center' the width of the necklace the bottom half needs to be symmetrical. It was in your first sample and is not in this second sample.

                           

                          I would be worried that any 'fix' to correct for this image would only apply to this or similar images and not work for all the images you need to center.

                          • 11. Re: Find Visual Centre of an image
                            Mattmcquiff Level 1

                            I'm just trying to break it down as the item is very similar. But I relise how if they are wrong then it simply won't work.

                             

                            Actually discecting it to work in applescript form but stuck trying to load the selection.

                             

                            tell application id "com.adobe.photoshop"

                             

                                      tell current document

                              --set {height:originalHeight, width:originalWidth} to it

                                                load selection from channel "Main Image Transparency"

                                      end tell

                             

                            end tell

                             

                            Result:

                            error "Adobe Photoshop CC got an error: Can’t get selection of current document." number -1728 from selection of current document


                            • 12. Re: Find Visual Centre of an image
                              Michael L Hale Level 5

                              I can not help with Applescript other than suggest you use the do javascript command to load the layer's transparency.

                               

                              It may improve the centering if you divide the bottom half into 3 or 4 sections, find the center of each of those selections, then use the median of the centers.