Skip to main content
Participating Frequently
December 25, 2019
Answered

Auto center

  • December 25, 2019
  • 3 replies
  • 1907 views

Hi!

 

How do I autmatically auto center an image with the help of a color marking?

I have a bunch of images with a blue dot (or small area) - I want that blue dot to be the center of the image. The entire image needs to move so that the blue dot ends up in the center.

 

This must be done with the help of a macro - I have to many images to do this manually.

 

Any suggestions?

This topic has been closed for replies.
Correct answer Chuck Uebele

You can try this. This works if you make a selection of the dot and you are on the layer that you want to center.

#target photoshop
var doc = activeDocument
var mainLayer = doc.activeLayer;
var xRef = (doc.selection.bounds[0]+doc.selection.bounds[2])/2
var yRef = (doc.selection.bounds[1]+doc.selection.bounds[3])/2
doc.selection.deselect();
mainLayer.translate (doc.width/2-xRef, doc.height/2-yRef);

3 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
December 25, 2019

You can try this. This works if you make a selection of the dot and you are on the layer that you want to center.

#target photoshop
var doc = activeDocument
var mainLayer = doc.activeLayer;
var xRef = (doc.selection.bounds[0]+doc.selection.bounds[2])/2
var yRef = (doc.selection.bounds[1]+doc.selection.bounds[3])/2
doc.selection.deselect();
mainLayer.translate (doc.width/2-xRef, doc.height/2-yRef);
BadEnarAuthor
Participating Frequently
December 25, 2019

Great!

Thanx!

Chuck Uebele
Community Expert
Community Expert
December 25, 2019

You would need some way to tell where the dot is, which, unless it's on a separate layer, would have to be done manually: use magic wand to select it, or draw a dot on a shape layer over it. Then you could get the location of either the selection or the shape layer dot and translate/move the other layer based on that info.

BadEnarAuthor
Participating Frequently
December 25, 2019

"Then you could get the location of either the selection or the shape layer dot and translate/move the other layer based on that info." - how do I do that?

JJMack
Community Expert
Community Expert
December 25, 2019

By center of the image do you mean the canvas enter.  How would a Script be able to locate the color marker in the image blue dot or small area? How did the marker get added to the image. To automate what you want automate the marker would need to be located to a precise pixel.

JJMack
BadEnarAuthor
Participating Frequently
December 25, 2019

Canvas center of course 🙂

The marker is in the photo from start - so I can't make a selection by color and somehow work from there?

JJMack
Community Expert
Community Expert
December 25, 2019

Great but is it a blue dot or an area.  Is the blue a unique blue color that appears no where else in the image. If the marker is a blue dot or an area where in the Area or in the dot is the actual pixel you want aligned to the canvas center. To automate the layer transform move the actual pixel needs to be moved so many pixels horizontally and vertically.  The Pixels need to be selected so its current location over the canvas can be determined so the horizontal and vertical distances can be calculated for the transforming move. If you want to automate the process there need to be a way of automating the selecting of the marked pixel.

JJMack