How to batch resize images in Ubuntu recursively w

2019-01-30 14:48发布

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. I am running Ubuntu 11.10 and i prefer learning how to do that directly from the terminal.

7条回答
孤傲高冷的网名
2楼-- · 2019-01-30 15:21

You can copy/paste this code in ubuntu, and save it as "resize.sh"

#!/bin/bash -e

CUR_DIR=`pwd`

cd $1

#resize

for file in *.jpg;

do

convert $file -resize 50% $file;

done

cd $CUR_DIR

After saving this file, run it from terminal using "./rotate.sh folder_containing_images"

For more such stuff, visit here

查看更多
再贱就再见
3楼-- · 2019-01-30 15: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%
查看更多
可以哭但决不认输i
4楼-- · 2019-01-30 15:24

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

convert $f.jpg -size 1024x768 $f.resized.png
查看更多
混吃等死
5楼-- · 2019-01-30 15:25

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%
查看更多
三岁会撩人
6楼-- · 2019-01-30 15:25

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.

查看更多
Rolldiameter
7楼-- · 2019-01-30 15:31

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/')' {} \;
查看更多
登录 后发表回答