
How to do Cropping and Scaling Using Javascript by Isak March 11, 2016 Howto
The Vidispine transcoder first crops, then rotates, then scales. This post is about how to use Javascript and some equations to emulate doing scaling before cropping. Brush up on your math skills and dive right in.
Let's look at the details of the previous blog post Using scripts to automatically fit video.
As said in the post above, the order of the operations is crop, rotate, scale. Assume this image (or video):
The goal is to rotate this image into the right orientation, and by cropping and padding we will keep the aspect ratio. Finally we would like a 1920 1080 output. First the crop. We crop the sides by 150 pixels each, and pad the top and bottom with 511 pixels each.
Then rotate the image. We now have:
You see that 1742:980 is very close to 16:9. Now we can scale the image.
The shape tag for the operation above:
1920 1080 -511 -511 150 150 #000080 1 1 1
2
3
4
5
6
7
8
9
10
11
12
13
1920
1080
-511
-511
150
150
#000080
1
1
So what if we wanted to scale first, then crop? Fortunately you can switch the order of the operations yourself, with a little bit of algebra. Above, we did:
Original dimension: x
Crop of the two sides: c1, c2
Scaling to size: x
If you start with an image that is 1000 pixels wide, and you want to scale it down to 500 pixels, then crop 100 pixels from each side, that is equivalent to:
Crop by 100 1000 / 500 = 200 pixels on each side. That gives you an image that is 1000 200 200 = 600 pixels.
Then scale the image to 300 pixels.
What if you know what the scaled dimension should be after cropping, and you know how much to crop in the scaled dimension? The scaling of the cropped dimension is x / (x c1 c2). That means, the cropped part in the scaled dimension is
c1 = c1 x / (x c1 c2)
c2 = c2 x / (x c1 c2)
Solving this equation system gives:
c1 = c1 x / ( c1 c2 x )
c2 = c2 x / ( c1 c2 x )
Or, in English. If you have an image that is 1000 pixels wide and you want to scale the image. After scaling, you want to crop 100 pixels from each side, and then the result to be 300 pixels:
Crop by 100 1000 / ( 100 100 300 ) = 200 pixels on each side. That gives you an image that is 1000 200 200 = 600 pixels.
Then scale the image to 300 pixels.
Ok, another example. Let's say you have an video, w h. We want to scale the height so it is 900 pixels. Then crop the sides so the resulting video is 1600 pixels wide.
The scaling of the video is 900 / h. That means that the scaled width is w = 900 / h w. So, in the scaled dimension what we want to crop is ( 1600 w ) / 2 on each side. But cropping is done before scaling. With the equation above, this is what the script could look like:
JavaScript
var inWidth = shape.getVideoComponent().get(0).getResolution().getWidth(); var inHeight = shape.getVideoComponent().get(0).getResolution().getHeight(); var scaling = preset.getVideo().getScaling(); var scaledWidth = scaling.getWidth() / inHeight * inWidth; var cropAfterScale = (scaling.getHeight() - scaledWidth) / 2: var cropBeforeScale = cropAfterScale * inWidth / (2 * cropAfterScale scaledWidth); scaling.setWidth(scaledWidth); scaling.setLeft(cropBeforeScale); scaling.setRight(cropBeforeScale);
1
2
3
4
5
6
7
8
9
10
11
var inWidth = shape.getVideoComponent().get(0).getResolution().getWidth();
var inHeight = shape.getVideoComponent().get(0).getResolution().getHeight();
var scaling = preset.getVideo().getScaling();
var scaledWidth = scaling.getWidth() / inHeight * inWidth;
var cropAfterScale = (scaling.getHeight() - scaledWidth) / 2:
var cropBeforeScale = cropAfterScale * inWidth / (2 * cropAfterScale scaledWidth);
scaling.setWidth(scaledWidth);
scaling.setLeft(cropBeforeScale);
scaling.setRight(cropBeforeScale);
Use this with a preset like:
1600 900 1 1 1
2
3
4
5
6
7
8
1600
900
1
1
Most recent headlines
05/01/2027
Worlds first 802.15.4ab-UWB chip verified by Calterah and Rohde & Schwarz to be ...
01/06/2026
January 6 2026, 05:30 (PST) Dolby Sets the New Standard for Premium Entertainment at CES 2026
Throughout the week, Dolby brings to life the latest innovatio...
01/05/2026
January 5 2026, 18:30 (PST) NBCUniversal's Peacock to Be First Streamer to ...
01/04/2026
January 4 2026, 18:00 (PST) DOLBY AND DOUYIN EMPOWER THE NEXT GENERATON OF CREATORS WITH DOLBY VISION
Douyin Users Can Now Create And Share Videos With Stun...
05/02/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
05/02/2026
Advanced Systems Group, LLC (ASG), a technology and services provider for media creatives and content owners, announced the appointment of Derek Pezzotti as Sen...
05/02/2026
Taurus Technologies, a Dallas-area professional AV systems integrator, has upgraded its in-house podcast studio with Brightline Lighting's AV/720 low-voltag...
05/02/2026
NBC Universal to Present XXV Olympic Winter Games Feb. 6-22 and Milan Cortina Paralympics March 6-15
NBC Sports to Utilize Grass Valley's Frame Rate Conver...
05/02/2026
Atomos today announced Shogun AV-19, a rack-mountable, 19-inch 4K HDR monitor-recorder-switcher designed for professional live production, broadcast, and video ...
05/02/2026
Vizrt, the leader in live production technology, revolutionizing viewer experience and engagement, today introduces two brand new solutions in partnership with ...
05/02/2026
Appear, a global leader in live production technology, today announced the appointment of Simon Frost in a newly created role as Chief Marketing Officer (CMO). ...
05/02/2026
New York gaffer Noah Chamis, ICLS ( You Deserve Each Other , The Half of It , Project Runway ) practices a mix of technical precision and creative play in his...
05/02/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
05/02/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
05/02/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
05/02/2026
Break out the cake and green sprinkles - GeForce NOW is turning six.
Since launch, members have streamed over 1 billion hours, and the party's just getting...
04/02/2026
The 11th-annual Summit will not only the unprecedented headwinds facing the business, but also the groundbreaking opportunities for the future....
04/02/2026
Just moments before the 2026 Grammy Awards kicked off, members of the event's audio team assembled for a group photo at the base of the stage inside Los Ang...
04/02/2026
At the Vienna Interdisciplinary Symposium on Aortic Repair (VISAR), Riedel Communications' Managed Technology Division delivered a turnkey technical infrast...
04/02/2026
The Mountain West Conference announces a new media rights package featuring CBS ...
04/02/2026
Earlier this week, the NFL announced it would play regular season games in Madrid, Paris, and Mexico City in 2026 as part of a nine-game international schedule,...
04/02/2026
Custom-built carts carry music speakers for Apple Music Super Bowl LX Halftime S...
04/02/2026
In an era of constrained spectrum, two tactics have emerged: work closely with regulatory bodies and utilize engineering chops
The Federal Communications Commi...
04/02/2026
Three examples of how wireless microphones are deployed to bring fans in deep and up close
Microphone manufacturers have myriad ways to put wireless to work fo...
04/02/2026
The mic manufacturer will join other suppliers in the new Rock Nashville production campus
Sennheiser is relocating its U.S. headquarters from its long-time lo...
04/02/2026
Olivia Colman and John Lithgow appear in Jimpa by Sophie Hyde, an official selection of the 2025 Sundance Film Festival. (Courtesy of Sundance Institute | pho...
04/02/2026
Lyrics are one of Spotify's most popular features, giving fans a richer way to experience the music and artists they love. They're viewed hundreds of mi...
04/02/2026
Queer Renegades: SBS Audio's new podcast reclaiming Australia's queer hi...
04/02/2026
The National Film and Video Foundation (NFVF) invites final-year animation students to participate in an exclusive creative showcase at the Annecy International...
04/02/2026
Viper Shield's robust phase of flight-testing production representative hard...
04/02/2026
Pilot To Launch with Super Bowl LX on February 8 and Continue with High Profile Live Events, Entertainment and Sports
Nielsen to Use State of the Art Wearable...
04/02/2026
New control solution applies broadcast robotics workflows to PTZ cameras with third-party integration and upgrade paths
Vinten, a global leader in robotic cam...
04/02/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/02/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/02/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/02/2026
Katie Vitolins Announced as Vice President of Alumni Products and Services An alumna and former trustee, Vitolins will lead the relaunch of Berklee's alum...
04/02/2026
Wednesday 4 February 2026
Full cast announced for Saturday Night Live UK, comin...
04/02/2026
Wednesday 4 February 2026
Fear of running out of mobile data (FORO) is a real i...
04/02/2026
Rohde & Schwarz powers next generation television in Brazil with DTV technology...
04/02/2026
Back to All News
Netflix Shares Teaser of the Third and Final Season of Knokke Off
Entertainment
04 February 2026
GlobalNetherlands
Link copied to clipboar...
04/02/2026
Back to All News
Netflix Premieres The TikTok Killer on March 6
Entertainment
04 February 2026
GlobalSpain
Link copied to clipboard
Download the pictures
...
04/02/2026
Back to All News
Music Is Having a Golden Moment on Netflix as New and Nostal...
04/02/2026
Fox Corporation Reports Second Quarter Fiscal 2026 Financial Results NEW YORK, NY, February 4, 2026 - Fox Corporation (Nasdaq: FOXA, FOX; FOX or the Compan...
04/02/2026
Lucas P. Arag n Joins FOX Advertising as Senior Vice President, Creative New York, NY - February 4, 2026 - Accomplished Creative Executive Lucas P. Arag n has...
04/02/2026
Following the passing of our friend and colleague Hugh Wallace, and with the full support of his family, RT will proceed with the broadcast of the new series o...
04/02/2026
Editor's note: This post is part of the Nemotron Labs blog series, which exp...
03/02/2026
Tagboard, a modern, interactive graphics system for news, sports, and entertainm...
03/02/2026
Studio Network Solutions (SNS) announces the launch of Trio, a new S3-compatible cloud storage service fully integrated with EVO for media backup, archival, and...
03/02/2026
50 Production Trucks at center of 160 U.S.-based productions...
03/02/2026
Nielsen, which specializes in audience measurement, data, and media intelligence, announces that it is piloting a new methodology enhancement to more accurately...