bash 使用 ImageMagick 批量调整图像到新文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8707694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch resize images into new folder using ImageMagick
提问by tekknolagi
I have a folder of images over 4MB
- let's call this folder dsc_big/
. I'd like to use convert -define jpeg:extent=2MB
to convert them to under 2MB
and copy dsc_big/*
to a folder dsc_small/
that already exists.
我有一个图像文件夹4MB
- 我们称之为文件夹dsc_big/
。我想convert -define jpeg:extent=2MB
用来将它们转换为下2MB
并复制dsc_big/*
到dsc_small/
已经存在的文件夹中。
I tried convert dsc_big/* -define jpeg:extent=2MB dsc_small/
but that produces images called -0
, -1
, and so on.
我尝试过,convert dsc_big/* -define jpeg:extent=2MB dsc_small/
但会产生名为-0
、 等的图像-1
。
What do I do?
我该怎么办?
回答by Eduardo Ivanec
convert
is designed to handle a single input file as far as I can tell, although I have to admit I don't understand the output you're getting. mogrifyis better suited for batch processing in the following style:
convert
据我所知,它旨在处理单个输入文件,尽管我不得不承认我不明白您得到的输出。mogrify更适合以下样式的批处理:
mogrify -path ../dsc_small -define jpeg:extent=2MB dsc_big/*
But honestly I consider it dangerous for general usage (it'll overwrite the original images if you forget that -path
) so I always use convert
coupled with a for loop for this:
但老实说,我认为它对于一般用途是危险的(如果你忘记了它会覆盖原始图像-path
)所以我总是使用convert
与 for 循环相结合的方法:
for file in dsc_big/*; do convert $file -define jpeg:extent=2MB dsc_small/`basename $file`; done
The basename
call isn't necessary if you're processing files in the current directory.
的basename
,如果你在当前目录中处理文件调用是没有必要的。
回答by sajin tm
This was the command which helped me after a long try. I wanted to make same sized thumbnails from a big list of large images which have variable width and height . It was for creating a gallery page.
这是经过长时间尝试后帮助我的命令。我想从具有可变宽度和高度的大图像列表中制作相同大小的缩略图。它用于创建画廊页面。
convert -define jpeg:size=250x200 *.jpg -thumbnail 250x200^ -gravity center -extent 250x200 crop/thumbnail-%d.jpeg
I got re-sized thumbnails which all having same width and height. :) thanks to ImageMagick.
我得到了重新调整大小的缩略图,它们都具有相同的宽度和高度。:) 感谢 ImageMagick。
回答by Nacho
Here's a solution withoutusing for loops on the console
这是在控制台上不使用 for 循环的解决方案
convert *.jpeg -define jpeg:extent=2MB -set filename:f '../dsc_small/%t_small.%e' +adjoin '%[filename:f]'
回答by Danish
Although this is an old question, but I'm adding this response for the benefit of anyone else that stumbles upon this.
尽管这是一个老问题,但我添加此回复是为了让其他任何偶然发现此问题的人受益。
I had the same exact issue, and being discouraged by the use of mogrify
, I wrote a small Python based utility called easymagickto make this process easier while internally using the convert
command.
我遇到了完全相同的问题,并且对 的使用感到气馁mogrify
,我编写了一个名为easymagick 的基于 Python 的小型实用程序,以便在内部使用该convert
命令时使此过程更容易。
Please note, this is still a work in progress. I'll appreciate any kind of feedback I can get.
请注意,这仍在进行中。我会很感激我能得到的任何反馈。
回答by Khawar
This works for me
这对我有用
convert -rotate 90 *.png rotate/image.jpg
produces image-0.jpg, image-1.jpg, image-2.jpg ..... in the 'rotate' folder. Don't know of a way to preserve the original filenames though.
在“旋转”文件夹中生成 image-0.jpg、image-1.jpg、image-2.jpg .....。虽然不知道保留原始文件名的方法。
回答by Valerie Anderson
I found that cd
-ing into the desired folder, and then using the bash global variable $PWD
made my convert
not throw any errors. I'm utilizing ImageMagick's recently implemented caption:
http://www.imagemagick.org/Usage/text/#captionfunction to label my images with the base filename and place them in another directory within the first.
我发现cd
-ing 进入所需的文件夹,然后使用 bash 全局变量$PWD
使我convert
没有抛出任何错误。我正在利用 ImageMagick 最近实现的caption:
http://www.imagemagick.org/Usage/text/#caption函数用基本文件名标记我的图像,并将它们放在第一个目录中的另一个目录中。
cd ~/person/photos
mkdir labeled
for f in $PWD/*.JPG; do
width=$(identify -format %w $f)
filename=$(basename $f .JPG)
convert -background '#0008' -colorspace transparent -fill white -gravity center -size ${width}x100 caption:"${filename}" ${f} +swap -gravity south -composite "$PWD/labeled/${filename}.jpg";
done