How to crop image from center without resizing with imagemagick
Asked Answered
Y

4

20
for parentDir in *
do
    cd "$parentDir"
    for subDir in *
    do
        cd "$subDir"
        for file in *.*
        do
            convert "$file" -crop 120x95 summary_"$file"
            convert "$file" -crop 160x225 detail_"$file"
        done
        mkdir detail
        mkdir summary
        mv summary_* summary/
        mv detail_* detail/
        cd ..
    done
cd ..
done

Here is my script, I need a way to crop the image without resizing, get rid of the extra surrounding.

For example: 1200* 1500 image ----> 120px * 90px from the center

Yun answered 24/10, 2016 at 18:8 Comment(1)
try adding -gravity center before the -cropGuatemala
V
40

If you are just trying to crop each image to one center part then use

convert input.suffix -gravity center -crop WxH+0+0 +repage output.suffix

Otherwise, you will get many WxH crops for each image.

Verbalize answered 30/12, 2016 at 23:19 Comment(4)
Don't forget to add output_file at the end of the command. I forgot it and got misleading error debug so take time to figure it out.Yearn
in batches for f in *.jpg; do convert "$f" -gravity center -crop 98x98+0+0 +repage "$f" ; doneScorekeeper
and fulfil with -extent in a canvas -background white for f in *.jpg; do convert "$f" -gravity center -crop 98x98+0+0 +repage -extent 98x98 -background white "$f" ; doneScorekeeper
@Ax is that a question or suggestion. Adding -extent will depend upon what result is wanted.Verbalize
D
1

Thanks to @fmw42 I've made this script to use with my file manager Dolphin, which can be adapted for others as well:

#!/usr/bin/env bash
# DEPENDS: imagemagick (inc. convert)
OLDIFS=$IFS
IFS="
"
# Get dimensions
WH="$(kdialog --title "Image Dimensions" --inputbox "Enter image width and height - e.g. 300x400:")"
# If no name was provided
if [ -z $WH ]
then
    exit 1
fi
for filename in "${@}"
do
    name=${filename%.*}
    ext=${filename##*.}
    convert "$filename" -gravity center -crop $WH+0+0 +repage "${name}"_cropped."${ext}"
done
IFS=$OLDIFS
Duwalt answered 18/9, 2020 at 11:28 Comment(0)
S
1

Enother imagemagick based solution. Here is a scrip version with mogrify to bulk images manipulation instead of convert which works on individual images:

for parentDir in *
do
  cd "$parentDir"
  for subDir in *
  do
    cd "$subDir"

    mkdir detail
    cp * detail/
    mogrify -gravity center -crop 160x225+0+0 +repage detail/*

    mkdir summary
    cp * summary/
    mogrify -gravity center -crop 120x95+0+0 +repage summary/*
  done
  cd ..
done
Shirt answered 16/5, 2022 at 1:18 Comment(0)
T
0

Other option IM version 6.2.4 and above

convert img.jpg -gravity center -extent 368x255 img.jpg

More info at https://www.imagemagick.org/Usage/crop/#extent

Tamarra answered 31/7 at 1:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.