Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Conform Layer to scale based on Alpha

  • Conform Layer to scale based on Alpha

    Posted by Bryan Woods on December 3, 2012 at 8:33 pm

    I need to line up logos in a row, with a set amount of spacing between them, and they all need to be scaled to the same size. The problem is these logos are all varying sizes, and some have extra white space around them giving them a larger bounding box than the actual image. I also need to be able to swap the logos out with others as needed and what ever is replaced gets recalculated and scaled appropriately. Help?

    My thought would be possibly analyzing a layer that would act as the “master scale” that all the logos adhere to, then use sampleImage to get the size of its alpha channel, and then conform the size of any logos (scaling by alpha channel and NOT by the bounding box dimensions of the layer) to master scale’s size. Is this possible via expressions? I’m not familiar with how to use sampleImage, but I do have some expression/scripting experience. Any questions about what I’m trying to accomplish please ask me! Thanks.

    Bryan Woods replied 13 years, 7 months ago 2 Members · 20 Replies
  • 20 Replies
  • Dan Ebberts

    December 3, 2012 at 9:52 pm

    Here’s a scale expression that will set the layer’s scale such that the width of the non-zero alpha part will match the value of nominalWidth (200px in this example):


    nominalWidth = 200;

    leftEdge = 0;

    for (i = 0; i <= width; i++){
    temp = sampleImage([i,height/2],[0.5,height/2],true,time);
    if (temp[3] > 0){
    leftEdge = i;
    break;
    }
    }

    rightEdge = width-1;

    for (i = width-1; i >= 0; i--){
    temp = sampleImage([i,height/2],[0.5,height/2],true,time);
    if (temp[3] > 0){
    rightEdge = i;
    break;
    }
    }
    value*nominalWidth/(rightEdge-leftEdge+1)

    Hopefully that will get you started.

    Dan

  • Dan Ebberts

    December 3, 2012 at 10:04 pm

    If the layer has already been scaled (ie not 100%), the last line should probably be like this:

    value*nominalWidth/(rightEdge-leftEdge+1)*100/value[0]

    Dan

  • Bryan Woods

    December 3, 2012 at 11:21 pm

    Cool, although I notice this uses width, and I would prefer using height. Is it as simple as swapping out “width” for “height” in the above code you’ve provided? I tried swapping out elements and they seem to dynamically resize properly based on the expression, so that part is working really well. Thanks for your help on this Dan.

  • Dan Ebberts

    December 3, 2012 at 11:37 pm

    For height, it would be like this:


    nominalHeight = 200;
    topEdge = 0;
    for (i = 0; i <= height; i++){
    temp = sampleImage([width/2,i],[width/2,0.5],true,time);
    if (temp[3] > 0){
    topEdge = i;
    break;
    }
    }
    bottomEdge = height-1;
    for (i = height-1; i >= 0; i--){
    temp = sampleImage([width/2,i],[width/2,0.5],true,time);
    if (temp[3] > 0){
    bottomEdge = i;
    break;
    }
    }
    value*nominalHeight/(bottomEdge-topEdge+1)*100/value[1]

    Dan

  • Bryan Woods

    December 3, 2012 at 11:39 pm

    Ok, went in and changed width to height and visa versa in your script, and everything seems to be working out well for logos that have no extra white space in them (ie proper bounding boxes around the logos). However, throwing in one that has some padding maybe at the top or sides (like a psd of a logo that is off center and then saved and brought in) does not seem to scale up properly which makes me wonder if its actually analyzing the alpha channel properly.

  • Bryan Woods

    December 3, 2012 at 11:50 pm

    Here’s an image to help illustrate what I’m talking about (disregard their position to each other, and rather look at the negative space between the bounding box and the actual content on the top logo vs. the bottom logo with a tight bounding box:

    Applying the expression doesn’t scale it properly. It gets sort of close, but not close enough.

  • Bryan Woods

    December 3, 2012 at 11:57 pm

    Oh no, wait! My mistake. It IS scaling it properly, but its position changes a little bit, so I have to go in and manually reposition it to be in line with the other logos.

  • Bryan Woods

    December 4, 2012 at 6:13 pm

    Ok, so working with it last night, I can’t come up with an elegant solution for logos that have extra negative space. I can offset a logo if I already know what center should be, but I need that extra bit of ease of just swap out logos and know that it will be resized AND aligned properly based on the other logos around it. I don’t know how that could be calculated though.

    The majority of my script is working out well though! I can currently throw in any logos and it will space them out equally and resize them properly, and if I need more or less, I just duplicate or remove layers and alt+replace layers as I need.

  • Dan Ebberts

    December 4, 2012 at 7:25 pm

    It seems like you could use a variation of the same sampleImage() expression for position, except that it would include code to adjust the y position based on how assymetrical the masked image is (using the layer’s height, scale, and top and bottom calculations). It would have to assume (I guess) that the layer’s anchor point hasn’t been moved from the center of the layer.

    Dan

  • Bryan Woods

    December 4, 2012 at 7:51 pm

    Maybe I’m going about it the wrong way. It might actually be easier to sample the image, then move the anchor point to the true center of the logo. From there, I can just grab all my logos and align to selection from the Align pallet. Because the biggest issue is making sure that all the logos are not only the same size, but also aligned horizontally. Is there a way to assign a new dimension to a layer based from what sampleImage finds and then position the anchor point at the center of that new dimension?

Page 1 of 2

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy