windows 调整图像大小的批处理脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40456998/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:42:30  来源:igfitidea点击:

A Batch Script To Resize Images

windowsshellbatch-filejpegimage-resizing

提问by Reed Williams

I'm looking for some help in writing a batch script to resize a bunch of .jpg images.

我正在寻找编写批处理脚本以调整一堆 .jpg 图像大小的帮助。

I don't have much experience with batch scripts. But this task will be preformed on a windows machine & so I thought a batch script might be a good way to go.

我对批处理脚本没有太多经验。但是这个任务将在 Windows 机器上执行,所以我认为批处理脚本可能是一个好方法。

I'm always interested in hearing alternative ideas & approaches, or being made aware of elements I haven't thought of.

我总是对听到替代的想法和方法感兴趣,或者了解我没有想到的元素。

Below I have listed the basic steps/needs of the script:

下面我列出了脚本的基本步骤/需求:

1) The images are located in a folder & are all(or should be) 500 x
500.

2) I need copy & past the images to a new folder, where they will be
resized to 250 x 250.

3) I then need to repeat step 2 but this time resize to 125 x 125.

回答by Ricardo Ortega Maga?a

回答by kenorb

Once you install ImageMagick for Windows, you can use magickcommand-line tool, e.g.

为 Windows安装ImageMagick 后,您可以使用magick命令行工具,例如

magick.exe mogrify -resize 250x250 -path 250x250/ *.png *.jpg
magick.exe mogrify -resize 125x125 -path 125x125/ *.png *.jpg

Note: Make sure your magick.execommand is in your PATHsystem variable and you're pointing to the existing or created the destined folders (e.g. mkdir 250x250/ 125x125/in above case).

注意:确保您的magick.exe命令在您的PATH系统变量中,并且您指向现有或创建的目标文件夹(例如mkdir 250x250/ 125x125/在上述情况下)。

For Linux/Ubuntu, see: How to easily resize images via command-line?

对于 Linux/Ubuntu,请参阅:如何通过命令行轻松调整图像大小?

回答by npocmaka

you can check scale.batwhich can resize images without a need of installing additional software - it uses only the windows built-in capabilities:

您可以检查scale.bat哪些可以调整图像大小而无需安装其他软件 - 它仅使用 Windows 内置功能:

@echo off
set "source_folder=c:\images"
set "result_folder_1=c:\res1"
set "result_folder_2=c:\res2"

for %%a in ("%source_folder%\*jpg") do (
   call scale.bat -source "%%~fa" -target "%result_folder_1%\%%~nxa" -max-height 250 -max-width 250 -keep-ratio no -force yes
)

for %%a in ("%source_folder%\*jpg") do (
   call scale.bat -source "%%~fa" -target "%result_folder_2%\%%~nxa" -max-height 125 -max-width 125 -keep-ratio no -force yes
)

Check also this.

也检查这个

回答by soja

If you want to do it on the command line specifically, perhaps so you can automate it, there is no specific command in Batch that is made for image manipulation. You can code up something in a JScript or another language and run it from the command line, but why do that when there are mature tools already available for this task?

如果您想专门在命令行上执行此操作,也许可以将其自动化,则 Batch 中没有用于图像处理的特定命令。您可以使用 JScript 或其他语言编写代码并从命令行运行它,但是既然已有成熟的工具可用于此任务,为什么还要这样做呢?

I recommend ImageMagick.

我推荐ImageMagick

Get the portable Windows binary, then you can use magick.exe to do what you want pretty easily. For example, to resize (by half) all the png images in folder 1 to folder 2:

获取可移植的 Windows 二进制文件,然后您可以使用 magick.exe 轻松完成您想要的操作。例如,要将文件夹 1 中的所有 png 图像大小调整(减半)到文件夹 2:

@echo off
if not exist 2 md 2
for %%a in (1\*.png) do "path\to\magick.exe" -resize 50x50% "1\%~nxa" "2\%~nxa"