Crop and pan Javascript snippet

Someone wrote in earlier today about wanting to take a series of images, crop them with different x/y coordinates, and then assemble them into an animated image. There’s no built in way to do the panning with the crop node, so I came up with this quick little JS node to do it, and thought it might be interesting enough to share with folks:

function processAsset(document, jsnode, asset) {
    
    var totalImages = 26;
    var currentPage = asset.pageNumber();
    var img = asset.CIImage();
    
    var targetWidth = 400;
    var targetHeight = 300;
    
    var deltaX = img.extent().size.width - targetWidth;
    var deltaY = img.extent().size.height - targetHeight;
    
    var x = deltaX * (currentPage / totalImages);
    var y = deltaY * (currentPage / totalImages);
    
    var cropRect = CGRectMake(x, y, targetWidth, targetHeight);
    
    var croppedImage = img.imageByCroppingToRect(cropRect);
    
    asset.setCIImage(croppedImage);
       
    return true;
}
1 Like