Cropping images with loop in Imagemagick

TheNomadicMonad

I'm trying to batch crop images in a folder. Whenever I try doing this loop

for image in '/home/donald/Desktop/Im'*.jpg; do 
    convert "$image" -gravity center -crop 95X95% "${image%.jpg}"-modified.jpg
done 

I end up with the error:

convert.im6: unable to open image `/home/donald/Desktop/Im*.jpg': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no images defined `/home/donald/Desktop/Im*-modified.jpg' @ error/convert.c/ConvertImageCommand/3044.

Which is bizarre because there are definitely images in the folder and according the meta-data they are jpg

When I run shopt | grep glob I get this output:

dotglob         off
extglob         on
failglob        off
globstar        off
globasciiranges off
nocaseglob      off
nullglob        off

Here's my version information:

GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

Echo $- outputs

himBH
Mark Setchell

The correct syntax is:

#!/bin/bash
shopt -s nullglob
for image in $HOME/Desktop/Im*.jpg; do
   convert "$image" ...
done

Another option may be to use the mogrify command which is part of the ImageMagick suite:

# Create an output directory for cropped images
mkdir cropped
# Crop image and direct output to sub-directory
mogrify -gravity center -crop 95x95% -path cropped  *.jpg

You should definitely not parse the output of ls.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How-to improve the performance of cropping images with ImageMagick?

From Dev

TCropping images with loop in imagemagick

From Dev

TCropping images with loop in imagemagick

From Dev

ImageMagick: when cropping and merging multiple PNG images into a single PDF, pages are shifted out of view

From Dev

Cropping images using ImageMagick results in all output files (TIF) of same size?

From Dev

Imagemagick cropping thumbnail command line

From Dev

Cropping images in opencv

From Dev

Cropping/Scaling ImageNet Images

From Dev

cropping images in python

From Dev

Cropping images in opencv

From Dev

HTML/CSS cropping images

From Dev

Rotating and cropping images

From Dev

ImageMagick cropping large image into xyz tiles

From Dev

Resizing and cropping images using ImageResizer

From Dev

Umbraco Image Processor not cropping images

From Dev

ImageMagick / Colorspace 'safe' images

From Dev

Appending images vertically in ImageMagick

From Dev

Use ImageMagick to compare Images

From Dev

Resize images with imagemagick

From Dev

Convert multiple images with ImageMagick

From Dev

How to rewrite images imagemagick?

From Dev

Imagemagick to merge and mask images

From Dev

canvas- Cropping images in different shapes

From Dev

Rails 4 Resizing and Cropping Images similar to Facebook

From Dev

WordPress Multi Site not cropping images on upload

From Dev

Cropping images to square in Xamarin.Forms

From Dev

How to batch / multi process cropping images in the terminal?

From Dev

How to resize images without cropping or losing quality?

From Dev

How to use the first image sequence in a multiple cropping process in ImageMagick

Related Related

HotTag

Archive