
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
06/10/2025
France T l visions, France's leading broadcaster, has received the 2025 EBU ...
04/09/2025
Monumental Sports & Entertainment (MSE), in collaboration with Dalet, has been a...
07/08/2025
July 8 2025, 22:30 (PDT) Tata Motors & Dolby Bring Dolby Atmos to Harrier.ev, R...
29/07/2025
Staines-upon-Thames, UK, 29 July, 2025 Yospace, the global leader in Dynamic Ad ...
29/07/2025
Six Fellows Selected for Program Supporting Projects From Transgender Storytellers of Color
Today the nonprofit Sundance Institute announced the six artists p...
29/07/2025
By Jessica Herndon
One of the most exciting things about the Sundance Film Fest...
29/07/2025
Today, we announced our second quarter 2025 earnings, fueled by standout subscriber and MAU growth. In the first half of 2025, subscriber net additions grew mor...
29/07/2025
Idag rapporterar vi resultatet f r andra kvartalet 2025, med stark tillv xt av antalet prenumeranter och m natliga aktiva anv ndare. Under f rsta halv ret kade...
29/07/2025
Streaming Holds Steady in a Lighter Summer Viewership Season
NEW YORK - July 29...
29/07/2025
NEW YORK Nielsen is reporting that viewing of content with ads became more popular in Q2, 2025, gaining 1.2 share points of overall TV viewing to capture 73.6% ...
29/07/2025
SAN ANTONIO QuickLink has launched two new versions of its StudioEdge line of products: StudioEdge-1 and StudioEdge-2 provide one-channel and two-channels of br...
29/07/2025
The Society of Broadcast Engineers has announced the recipients of the 2025 SBE National Awards, which recognize outstanding achievements by individual members,...
29/07/2025
CHAMPAIGN, Ill. Cobalt Digital is heading to IBC 2025 with an expanded lineup of IPMX-compliant products and solutions that highlight its simple plug-and-play a...
29/07/2025
AMSTERDAM German manufacturer Guntermann & Drunck GmbH (G&D) has announced that it will present a wide range of KVM and video processing solutions for broadcast...
29/07/2025
At IBC 2025 in Amsterdam (September 12 15), German manufacturer Guntermann & Drunck GmbH (G&D) will present a range of intelligent solutions designed to meet th...
29/07/2025
MoU will support the Map Africa Initiative, a program designed to create a con...
29/07/2025
X-Rite Launches CT2100 Spectrophotometer for Fast, Affordable Retail Paint Color...
29/07/2025
Filming is now underway with Damien Molony and wider cast returning to Jersey for Bergerac, written by Toby Whithouse alongside Ashley Sanders, Emilie Robson an...
29/07/2025
NBA Summer League Tests Out, Refines Audio Workflows New mic arrays and ways of mixing them are a focus By Dan Daley, Audio Editor
Tuesday, July 29, 2025 - 7...
29/07/2025
Athlete Audio Builds Fan Engagement, Player Branding at WNBA All-Star Specialist A2 Ron Thompson has helped the technology evolve for decades By Dan Daley, Aud...
29/07/2025
Dante, Dell Technologies, Google, SMT, and Wave Central Renew Corporate Sponsors...
29/07/2025
Ross Video Case Study: How to Draw Fans Deeper into the Game By SVG Staff
Tuesday, July 29, 2025 - 11:36 am
Print This Story | Subscribe
Story Highlight...
29/07/2025
FIFA Club World Cup 2025: Sounding off with HBS at the largest production for a ...
29/07/2025
Back to All News
Breathless Returns to Netflix, Premiering October 31stPlay Video
Play Video
Entertainment
29 July 2025
GlobalSpain
Link copied to clipboa...
29/07/2025
Back to All News
Made in New Jersey: Finding the Perfect Shot for Our Hit Seque...
29/07/2025
Relationship Marks First U.S.-Based Distribution Partnership for FOR-A America...
29/07/2025
A wide shot of MID's new Public Meeting Chambers in session, showcasing the ...
29/07/2025
Through reliable connectivity and industry-leading levels of data completeness, Arqiva is helping water companies to meet regulatory targets and push for a wate...
28/07/2025
Summer is here, and whether you're road-tripping, relaxing poolside, or hosting friends for a backyard barbecue, the right soundtrack can make every moment ...
28/07/2025
In 2021, Spotify launched Amplifika in Brazil as a dedicated program to foster t...
28/07/2025
Summer is the perfect time to dive into a new story, whether you're on the move or just looking for an escape. With Spotify Premium, eligible listeners in s...
28/07/2025
IABM, the International Trade Association for Broadcast and Media Technology, has confirmed the appointment of its Members' Board for 2025, following the co...
28/07/2025
Media Prima has chosen DHD SX2 audio production mixers for integration into new broadcast studios at Balai Berita in Bangsar on the southwest periphery of Kuala...
28/07/2025
MNC Software, a global leader in network management and operational support systems tailored to the broadcast and media industry, has won a major monitoring and...
28/07/2025
Back to All News
Netflix Unveils the Official Trailer for the Limited Series Tw...
28/07/2025
Back to All News
New Korean Romantic Comedy Take Charge of My Heart' Produ...
28/07/2025
Kerry's dominant All Ireland Football Final display draws a peak of over one...
28/07/2025
Kerry's dominant All Ireland Football Final display draws a peak of over one...
28/07/2025
The electrical grid is designed to support loads that are relatively steady, such as lighting, household appliances, and industrial machines that operate at con...
28/07/2025
28 Jul 2025
VEON's Beeline Kazakhstan Opens New Office in Almaty Supporting...
28/07/2025
New classic: EMG / Gravity Media France on overlapping schedules and athlete acc...
28/07/2025
Indiana Pacers Sports & Entertainment's Emily Wright on the IP-based Tech Tr...
28/07/2025
Live From National Baseball Hall of Fame Induction: Cooperstown Is at the Heart ...
28/07/2025
SVG Attendees Get Shared Reality' Treatment at Cosm Experience & Tech Tour ...
28/07/2025
Monday 28 July 2025
To view this content, please enable our use of cookies. To ...
28/07/2025
Monday 28 July 2025
To view this content, please enable our use of cookies. To ...
28/07/2025
LinkedIn Wins Legal Battle to Protect Member Data Published on Jul 28, 2025 Categories: Company News
LinkedIn Corporate Communications
Share
LinkedIn ...
26/07/2025
IABM, the International Trade Association for Broadcast and Media Technology, has confirmed the appointment of its Members' Board for 2025, following the co...
26/07/2025
BALTIMORE In another sign that dealmaking for U.S. TV stations may be heating up amid hopes that regulators will eliminate or loosen broadcast ownership caps, S...
26/07/2025
LISBON wTVision, a provider of real-time graphics and broadcast services, has established a strategic alliance with Adistec that will see Adistec will distribut...