Subba Gudipalli
Subba Gudipalli

Reputation: 11

Convert multiple Imagemagick commands into 1 command (trim, resize, square)

Currently, I am using multiple imagemagick commands to trim, resize (if width or height > 5000) and square. Is it possible to combine into 1 single command?

step 1: convert input_file.tif -fuzz 1% -trim output_file_trim.tif

step 2: get new image width and height using identify command from output_file_trim.tif

step 3: get max dimension from image width and height

step 4: if max dimension > 5000 then
convert output_file_trim.tif -resize 5000x5000 output_file_trim.tif

Step 5: Finally, finish the image conversion

convert output_file_trim.tif -flatten -gravity center -background white -extent "$max_dimension"x"$max_dimension" -format jpg output_file_final.jpg

@fmw42. Is the following single command correct to achieve this requirement:

convert `input_file.tif` -fuzz 1% -trim +repage \( +clone -rotate 90 +clone -mosaic +level-colors white \) +swap -flatten -gravity center -extent 105x105% -composite -format jpg `output_file_final.jpg`


Upvotes: 1

Views: 502

Answers (2)

GeeMack
GeeMack

Reputation: 5385

This command will read the input image and trim it. Then it resizes it to fit in a 5000x5000 box if it's larger than 5000x5000. Then it re-dimensions the canvas to a square with both dimensions being the larger of the width or height. It finishes by placing the image in the center of that square canvas with a white background.

convert input_file.tif -fuzz 1% -trim +repage -resize "5000x5000>" \
   -set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]" -virtual-pixel white \
   -distort affine "0,0 %[fx:h>w?(h-w)/2:0],%[fx:w>h?(w-h)/2:0]" \
   output_file_final.jpg

Upvotes: 1

fmw42
fmw42

Reputation: 53089

Putting your 5 steps into one command can only be done in IM 7 as follows (unix syntax):

magick -quiet input_file.tif -fuzz 1% -trim +repage \
-resize "5000>" \
-flatten -gravity center -background white \
-extent "%[fx:max(w,h)>5000?5000:max(w,h)]x%[fx:max(w,h)>5000?5000:max(w,h)]" \
output_file_final.jpg

In IM 6, you need to do it in two command. First find the larger of the max(w,h) and 500 as dim and save a temp image from your step one. Then do another command to finish it using that dim

dim=$(convert -quiet input_file.tif -fuzz 1% -trim +repage \
+write output_file_final.jpg -format "%[fx:max(w,h)>5000?5000:max(w,h)]" info:)

convert output_file_final.jpg -resize "5000>" \
-flatten -gravity center -background white \
-extent ${dim}x${dim} output_file_final.jpg

I do not understand your last command. It does not relate to the steps you outlined.

Upvotes: 0

Related Questions