imagemagick - Fitting images by specifying the new aspect ratio -


can imagemagick's convert append white or black bars maintain aspect ratio after specifying just aspect ratio?

more concretely

suppose have 2000x1000 widescope image , compute new image has aspect ratio of 4:3 fit, tv. can do

convert input.png -background black -extent 2000x1500 -gravity center output.jpg 

but here have manually chosen 2000x1500 produce 250x2 pixels of blakc. can ask convert to:

  1. change aspect ratio 4:3
  2. not lose pixels; not interpolate pixels
  3. center image

?

if it's possible chose background color dominant color in image (as in itunes 11), mention how.

convert not have built-in capability pad image out given aspect ratio, need script this. here how might done in bash:

#!/bin/bash -e  im="$1" targetaspect="$2"  read w h <<< $(identify -ping -format "%w %h" "$im") curaspect=$(bc <<< "scale=10; $w / $h")  echo "current-aspect: $curaspect; target-aspect: $targetaspect"  comparison=$(bc <<< "$curaspect > $targetaspect") if [[ "$comparison" = "1" ]];     targeth=$(bc <<< "scale=10; $w / $targetaspect")     targetextent="${w}x${targeth%.*}" else     targetw=$(bc <<< "scale=10; $h * $targetaspect")     targetextent="${targetw%.*}x$h" fi  echo convert "$im" -background black \     -gravity center -extent "$targetextent" \     output.jpg 

call script input image , target aspect ratio given floating point number (for example, 4/3 = 1.333):

$ do-aspect input.png 1.333 

notes:

  • bc used floating point math, because bash has integer arithmetic.
  • note -gravity center on final command line before -extent. because gravity setting while extent operator. settings should precede operators affect, or else convert unexpected things when commands start more complicated.
  • when you're happy results of program, can either copy , execute output, or remove echo before final convert.

to question finding dominant color of image, there different ways of doing that, 1 common method resize image 1x1 , output color of resultant pixel - average color of whole image:

convert input.png -resize 1x1 -format '%[pixel:p[0,0]]' info: 

you can other processing on image before resizing different heuristic.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -