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

Find Visual Centre of an image

Contributor ,
Aug 21, 2013 Aug 21, 2013

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

TOPICS
Actions and scripting
2.9K
Translate
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
Adobe
Contributor ,
Aug 21, 2013 Aug 21, 2013

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.....

Translate
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
Guru ,
Aug 21, 2013 Aug 21, 2013

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

Translate
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
Contributor ,
Aug 21, 2013 Aug 21, 2013

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.

Translate
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
New Here ,
Aug 21, 2013 Aug 21, 2013

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

Translate
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
Contributor ,
Aug 21, 2013 Aug 21, 2013
Translate
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
Guru ,
Aug 22, 2013 Aug 22, 2013

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 );

}

Translate
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
Contributor ,
Aug 29, 2013 Aug 29, 2013

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

Translate
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
Guru ,
Aug 29, 2013 Aug 29, 2013

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.

Translate
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
Contributor ,
Aug 30, 2013 Aug 30, 2013

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


Translate
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
Guru ,
Aug 30, 2013 Aug 30, 2013
LATEST

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.

Translate
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
Guru ,
Aug 21, 2013 Aug 21, 2013

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.

Translate
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
Contributor ,
Aug 21, 2013 Aug 21, 2013

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

Translate
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