Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions position of highest alpha pixel

  • position of highest alpha pixel

    Posted by Adam Greenberg on April 4, 2023 at 6:49 pm

    Hi, I have to a new project that would require the user to import a graphic ( from a 3rd party ) and I would like to know if it is possible to have the position of a text layer appear 50 pixels ( for example ) above the top of this graphic. It could be a png, ai, etc.

    I would probably auto resize it with an expression I have used before.

    Is there a way to use sampleimage to look for the highest ( in regards to position ) alpha pixel ?

    Brie Clayton
    replied 3 years, 1 month ago
    3 Members · 10 Replies
  • 10 Replies
  • Filip Vandueren

    April 5, 2023 at 7:11 am

    Hi Adam,

    there was a thread about this a few years ago:

    https://creativecow.net/forums/reply/2435715/#.ZC0d9tachSs.link

    it samples lines/columns from all four sides until it hits one that contains alpha information.

    It can be made faster with a binary search, but for precision while sampling and averaging out, you would again want the project to be at least 16bpc

    Reply To: Is there a way to know the width and height of a layer similar to sourceRectAtTime?

  • Filip Vandueren

    April 5, 2023 at 8:53 am

    Here it is with a faster binary (powers of 2) approach, only searching the top edge, up to pixel accuracy:

    posterizeTime(0);
    const l = thisComp.layer("Logo");
    const w = l.width;
    const h = l.height;
    const heightLog2 = Math.ceil(Math.log(h)/Math.log(2));
    const threshold = 1; // on average 1 pixel in a row is opaque to count as the topEdge;
    var topEdge = 0;
    for(i=heightLog2; i>-1; i--) {
    let partition = 2 ** i;
    let s = l.sampleImage([w / 2, topEdge+partition/2], [w / 2, partition/2], false, time);
    if (s[3]<=threshold/(w*partition)) topEdge+=partition;
    }
    l.toComp([w/2, topEdge - 50]);
  • Adam Greenberg

    April 5, 2023 at 2:05 pm

    Amazing Filip, thanks so much.

    is there a way to have the x value constant ? I tried to replace the values with a number but it didnt’t work. I only need the height position to change.

  • Filip Vandueren

    April 5, 2023 at 3:44 pm

    the lastline could be something like:

    [960, l.toComp([w/2, topEdge])[1] -50 ];
  • Adam Greenberg

    April 5, 2023 at 3:44 pm

    my error, I think it was an anchor point issue on my end,

    thanks again Filip, this is a great expression

  • Adam Greenberg

    April 5, 2023 at 3:48 pm

    oh yes this works, I was entering that value in the wrong place.

    Thanks Filip !!!! ( for the 3rd time )

  • Adam Greenberg

    April 5, 2023 at 4:29 pm

    Filip, just for the math, could we do the same to find the bottom pixel and place something underneath ?

  • Adam Greenberg

    April 5, 2023 at 5:23 pm

    I have tried this but Im not sure it’s the most accurate way;

    [960, (h-l.toComp([w/2, topEdge])[1]+50) ];

  • Adam Greenberg

    April 5, 2023 at 6:43 pm

    ok this finds the bottom in case anyone needs it; ( thanks Filip )

    posterizeTime(0);

    const l = thisComp.layer(“TITRE_SHOW”);

    const w = l.width;

    const h = l.height;

    const heightLog2 = Math.ceil(Math.log(h)/Math.log(2));

    const threshold = 1; // on average 1 pixel in a row is opaque to count as the bottomEdge;

    var bottomEdge = h;

    for(i=heightLog2; i>-1; i–) { let partition = 2 ** i; let s = l.sampleImage([w / 2, bottomEdge-partition/2], [w / 2, partition/2], false, time); if (s[3]<=threshold/(w*partition)) bottomEdge-=partition; }

    [960, l.toComp([w/2, bottomEdge])[1]+50];

  • Brie Clayton

    April 7, 2023 at 1:53 am

    Thank you, Filip!

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