Crop and scale image using ffmpeg

Posted By : Rama shankar | 11-Aug-2016

Ffmpeg  is a framework use to convert (encode or decode ) video from one format to another format. we can also use it for scaling  and cropping image

 

1) Image Scaling 
 
 
For reducing or increasing size of an image we can use ffmpeg  by this command
 
1.ffmpeg - i input.png -vf scale=w:h output.png  
2.//where  -i is input parameter,  w is width of image in pixels ,  h  is height of image in pixels and output.png output file name
3.eg .ffmpeg - i input.png -vf scale=310:240 output.png
 
The above command simply scale image to the given  size width 400 px and height 400 px but the images aspect ratio may be destroy .
 
If you want to maintain the aspect ration of original image  you can use this command
1.ffmpeg - i input.png -vf scale=310:ih*240/iw output.png  
 
This command will maintain the original aspect ratio of image eg. if image has aspect ratio 2:3 then the image conversion will be de into 2:3 aspect ratio 
we can also convert image into own aspect ratio e.g. if an image has 2:3 but we want 1:1 
this can be done by this command
 
1.ffmpeg -i input.jpg -vf scale="'if(gt(a,1/1),320,-1)':'if(gt(a,1/1),-1,240)'" output.png


reference https://trac.ffmpeg.org/wiki/Scaling%20(resizing)%20with%20ffmpeg
 
 
2) cropping
 
 
To get the a part  of an image ffmpeg can be use

1.ffmpeg -i input.png -vf  "crop=w:h:x:y" input_crop.png
2. 
3.where -vf  video filter
4.w : width , h height , x and y are the left top coordinates of image
 
Thanks

About Author

Author Image
Rama shankar

Rama is a bright Java and Grails developer and has worked on development of various SaaS applications using Grails framework.

Request for Proposal

Name is required

Comment is required

Sending message..