Mogrify all files in directory, and add suffix.

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
akobylarek
Posts: 2
Joined: 2011-07-14T12:38:38-07:00
Authentication code: 8675308

Mogrify all files in directory, and add suffix.

Post by akobylarek »

Version: 6.71-0
Unix

I am trying to resize all pngs in a directory, there a couple hundred so I don't really want to do it by hand.

I want to keep the old files, and I want the new files to append a suffix on the end.

TempImage.png creates TempImage_Resized.png

Any help on how to do this would be greatly appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Mogrify all files in directory, and add suffix.

Post by fmw42 »

the closest I can come is to create two directories: tmp1 has the images and tmp2 is empty

cd tmp1
mogrify -path /Users/fred/tmp2/ -format "Resized.png" -resize 50% *.png

The results will be originalimagename.Resized.png and placed in tmp2. (or leave off the path and they will end up in tmp1)


see mogrify at
http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/basics ... fy_convert
http://www.imagemagick.org/Usage/basics/#mogrify_not
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Mogrify all files in directory, and add suffix.

Post by glennrp »

    This uses convert, not mogrify, but it should work on Unix/Linux:

    Code: Select all

    for x in *.png
    do
    root=`echo $x | sed -e "s/.png//"`
    convert $x -resize 200x150 ${root}_Resized.png
    done
    The back-ticks ( ` ) are hard to see in the listing; don't omit them!

    Here's a mogrify command I use sometimes:

    Code: Select all

    mogrify -resize 200x150 -format PNG *.png
    In this example the original files are filename.png and the thumbnails are filename.PNG
    User avatar
    fmw42
    Posts: 25562
    Joined: 2007-07-02T17:14:51-07:00
    Authentication code: 1152
    Location: Sunnyvale, California, USA

    Re: Mogrify all files in directory, and add suffix.

    Post by fmw42 »

    Here is an improvement that gets what you want.


    cd /Users/fred/tmp1
    mogrify -path /Users/fred/cyclops2/ -format "_Resized.jpg" -resize 50% *.png
    cd /Users/fred/tmp2
    filelist=$(ls)
    for file in $filelist; do
    mv "$file" "$(echo "$file" | sed 's/._/_/g' )"
    done
    User avatar
    anthony
    Posts: 8883
    Joined: 2004-05-31T19:27:03-07:00
    Authentication code: 8675308
    Location: Brisbane, Australia

    Re: Mogrify all files in directory, and add suffix.

    Post by anthony »

    I didn't know morgify -format would replace more than just the suffix! --nice
    Anthony Thyssen -- Webmaster for ImageMagick Example Pages
    https://imagemagick.org/Usage/
    User avatar
    fmw42
    Posts: 25562
    Joined: 2007-07-02T17:14:51-07:00
    Authentication code: 1152
    Location: Sunnyvale, California, USA

    Re: Mogrify all files in directory, and add suffix.

    Post by fmw42 »

    anthony wrote:I didn't know morgify -format would replace more than just the suffix! --nice

    Neither did I until I experimented. But I was puzzled and unable to achieve any results using %o as you discussed at http://www.imagemagick.org/Usage/basics ... fy_convert

    "Mogrify itself could modify the filename (using "-format" and "-path" settings) to generate a new filename accessable as '%o', in "convert" that is the same as the '%i' escape."

    can you provide a working example? have I misunderstood what you are trying to say here?
    User avatar
    anthony
    Posts: 8883
    Joined: 2004-05-31T19:27:03-07:00
    Authentication code: 8675308
    Location: Brisbane, Australia

    Re: Mogrify all files in directory, and add suffix.

    Post by anthony »

    fmw42 wrote:
    anthony wrote:I didn't know morgify -format would replace more than just the suffix! --nice
    Neither did I until I experimented. But I was puzzled and unable to achieve any results using %o as you discussed at http://www.imagemagick.org/Usage/basics ... fy_convert

    "Mogrify itself could modify the filename (using "-format" and "-path" settings) to generate a new filename accessible as '%o', in "convert" that is the same as the '%i' escape."

    can you provide a working example? have I misunderstood what you are trying to say here?
    Hmm I'm a little puzzled myself looking back on it. I'll remove that comment for now form IM examples, at least until it comes back to me, and I can write it more distinctly.

    Seems I also need a good link to the actual percent escape properties list page there.
    Anthony Thyssen -- Webmaster for ImageMagick Example Pages
    https://imagemagick.org/Usage/
    User avatar
    fmw42
    Posts: 25562
    Joined: 2007-07-02T17:14:51-07:00
    Authentication code: 1152
    Location: Sunnyvale, California, USA

    Re: Mogrify all files in directory, and add suffix.

    Post by fmw42 »

    akobylarek
    Posts: 2
    Joined: 2011-07-14T12:38:38-07:00
    Authentication code: 8675308

    Re: Mogrify all files in directory, and add suffix.

    Post by akobylarek »

    fmw42 wrote: cd /Users/fred/tmp1
    mogrify -path /Users/fred/cyclops2/ -format "_Resized.jpg" -resize 50% *.png
    cd /Users/fred/tmp2
    filelist=$(ls)
    for file in $filelist; do
    mv "$file" "$(echo "$file" | sed 's/._/_/g' )"
    done
    Brilliant, it worked perfectly. Thank you so much.
    Post Reply