利用ImageMagick命令行批量转换图片格式(webp转jpg、png转ico、heic转jpg)

准备条件:

系统需安装 ImageMagick 软件包,Linux、Cygwin/WSL下均可使用;Windows未经测试;

注:HEIC为苹果系统专有的图像格式,Windows系统上要转换需安装第三方工具HeicFileConsole,ImageMagick转换heic格式经本人测试无效(提示缺少相关的编解码器); image.png

HeicFileConsole下载地址:https://heicfile.com/command-line.html

简单命令示例:

1)、转换webp图片为JPG图片:

magick input.webp output.jpg
或
convert input.webp output.jpg

2)、批量转换当前目录webp图片为JPG图片:

magick mogrify -format JPEG *.webp

3)、指定源文件路径批量转换:

magick mogrify -format JPEG -path /path/to/dir *.webp

4)、HeicFileConsole转换HEIC图片为JPG: 使用帮助参考官网说明: https://heicfile.com/command-line.html

相关脚本函数:

png2ico() {
	#快捷把png图片转为ico图标:
	#资料来源:https://askubuntu.com/questions/867567/convert-jpg-or-png-to-ico-using-terminal-and-back
    #local i="${1}" o="${2:-${1:r}.ico}" s="${png2ico_size:-256}"
    #convert -resize x${s} -gravity center -crop ${s}x${s}+0+0 "$i" -flatten -colors 256 -background transparent "$o"
	if [ "$*" == "-h" -o "$*" == "--help" -o -z "$1" ];then
		echo -e "png2ico:快捷把png图片转为ico图标,缺省保存文件名默认生成\`同名文件名.ico\`;"
		echo -e "Usage:   png2ico *input_image.png [output.ico] [crop size]\n"
		echo -e "Example: png2ico 123.png "
		echo -e "\t png2ico 123.png save.ico 64 "
		return
	fi
	local i="${1}" o="${2:-${1:r}.ico}" size="${3:-32}"
	convert -resize x${size} -gravity center -crop ${size}x${size}+0+0 "$i" -flatten -colors 256 -background transparent "$o"
	#convert <your-image-here> -define icon:auto-resize=256,64,48,32,16 favicon.ico
}

webp2jpg() {
	#快捷把webp图片转成jpg:
	#See Also:https://stackoverflow.com/questions/49591274/cli-command-to-convert-webp-images-to-jpg
	#See Also2:https://stackoverflow.com/questions/60357123/bash-taking-a-wildcard-as-argument-of-a-shell-script-and-being-able-to-expand
	#ImageMagick v7:`magick input.webp output.jpg`
	#ImageMagick v6:`convert input.webp output.jpg`
	#-----------------------------------------
	#批量:magick mogrify -format JPEG *.webp
	if [ "$*" == "-h" -o "$*" == "--help" -o -z "$1" ];then
		echo -e "webp2jpg:快捷把webp图片转为jpg图片,缺省保存文件名默认生成\`同名文件名.jpg\`;"
		echo -e "Usage:   webp2jpg input.webp output.jpg"
		echo -e "\t webp2jpg 123.webp \n"
		echo -e "Example:  "
		echo -e "\t webp2jpg input.webp    #可省略保存文件名 "
		echo -e "\t webp2jpg input.webp output.jpg "
		echo -e "\t webp2jpg *.webp    #在当前目录下批量转换"
		return
	fi
	if [ $# -ge 3 ] || ([ $# -eq 2 ] && [ -f "$1" -a -f "$2" ]);then #传递3个及以上参数,或传递两个参数时(两个文件名均存在的情况),则启用批量转换...
		magick mogrify -monitor -format jpg *.webp
		return
	elif [ -f "$1" ];then
		[ ! -z "$2" ] && local saveFile="$2" || local saveFile="${1}.jpg" 
		magick "$1" "$saveFile"
		#convert "$1" "$saveFile"
	else
		print_color 40 "源文件不存在:$1 ..."
	fi
}

heic2jpg() {
	#快捷把HEIC图片(苹果系统专有格式)转成jpg:
	_HeicFileConsole "$@"
}

heic2png() {
	#调用HeicFileConsole第三方工具把HEIC(苹果系统专有格式)图片转为png
	#See Also:https://heicfile.com/command-line.html
	_HeicFileConsole "$@"
}

_HeicFileConsole() {
	#Window系统下把heic图片转为jpg/png的第三方命令行工具
	#Google搜索:command line heic to jpg on windows
	#使用方法:在有.heic图片的目录下执行wrapper函数heic2jpg即可;
	#软件官网:https://heicfile.com/command-line.html
	local apppath="/v/windows/HeicFileConsole/HeicFileConsole.exe"	
	if [ -e "${apppath}" ];then
		gsudo cmd /c start "" /D "$(cygpath -aw $PWD)" "$(cygpath -aw $apppath)" "$@"
	else
		echo -e "program not found!\npath:${apppath//\\/\\\\} "
	fi
}

使用效果:

参考资料

https://stackoverflow.com/questions/49591274/cli-command-to-convert-webp-images-to-jpg https://askubuntu.com/questions/867567/convert-jpg-or-png-to-ico-using-terminal-and-back https://heicfile.com/command-line.html

2
1
@guhunge
加入
更多来自 stevehe