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

夙愿已清 提交于 2019-11-28 16:48:42

问题


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.


回答1:


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%



回答2:


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%



回答3:


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

convert $f.jpg -size 1024x768 $f.resized.png



回答4:


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.




回答5:


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.




回答6:


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/')' {} \;


来源:https://stackoverflow.com/questions/10802606/how-to-batch-resize-images-in-ubuntu-recursively-within-the-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!