bash调整图像尺寸以适合特定尺寸 [英] bash adjusting image dimensions to fit a certain size
问题描述
我到处搜索但找不到答案。
我想准确输出50Kb文件夹的所有图像并保持原始宽高比。
I searched everywhere but couldn't find an answer to this. I would like to output all images of a folder in 50Kb exactly and maintain the original aspect ratio.
我尝试了 ImageMagick
并调整为250x250,但它对我不起作用,它的作用是改变第一个维度并适应另一个维度,因此输出图像的大小不同。
I tried ImageMagick
and resizing to 250x250 e.g but it is not working for me, what it does is change the first dimension and adapt the other, so output images have not the same size.
for image in `ls $@*.jpg`; do
mogrify "${image}" -resize 250x250 "${image}"
done
怎么做?谢谢
推荐答案
更新答案
如果您希望图像的文件大小限制为50kB,请使用:
If you want the file size of the images to be limited to 50kB, use:
convert input.jpg -define jpeg:extent=50KB output.jpg
原始答案
使用 mogrify
,但要小心它会覆盖你的所有图片:
Use mogrify
, but be careful it will overwrite all your images:
mogrify -resize 250x250! *.jpg
一般情况下,使用 mogrify
我喜欢使用 -path
选项来指定结果的输出目录,这样可以避免原件被覆盖:
In general, when using mogrify
I like to use the -path
option to specify an output directory for results, like this to avoid originals getting overwritten:
mkdir results
mogrify -path results ...
如果你在调整大小后添加!
,即使它扭曲图片的形状也会强制调整大小,你的脚本会有效。
Your script will work if you add the !
after the resize which forces the resize even if it distorts the shape of the picture.
这篇关于bash调整图像尺寸以适合特定尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!