Skip to main content
Participant
September 25, 2012
Question

Automatic Image Compilation

  • September 25, 2012
  • 2 replies
  • 1583 views

Hello All,

I am hoping this great community can help me out.

I am looking to take two images and automatically compile them in to one.  Seems easy but there is a catch, I guess there is always a catch.

Image A is the same each time.  It is a picture of a person in a 4x6 aspect ratio.

Image B changes each time.  This image also has a predetermined location that is transparent where I want Image A to be resized to and placed automatically.

Step as I see them

Take image B and determine the size and location of the transparency

Resize image A to be the size determined

Create a canvas to the overall size of image B

Move image A to the location of the transparency on the new canvas

Place image B on on the new canvas thus being on top of image A

Save new Canvas

Is this possibly to script so I can batch do 350 or so of these?

Thanks in advance.

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
September 26, 2012

BMorden wrote:

I am looking to take two images and automatically compile them in to one.  Seems easy but there is a catch, I guess there is always a catch.

Image A is the same each time.  It is a picture of a person in a 4x6 aspect ratio.

Image B changes each time.  This image also has a predetermined location that is transparent where I want Image A to be resized to and placed automatically.

I think we more information.   Are Images files?

Like is image A and image file if so what file format. 

Are images of type B files if so are they PSD or PNG files since they have transparency. 

You want to batch it there are many type A images and many type B  images there needs be some process perhaps has two input folders one for Image A types and one for type b images.  The main routine would get a type A file name then need to be able to open the type b files for that particular type A file.

Once a type B image is open the layers transparency can be selected and inverted to get the transparent area. This could be saved as an Alpha chanel and used to position the place file A smart object layer resize to fit the transparent area and masked with the alpha channel to  do a virtual crop of the 4x6 image to the aspect ration of the transparent area if the area does not have a 2:3 aspect ratio.

JJMack
BMordenAuthor
Participant
September 26, 2012

Thanks for the responses. 

I have actively tried to solve this as well and I have come up with a pretty darn good solution so far...I have pulled a bunch of different ideas from others and created some custom scripting too.  This is a PoC to show that it can be done for 1 image.  I just need to clean it up and then figure out to it to run as a batch.  Be nice as this is my first attempt at PhotoShop Scripting

layerPixels2Selection = function(fLayer){

          if(mainPhoto.activeLayer.isBackgroundLayer){

             return;// background doesn't have a transparency mask

             }

          var desc = new ActionDescriptor();

          var ref = new ActionReference();

          ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );

          desc.putReference( charIDToTypeID( "null" ), ref );

          var ref1 = new ActionReference();

          ref1.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );

          desc.putReference( charIDToTypeID( "T   " ), ref1 );

          executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );

 

}

function MoveLayerTo(fLayer,fX,fY) {

  var Position = fLayer.bounds;

  Position[0] = fX - Position[0];

  Position[1] = fY - Position[1];

  fLayer.translate(-Position[0],-Position[1]);

}

//demo

// Save the current preferences

var startRulerUnits = app.preferences.rulerUnits

var startTypeUnits = app.preferences.typeUnits

var startDisplayDialogs = app.displayDialogs

// Set Adobe Photoshop CS6 to use pixels and display no dialogs

app.preferences.rulerUnits = Units.PIXELS

app.preferences.typeUnits = TypeUnits.PIXELS

app.displayDialogs = DialogModes.NO

var mainPhoto = app.activeDocument;

layerPixels2Selection(mainPhoto)

mainPhoto.selection.invert();

var mainPhotoWidth = mainPhoto.width;

var mainPhotoHeight = mainPhoto.height;

var mainPhotoResolution = mainPhoto.resolution;

var mainPhotoAspect = mainPhoto.width / mainPhoto.height;

var selectionTopLeftXOffset = mainPhoto.selection.bounds[0].as('px')

var selectionTopLeftYOffset = mainPhoto.selection.bounds[1].as('px')

var selectionBottomRightXOffset = mainPhoto.selection.bounds[2].as('px')

var selectionBottomRightYOffset = mainPhoto.selection.bounds[3].as('px')

var selectionWidth = mainPhoto.selection.bounds[2].as('px') -  mainPhoto.selection.bounds[0].as('px');

var selectionHeight = mainPhoto.selection.bounds[3].as('px') -  mainPhoto.selection.bounds[1].as('px');

var selectionAspect = selectionWidth / selectionHeight;

//alert (selectionWidth + ", " + selectionHeight);

var lifestylePhoto = File("C:/Greeting Cards - Resized/lifestyle.jpg")

open (lifestylePhoto)

var lifestylePhoto = app.documents.getByName("lifestyle.jpg")

var lifestylePhotoAspect = lifestylePhoto.width / lifestylePhoto.height;

if (selectionAspect > lifestylePhotoAspect) {

          var lifestylePhotoNewWidth = selectionWidth;

          var lifestylePhotoNewHeight = selectionWidth / lifestylePhotoAspect;

} else {

          var lifestylePhotoNewWidth = selectionHeight * lifestylePhotoAspect;

          var lifestylePhotoNewHeight = selectionHeight;

}

lifestylePhoto.resizeImage(lifestylePhotoNewWidth, lifestylePhotoNewHeight);

lifestylePhoto.selection.selectAll();

lifestylePhoto.selection.copy();

app.documents.add(mainPhotoWidth, mainPhotoHeight, mainPhotoResolution, 'newImage');

var newPhoto = app.documents.getByName("newImage");

newPhoto.paste();

MoveLayerTo(newPhoto.artLayers[0], selectionTopLeftXOffset, selectionTopLeftYOffset);

app.activeDocument = mainPhoto;

mainPhoto.selection.selectAll();

mainPhoto.selection.copy();

app.activeDocument = newPhoto

newPhoto.paste();

// Reset the application preferences

app.preferences.rulerUnits = startRulerUnits

app.preferences.typeUnits = startTypeUnits

app.displayDialogs = startDisplayDialogs

Inspiring
September 26, 2012

There are normally lots of ways to do something in Photoshop. Some are better suited to being scripted. I had asked for details about the images because there may be a different way to do this than the approach you outlined and it's hard to know without more details or sample images( which I can understand you might not want to share ).

Having said that, are you happy with what you have now and just want help running your code on a batch of images? Again there may be better/faster ways to do what your script is doing but it's hard to even test the code you posted without knowing more about the images it uses.

Inspiring
September 25, 2012

I am trying to understand this workflow. As I understand what you outlined you have a person image which you want to combine with multiple other images( scenes maybe? ). If that is correct I have several questions.

1. Are the B images unique to the person image? Or is B setup to work with other person images?.

2. Is there only one person image? Or do you need to do this with muptiple person images as well( each with a set of B images )?

3. If this needs to work with multiple person images is each person in the same position/size in the image?

4. Are there 350 B images? Or does that 350 batch run contain a mix of A and sets of B?