How to batch resize images in Ubuntu recursively within the terminal?

前端 未结 6 1361
栀梦
栀梦 2020-12-23 10:15

I have multiple images stored in a set of organized folders. I need to re-size those images to a specific percentage recursively from their parent directory

相关标签:
6条回答
  • 2020-12-23 10:17

    You could use imagemagick. For instance, for resizing all the JPG images under the current directory to 50% of their original size, you could do:

    for f in `find . -name "*.jpg"`
    do
        convert $f -resize 50% $f.resized.jpg
    done
    

    The resulting files will have ".jpg" twice in their names. If that is an issue, you can check the following alternatives.

    For traversing/finding the files to resize, you can use xargs too. Example:

    find . -name "*.jpg" | xargs convert -resize 50%
    

    This will create copies of the images. If you just want to convert them in place, you can use:

    find . -name "*.jpg" | xargs mogrify -resize 50%
    
    0 讨论(0)
  • 2020-12-23 10:24

    Extending the answer from @betabandido

    Incase there are spaces in filenames or folder names in which the images are, then one should use -print0 with find and -0 with xargs to avoid any parsing errors.

    find . -name "*.jpg" -print0 | xargs -0 convert -resize 50%
    find . -name "*.jpg" -print0 | xargs -0 mogrify -resize 50%
    
    0 讨论(0)
  • 2020-12-23 10:29

    You can use imagemagick tool for batch resize.

    It will maintain the aspect ratio

    $ convert dragon.gif    -resize 64x64  resize_dragon.gif
    

    It will not maintain the aspect ratio

    $ convert dragon.gif    -resize 64x64\!  exact_dragon.gif
    
    $ cat resize.sh 
    #!/bin/bash
    for f in `find . -name "*.jpg"`
    do
        convert $f -resize 45x60\!  $f.resize.jpg
    done
    

    It will resize the image to 45x60 without maintaining the aspect ratio in current directory.

    0 讨论(0)
  • 2020-12-23 10:37

    You can also use

    sudo apt-get install nautilus-image-converter
    

    But this only works for image in the current folder. You just install and then right click on an image or multiple ones and choose the size you want and that's it.

    I believe this also uses imagemagick.

    0 讨论(0)
  • 2020-12-23 10:42

    there are a few answers like:

    find . -name "*.jpg" | xargs convert -resize 50%
    

    this won't work as it will expand the list like this: convert -resize 50% a.jpg b.jpg c.jpg which will resize a.jpg in c-0.jpg, b.jpg in c-1.jpg and let c.jpg untouched.

    So you have to execute the resize command for each match, and give both input file name and output file name, with something like:

    find . -name "*.jpg" | xargs -n 1 sh -c 'convert -resize 50% $0 $(echo $0 | sed 's/\.jpg/-th\.jpg/')'
    

    each match of find is individually passed by xargs -n 1 to the resize script: sh -c 'convert -resize 50% $0 $(echo $0 | sed 's/\.jpg/-th\.jpg/')'. This script receives the file name in argument $0, uses sed to make an output file name by substitution of the original .jpg suffix by a -th.jpg one. And it runs the convert command with those two file names.

    Here is the version without xargs but find -exec:

    find -name '*.jpg' -exec sh -c 'convert -resize 50% $0 $(echo $0 | sed 's/\.jpg/-th\.jpg/')' {} \;
    
    0 讨论(0)
  • 2020-12-23 10:43

    It's also works if you give the new resize resolution :

    convert $f.jpg -size 1024x768 $f.resized.png
    
    0 讨论(0)
提交回复
热议问题