Batch Resize and Output with Same Filename with a Prefix

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?".
Post Reply
Hobbit
Posts: 3
Joined: 2017-10-25T10:07:32-07:00
Authentication code: 1151

Batch Resize and Output with Same Filename with a Prefix

Post by Hobbit » 2017-10-25T10:23:50-07:00

Hi,

I'm a new user (version 7.0.7-8 Q16 x64 2017-10-14) and I've read the user guide and searched online without success in getting this done. The user guide talks about mogrify and convert, as does most of the online discussion, but the only Imagemagick executable I have is magick.exe, and the options seem to be different and give errors.

I want to resize by 50% a folder of files and output them with the same filenames but with a prefix, like this:

example1.png --> small_example1.png
example2.png --> small_example2.png
etc.

Could someone please point me in the right direction to do this?

Apologies if this question has already been answered.


Cheers,

User avatar
fmw42
Posts: 22080
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Batch Resize and Output with Same Filename with a Prefix

Post by fmw42 » 2017-10-25T11:17:57-07:00

In IM 7 magick is the same as convert. But in IM 7, you have to preface the other tools with magick. So for mogrify, you need to use magick mogrify. mogrify will process a whole folder of images, but it will only change the suffix. So you cannot add a prefix. Thus you must write a script loop over each image in the folder and use magick to resize the file and add a prefix to the output name from the input name.

So your script loop would be over the following command:

Code: Select all

magick.exe image.suffix -resize 50% newimage.suffix
Where you need to get the name and suffix of the file and then append to the name and add the suffix back again for the output.

You can get the input name and suffix from

magick image.suffix -format "%t" info:
magick image.suffix -format "%e" info:

See http://www.imagemagick.org/script/escape.php

Scripting is OS dependent. You seem to be on Windows and I am using a Mac so unix shell scripting. So I will not be able to help with batch scripting on Windows.

In Unix syntax, this would be:

Code: Select all

cd to directory of images
list=$(ls *.png)
for img in $list; do
name=$(magick $img -format "%t" info:)
suffix=$(magick $img -format "%e" info:)
magick $img -resize 50% "small_${name}.suffix"
done

User avatar
GeeMack
Posts: 469
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Batch Resize and Output with Same Filename with a Prefix

Post by GeeMack » 2017-10-25T11:32:45-07:00

Hobbit wrote:
2017-10-25T10:23:50-07:00
I want to resize by 50% a folder of files and output them with the same filenames but with a prefix, like this:

example1.png --> small_example1.png
From the Windows command prompt, if you want to resize all the PNG files in the current directory to 50%, a command like this will do exactly that...

Code: Select all

for %I in ( *.png ) do magick "%I" -set filename: "small_%t" -resize 50% "%[filename:].png"
If you want that command to work in a BAT script, you'll need to make all the single percent signs "%" into doubles "%%".

Post Reply