Modifying a list of images

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
harryhermit
Posts: 4
Joined: 2018-09-24T10:23:26-07:00
Authentication code: 1152

Modifying a list of images

Post by harryhermit »

Hi,

I'm using imagemagick in windows and I use this script at the command line:

magick *.jpg -set filename:f "%[t]" -background white -gravity center ^
-extent %[fx:max(w,h)]x%[fx:max(w,h)] -quality 100 "%[filename:f].jpg"

It works great for modifying images in the folder I navigate to, but I need to modify specific images nested in hundreds of different folders. I have a list of these images and their locations in a txt file. Is there a way to run this script in batch for these specific images in the txt file?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Modifying a list of images

Post by snibgo »

In Windows, use a "for" loop to read the text file. See "help for".
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Modifying a list of images

Post by GeeMack »

harryhermit wrote: 2018-09-24T23:58:40-07:00I have a list of these images and their locations in a txt file. Is there a way to run this script in batch for these specific images in the txt file?
If the text list has full path file names, you can have IM read that list by preceding it with "@". Then when setting the file name in your command, use "%[d]" to indicate the directory path. This command should do pretty much what you're looking for...

Code: Select all

magick @list.txt -set filename:f "%[d]\\%[t]" -background white -gravity center ^
   -extent %[fx:max(w,h)]x%[fx:max(w,h)] -quality 100 "%[filename:f].jpg"
Some issues to consider...

First, IM will try to read in all the files in "list.txt" before doing any processing, so if that will exhaust your memory resources you'd be better off building a command using a "for" loop as mentioned by snibgo above.

Also, I'd make sure to use full path filenames in the list, not relative paths. Note that setting the filenames for Windows requires you to escape the backslash "\" with another backslash like "%[d]\\%[t]".

If there are any spaces in any of your file or path names, you need to double quote those names in the text file otherwise IM will try to read the parts as if they're separate file names.

And as always, when running IM commands in a BAT script, make sure to double all the percent signs "%%" on everything except Windows own variables like "%TIME%".
harryhermit
Posts: 4
Joined: 2018-09-24T10:23:26-07:00
Authentication code: 1152

Re: Modifying a list of images

Post by harryhermit »

Thanks for the help! I got it to run using this batch:

for /F "tokens=*" %%A in (images.txt) do magick %%A -set filename:f "%%[t]" -background white -gravity center ^
-extent %%[fx:max(w,h)]x%%[fx:max(w,h)] -quality 100 "%%[filename:f].jpg"
Post Reply