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
04/08/2024
Dalet, a leading technology and service provider for media-rich organizations, is excited to announce Santiago Solanas as its new Chief Executive Officer (CEO)....
03/06/2024
Dalet, a leading technology and service provider for media-rich organizations, a...
18/05/2024
PORTSMOUTH, N.H. Bundling is back in a big way, with all the major streaming companies and many pay TV operators exploring ways to simplify the consumer experie...
18/05/2024
WASHINGTON, D.C. Federal Communications Commission Chairwoman Jessica Rosenworcel has announced a tentative agenda for the June Open Commission Meeting schedule...
18/05/2024
Matthews Studio Equipment has introduced Grip Rail, which the company said offers a better way to mount equipment on location, in the studio, or on the fly....
18/05/2024
In a notable development in the industry-wide effort to address privacy concerns while improving efficacy of marketing efforts in a cookieless ad landscape, IAB...
18/05/2024
Missed any of our product coverage during your busy week? The TV Tech weekly product and services news wrap-up provides links to all of our coverage from May 13...
18/05/2024
DHD Elevates the Art of Podcast Production
Brie Clayton May 17, 2024
0 Comments
Hero image: the DHD DX2 base and expansion modules
Latest-generation ...
17/05/2024
Aerojet Rocketdyne has worked to modernize facilities at its Camden, Arkansas, l...
17/05/2024
The FCC has issued a Notice of Proposed Rulemaking (NPRM) that would revise rules governing low power TV stations (LPTV) in a number of areas, including online ...
17/05/2024
Demystifying Post-Production: Introducing Cinema 4D Particles Week 4
Brie Clayton May 17, 2024
0 Comments
With the spring release of Maxon One, we...
17/05/2024
Takashi Yamazaki Film Godzilla Minus One Graded with DaVinci Resolve Studio
Brie Clayton May 17, 2024
0 Comments
Hero image credit: 2023 TOHO CO., LT...
17/05/2024
Sterling Event Group Streamlines Live Event Productions with AJA
Brie Clayton May 17, 2024
0 Comments
Live event productions only happen once, which ...
17/05/2024
Muster Ngobi, product manager at LYNX Technik tells TVBEurope how the ever-evolving media industry provides a truly dynamic working environment
By Matthew Corr...
17/05/2024
NEW YORK As declines in linear TV viewing make the ongoing popularity of live sports, particularly football, central to financial success of the TV industry, th...
17/05/2024
During Netflixs second Upfront presentation to advertisers, Amy Reinhard, Netflix's president of advertising, walked advertisers through the continued growt...
17/05/2024
CINCINNATI The E.W. Scripps Company has added to its leadership team for news by promoting Jeff Kiernan a veteran journalist and general manager of Scripps'...
17/05/2024
Top executives from Disney, Fox and Warner Bros. Discovery have consistently insisted that their joint venture to launch the Venu Sports streaming bundle in the...
17/05/2024
ESPN has announced that its coverage of Caitlin Clark's WNBA debut in the Indiana Fever versus the Connecticut Sun season opener was the most-watched WNBA g...
17/05/2024
ATEM Mini Extreme ISO switcher and Blackmagic Pocket Cinema Camera 4K
Brie Clayton May 16, 2024
0 Comments
Blackmagic Design announced today that Yoic...
17/05/2024
Pixomondo's Virtual Production Academy Expands with Programs at Sony PCL, Vo...
17/05/2024
The Warner Bros. Discovery upfront presentation took place Wednesday, May 15 at ...
17/05/2024
Season 25 of The Voice wraps on NBC Tuesday, May 21, with performances from The Black Keys, Jelly Roll, Kate Hudson, Lainey Wilson, Muni Long, Thomas Rhett and ...
17/05/2024
New CNN CEO Mark Thompson spelled out his plan for the struggling news network d...
17/05/2024
Netflix, a newcomer to the advertising business, said it plans to launch an in-house advertising technology platform....
17/05/2024
Netflix shared some programming projects at an upfront presentation in New York. Those include the basketball-themed comedy series Running Point, a Mindy Kaling...
17/05/2024
Plex is offering movie and music collectors a 20% discount off its Lifetime Plex Pass as part of its Geek Week sale....
17/05/2024
Giant media buyer GroupM said it named Toby Jenner as global president, Group M Clients, a new position at the company....
17/05/2024
Smaller advertisers are increasingly buying connected TV programmatically, according to a new report from FreeWheel, Comcast's ad-tech unit....
17/05/2024
TCLtvPlus, the streaming app on smart TVs made by TCL, has added live linear channel from music-video programmer Vevo....
17/05/2024
StackAdapt said it made a deal to integrate data from Samba TV into its programmatic advertising platform....
17/05/2024
Tonight on House of Zwide: Dorothy is blown away by Ona's sketches for her w...
17/05/2024
Tonight on Scandal: Dintle has a visit from her past that leaves her very unsett...
17/05/2024
WO Traffic provides a solid foundation from which stations can manage, execute, and scale end-to-end ad trafficking and sales, both today and into the future. W...
17/05/2024
Broadcast Innovation in India: How AI and Automated Production Helps Smaller Spo...
17/05/2024
SVG Sports Cloud Production Forum Gives Refresher Course on Cloud-Based Tools, E...
17/05/2024
WNBA Tip-Off 2024: Scripps Sports Constructs New Studio for Second Season of WNB...
17/05/2024
SVG College Summit 2024: Auburn's War Eagle Productions Breaks Down How They...
17/05/2024
Back to All News
Netflix & Shondaland Announce the Song List and Soundtrack for...
17/05/2024
Skeem Saam: Thursday's episode, 16 May 2024 [video]Missed an episode of Skeem Saam? No problem! Watch the latest episode of your favourite South African soa...
17/05/2024
Prison Journalism: Letter to my mothersThabo Mthembu was incarcerated in Pollsmoor Prison from 2014 to 2019. Read Thabo's story
by Thabo Mthembu
17-05-20...
17/05/2024
Paul McCartney becomes UK's first billionaire musicianMusic icon Paul McCartney has become the UK's first billionaire musician, according to the Sunday ...
17/05/2024
Tonight on Smoke and Mirrors: Sakhile advises Tiny against sabotaging PetuniaDon't miss Friday, 17 May's riveting episode of South African soapie Smoke ...
17/05/2024
RT has today announced that Operation Transformation (OT) is to end after 17 seasons. As series come to an end each year, RT undertakes an editorial review to...
17/05/2024
By Craig Anderton
When I heard about binaural beats, I was interested-I like beats, and I'm into binaural audio. But this has nothing to do with either o...
17/05/2024
Scripps Research chemist Donna Blackmond elected to the Royal Society of the U.K. Blackmond's wide-ranging work has shaped origin of life theories, our unde...
16/05/2024
PARK CITY, UTAH - JANUARY 18: Director Yance Ford introduces the Power premiere at Library Center Theatre. (Photo by Chad Salvador/Shutterstock for Sundance F...
16/05/2024
May is Mental Health Awareness month, a time to recognize those living with ment...
16/05/2024
ABC and SBS bring digital radio (DAB ) services to the Gold Coast
15 May, 2024
Media releases
ABC and SBS today announced that audiences on the Gold Coast ...
16/05/2024
eds3_5_jq(document).ready(function($) { $(#eds_sliderM519).chameleonSlider_2_1({...