Andy Li
Andy Li

Reputation: 327

How to crop image from center without resizing with imagemagick

convert "$file" -crop 120x95 summary_"$file"
convert "$file" -crop 160x225 detail_"$file"

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

Upvotes: 21

Views: 17104

Answers (4)

fearis
fearis

Reputation: 499

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

Upvotes: 1

Ivan Kovtun
Ivan Kovtun

Reputation: 703

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

Upvotes: 1

Sadi
Sadi

Reputation: 111

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

Upvotes: 1

fmw42
fmw42

Reputation: 53212

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.

Upvotes: 41

Related Questions