Multiple resize/rename on all files within directory

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
TomC

Multiple resize/rename on all files within directory

Post by TomC »

Hello. I am new and have read the FAQs. Quite simply, I need help with command syntax please.

I am attempting to resize/rename all (25) .jpg files within a directory. Ideally, each resize set of files would go it's own sub dir. I don't want to lose the original file. I don't want to type in each file name either, Is there a way in IM command without doing a shell script? This is what I have tried:

1. convert Filename_1096_C3_%d.jpg[1-16] -shave 240x0 -resize 96x72 newthumbs/Filename_1096_c3r_%d.jpg[1-16] (no go)

2. mogrify -shave 240x0 -resize 96x72 *.jpg (this one works, but I need to copy to subdir first, then process on copied files.


I know this may be easy for most of you, but I am stuck.

Thanks in advance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multiple resize/rename on all files within directory

Post by fmw42 »

mogrify is the best bet, but it works on one directory at a time.

otherwise, you will likely have to write a loop in a shell script to find all your files and then resize them where you want to place them.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiple resize/rename on all files within directory

Post by snibgo »

To put the outputs into directory xyz, try:

Code: Select all

mogrify -shave 240x0 -resize 96x72 -path xyz *.jpg
I get an error message with this, but only after it successfully creates the files.
mogrify: unable to open image `xyz\': No such file or directory @ error/blob.c/OpenBlob/2491.
mogrify: no decode delegate for this image format `xyz\' @ error/constitute.c/ReadImage/532.
Perhaps someone else can explain why I get the error.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multiple resize/rename on all files within directory

Post by fmw42 »

use the full path to the directory.

for example

create directory folder2 to hold new files
cd to directory folder1 which has the files

mogrify -path /Users/fred/folder2 -format jpg -shave 240x0 -resize 96x72 *.jpg
billbell52

Re: Multiple resize/rename on all files within directory

Post by billbell52 »

You can use this windows batch script. Modify to meet your needs. Save as .bat file. Double click to execute.

rem convert dir of bmp images to black and white
set SOURCE=C:\userdata\bell\letters
set DESTDIR=C:\userdata\bell\batch
set DESTSUBDIR=
set SRCTYPE=bmp
set DESTTYPE=bmp
rem Following must be one line. No carriage returns.
for /r "%SOURCE%" %%X in ("*.%SRCTYPE%") do (if not exist "%DESTDIR%\%%~nX.%DESTTYPE%" (convert %%X -black-threshold "50%" -white-threshold "49%" %DESTDIR%\%%~nX.%DESTTYPE%) else (echo "Exists %DESTDIR%\%%~nX.%DESTTYPE%"))
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiple resize/rename on all files within directory

Post by snibgo »

fmw42 wrote:use the full path to the directory.
Thanks, Fred. I tried with absolute paths, relative paths, with or without trailing slash, or using back-slash (the Windows convention). They all work, but all give the error.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multiple resize/rename on all files within directory

Post by fmw42 »

Thanks, Fred. I tried with absolute paths, relative paths, with or without trailing slash, or using back-slash (the Windows convention). They all work, but all give the error.
Check your file and folder permission.

What version of IM are you using. If old, then perhaps update. Or perhaps just reinstall. Something odd is happening on your system. What version of libjpg? If old, perhaps upgrade that too.

What platform are you on?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiple resize/rename on all files within directory

Post by snibgo »

Windows 7, IM 6.6.2-4.

No problem with permissions.

Code: Select all

md xyz
mogrify -path xyz tom.jpg
(Above works no problem.)

Code: Select all

mogrify -path xyz t*.jpg
mogrify: unable to open image `xyxxy\': No such file or directory @ error/blob.c/OpenBlob/2491.
mogrify: no decode delegate for this image format `xyxxy\' @ error/constitute.c/ReadImage/532.
mogrify: unable to open image `xyz\': No such file or directory @ error/blob.c/OpenBlob/2491.
mogrify: no decode delegate for this image format `xyz\' @ error/constitute.c/ReadImage/532.
As you might guess, this directory has two subdirectories: xyz and xyxxy.

The behaviour is the same when I use PNG files.

It is also the same when I omit "-path xyz". Oh dear, so the problem occurs with a trivial:

Code: Select all

mogrify t*.jpg
EDIT: the above problem is in Windows 7, IM 6.6.2-4 Q16.

The problem doesn't occur with IM 6.6.1-1 Q8 or 6.6.0-8 Q16.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiple resize/rename on all files within directory

Post by snibgo »

I've raised a bug report: viewtopic.php?f=3&t=16489
snibgo's IM pages: im.snibgo.com
TomC

Re: Multiple resize/rename on all files within directory

Post by TomC »

All,

Thanks for the prompt replies.

Adding the '-path' does help with moving the files.

I am still getting the error message even though the script seems to work.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiple resize/rename on all files within directory

Post by snibgo »

I am still getting the error message even though the script seems to work.
See the bug report. I expect this will be fixed in the next release.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Multiple resize/rename on all files within directory

Post by anthony »

Writing an Image, Multiple Times
http://www.imagemagick.org/Usage/files/#write

Then add this to a loop, such as...
Mogrify - Batch Processing Alternatives
http://www.imagemagick.org/Usage/basics/#mogrify_not
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Multiple resize/rename on all files within directory

Post by anthony »

NOTE that mogrify can write an image multiple times using -write

For example....

Code: Select all

   mogrify -set filename:name %f -write '%[filename:name]_orig.gif' \
              -resize x800  -write '%[filename:name]_800.gif' \
              -resize x100  *.jpg
Of course the last resized image (100 pixels high) got saved into the ORIGINAL file name.
but a (read/write) copy was made first!

See IM examples, Filename Percent Escapes
http://www.imagemagick.org/Usage/files/#save_escapes

NOTE you must use a setting starting with filename: when writing filenames. this is for security concerns though input from ALL unknown source should be check anyway.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tg3793
Posts: 6
Joined: 2010-11-19T08:00:56-07:00
Authentication code: 8675308

Re: Multiple resize/rename on all files within directory

Post by tg3793 »

Ok; well I thought it needed to be a little bit easier than this. Whether you do this on your home Linux desktop or a remote server doesn't matter (I did it on the latter).

Code: Select all

mogrify *.jpg -resize 10% *.jpg
The command above took about fifty images that were 2meg to 3meg each and converted them to images that were 90k to 150k in just a few seconds. All of the original images in the same directory were converted where they were ... Morgrify is great !

However I'd now like to stretch them to 640x480 since since they seem to all be about 420x360 (at the moment) or something similar ... I'm sure I'll dig up a switch around here somewhere for that :-)
tg3793
Posts: 6
Joined: 2010-11-19T08:00:56-07:00
Authentication code: 8675308

Re: Multiple resize/rename on all files within directory

Post by tg3793 »

ah ... I found a better method

Code: Select all

mogrify *.jpg -quality 75 -resize "640x480>" *
This rocks !! :-)
Post Reply