image-manipulation-image-magick

image-manipulation-image-magick

热门

使用ImageMagick处理和操作图像。支持调整大小、格式转换、批量处理以及获取图像元数据。适用于处理图像、创建缩略图、调整壁纸尺寸或执行批量图像操作。

3.6万Star
4556Fork
更新于 2026/7/13
SKILL.md
readonly只读
name
image-manipulation-image-magick
description

使用ImageMagick处理和操作图像。支持调整大小、格式转换、批量处理以及获取图像元数据。适用于处理图像、创建缩略图、调整壁纸尺寸或执行批量图像操作。

使用ImageMagick进行图像处理

本技能支持在Windows、Linux和macOS系统上使用ImageMagick执行图像处理和操作任务。

何时使用本技能

当您需要以下操作时,请使用本技能:

  • 调整图像大小(单个或批量)
  • 获取图像尺寸和元数据
  • 转换图像格式
  • 创建缩略图
  • 为不同屏幕尺寸处理壁纸
  • 按特定条件批量处理多个图像

先决条件

  • 系统上已安装ImageMagick
  • Windows:PowerShell中ImageMagick可用为magick(或位于C:\Program Files\ImageMagick-*\magick.exe
  • Linux/macOS:Bash中已通过包管理器(aptbrew等)安装ImageMagick

核心功能

1. 图像信息

  • 获取图像尺寸(宽x高)
  • 检索详细元数据(格式、色彩空间等)
  • 识别图像格式

2. 图像调整大小

  • 调整单个图像大小
  • 批量调整多个图像大小
  • 创建指定尺寸的缩略图
  • 保持宽高比

3. 批量处理

  • 根据尺寸处理图像
  • 筛选并处理特定文件类型
  • 对多个文件应用变换

使用示例

示例0:解析magick可执行文件

PowerShell (Windows):

# 优先使用PATH上的ImageMagick
$magick = (Get-Command magick -ErrorAction SilentlyContinue)?.Source

# 回退:Program Files下的常见安装模式
if (-not $magick) {
    $magick = Get-ChildItem "C:\\Program Files\\ImageMagick-*\\magick.exe" -ErrorAction SilentlyContinue |
        Select-Object -First 1 -ExpandProperty FullName
}

if (-not $magick) {
    throw "未找到ImageMagick。请安装它并将'magick'添加到PATH。"
}

Bash (Linux/macOS):

# 检查magick是否在PATH中可用
if ! command -v magick &> /dev/null; then
    echo "未找到ImageMagick。请使用包管理器安装:"
    echo "  Ubuntu/Debian: sudo apt install imagemagick"
    echo "  macOS: brew install imagemagick"
    exit 1
fi

示例1:获取图像尺寸

PowerShell (Windows):

# 单个图像
& $magick identify -format "%wx%h" path/to/image.jpg

# 多个图像
Get-ChildItem "path/to/images/*" | ForEach-Object { 
    $dimensions = & $magick identify -format "%f: %wx%h`n" $_.FullName
    Write-Host $dimensions 
}

Bash (Linux/macOS):

# 单个图像
magick identify -format "%wx%h" path/to/image.jpg

# 多个图像
for img in path/to/images/*; do
    magick identify -format "%f: %wx%h\n" "$img"
done

示例2:调整图像大小

PowerShell (Windows):

# 调整单个图像大小
& $magick input.jpg -resize 427x240 output.jpg

# 批量调整图像大小
Get-ChildItem "path/to/images/*" | ForEach-Object { 
    & $magick $_.FullName -resize 427x240 "path/to/output/thumb_$($_.Name)"
}

Bash (Linux/macOS):

# 调整单个图像大小
magick input.jpg -resize 427x240 output.jpg

# 批量调整图像大小
for img in path/to/images/*; do
    filename=$(basename "$img")
    magick "$img" -resize 427x240 "path/to/output/thumb_$filename"
done

示例3:获取详细图像信息

PowerShell (Windows):

# 获取图像的详细信息
& $magick identify -verbose path/to/image.jpg

Bash (Linux/macOS):

# 获取图像的详细信息
magick identify -verbose path/to/image.jpg

示例4:根据尺寸处理图像

PowerShell (Windows):

Get-ChildItem "path/to/images/*" | ForEach-Object { 
    $dimensions = & $magick identify -format "%w,%h" $_.FullName
    if ($dimensions) {
        $width,$height = $dimensions -split ','
        if ([int]$width -eq 2560 -or [int]$height -eq 1440) {
            Write-Host "正在处理 $($_.Name)"
            & $magick $_.FullName -resize 427x240 "path/to/output/thumb_$($_.Name)"
        }
    }
}

Bash (Linux/macOS):

for img in path/to/images/*; do
    dimensions=$(magick identify -format "%w,%h" "$img")
    if [[ -n "$dimensions" ]]; then
        width=$(echo "$dimensions" | cut -d',' -f1)
        height=$(echo "$dimensions" | cut -d',' -f2)
        if [[ "$width" -eq 2560 || "$height" -eq 1440 ]]; then
            filename=$(basename "$img")
            echo "正在处理 $filename"
            magick "$img" -resize 427x240 "path/to/output/thumb_$filename"
        fi
    fi
done

指南

  1. 始终引用文件路径 - 对可能包含空格的文件路径使用引号
  2. 使用&运算符(PowerShell) - 在PowerShell中使用&调用magick可执行文件
  3. 将路径存储在变量中(PowerShell) - 将ImageMagick路径赋值给$magick以使代码更简洁
  4. 使用循环 - 处理多个文件时,使用ForEach-Object(PowerShell)或for循环(Bash)
  5. 先验证尺寸 - 在处理前检查图像尺寸以避免不必要的操作
  6. 使用适当的调整大小标志 - 考虑使用!强制精确尺寸或^表示最小尺寸

常见模式

PowerShell模式

模式:存储ImageMagick路径
$magick = (Get-Command magick).Source
模式:将尺寸作为变量获取
$dimensions = & $magick identify -format "%w,%h" $_.FullName
$width,$height = $dimensions -split ','
模式:条件处理
if ([int]$width -gt 1920) {
    & $magick $_.FullName -resize 1920x1080 $outputPath
}
模式:创建缩略图
& $magick $_.FullName -resize 427x240 "thumbnails/thumb_$($_.Name)"

Bash模式

模式:检查ImageMagick安装
command -v magick &> /dev/null || { echo "需要ImageMagick"; exit 1; }
模式:将尺寸作为变量获取
dimensions=$(magick identify -format "%w,%h" "$img")
width=$(echo "$dimensions" | cut -d',' -f1)
height=$(echo "$dimensions" | cut -d',' -f2)
模式:条件处理
if [[ "$width" -gt 1920 ]]; then
    magick "$img" -resize 1920x1080 "$outputPath"
fi
模式:创建缩略图
filename=$(basename "$img")
magick "$img" -resize 427x240 "thumbnails/thumb_$filename"

限制

  • 大型批量操作可能消耗大量内存
  • 某些复杂操作可能需要额外的ImageMagick委托
  • 在较旧的Linux系统上,使用convert代替magick(ImageMagick 6.x vs 7.x)