image-manipulation-image-magick

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Image Manipulation with ImageMagick

使用ImageMagick进行图像处理

This skill enables image processing and manipulation tasks using ImageMagick across Windows, Linux, and macOS systems.
本技能支持在Windows、Linux和macOS系统上使用ImageMagick完成图像处理与操作任务。

When to Use This Skill

何时使用本技能

Use this skill when you need to:
  • Resize images (single or batch)
  • Get image dimensions and metadata
  • Convert between image formats
  • Create thumbnails
  • Process wallpapers for different screen sizes
  • Batch process multiple images with specific criteria
当你需要以下操作时可使用本技能:
  • 调整图像大小(单张或批量)
  • 获取图像尺寸和元数据
  • 图像格式转换
  • 创建缩略图
  • 为不同屏幕尺寸处理壁纸
  • 按特定条件批量处理多张图像

Prerequisites

前提条件

  • ImageMagick installed on the system
  • Windows: PowerShell with ImageMagick available as
    magick
    (or at
    C:\Program Files\ImageMagick-*\magick.exe
    )
  • Linux/macOS: Bash with ImageMagick installed via package manager (
    apt
    ,
    brew
    , etc.)
  • 系统已安装ImageMagick
  • Windows:PowerShell中可通过
    magick
    调用ImageMagick(或路径为
    C:\Program Files\ImageMagick-*\magick.exe
  • Linux/macOS:通过包管理器(
    apt
    brew
    等)在Bash环境中安装ImageMagick

Core Capabilities

核心功能

1. Image Information

1. 图像信息

  • Get image dimensions (width x height)
  • Retrieve detailed metadata (format, color space, etc.)
  • Identify image format
  • 获取图像尺寸(宽×高)
  • 提取详细元数据(格式、色彩空间等)
  • 识别图像格式

2. Image Resizing

2. 图像缩放

  • Resize single images
  • Batch resize multiple images
  • Create thumbnails with specific dimensions
  • Maintain aspect ratios
  • 缩放单张图像
  • 批量缩放多张图像
  • 创建指定尺寸的缩略图
  • 保持宽高比

3. Batch Processing

3. 批量处理

  • Process images based on dimensions
  • Filter and process specific file types
  • Apply transformations to multiple files
  • 根据尺寸处理图像
  • 筛选并处理特定文件类型
  • 对多个文件应用转换操作

Usage Examples

使用示例

Example 0: Resolve
magick
executable

示例0:定位
magick
可执行文件

PowerShell (Windows):
powershell
undefined
PowerShell (Windows):
powershell
undefined

Prefer ImageMagick on PATH

Prefer ImageMagick on PATH

$magick = (Get-Command magick -ErrorAction SilentlyContinue)?.Source
$magick = (Get-Command magick -ErrorAction SilentlyContinue)?.Source

Fallback: common install pattern under Program Files

Fallback: common install pattern under 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 not found. Install it and/or add 'magick' to PATH." }

**Bash (Linux/macOS):**
```bash
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 not found. Install it and/or add 'magick' to PATH." }

**Bash (Linux/macOS):**
```bash

Check if magick is available on PATH

Check if magick is available on PATH

if ! command -v magick &> /dev/null; then echo "ImageMagick not found. Install it using your package manager:" echo " Ubuntu/Debian: sudo apt install imagemagick" echo " macOS: brew install imagemagick" exit 1 fi
undefined
if ! command -v magick &> /dev/null; then echo "ImageMagick not found. Install it using your package manager:" echo " Ubuntu/Debian: sudo apt install imagemagick" echo " macOS: brew install imagemagick" exit 1 fi
undefined

Example 1: Get Image Dimensions

示例1:获取图像尺寸

PowerShell (Windows):
powershell
undefined
PowerShell (Windows):
powershell
undefined

For a single image

For a single image

& $magick identify -format "%wx%h" path/to/image.jpg
& $magick identify -format "%wx%h" path/to/image.jpg

For multiple images

For multiple images

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

**Bash (Linux/macOS):**
```bash
Get-ChildItem "path/to/images/*" | ForEach-Object { $dimensions = & $magick identify -format "%f: %wx%h`n" $_.FullName Write-Host $dimensions }

**Bash (Linux/macOS):**
```bash

For a single image

For a single image

magick identify -format "%wx%h" path/to/image.jpg
magick identify -format "%wx%h" path/to/image.jpg

For multiple images

For multiple images

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

Example 2: Resize Images

示例2:调整图像大小

PowerShell (Windows):
powershell
undefined
PowerShell (Windows):
powershell
undefined

Resize a single image

Resize a single image

& $magick input.jpg -resize 427x240 output.jpg
& $magick input.jpg -resize 427x240 output.jpg

Batch resize images

Batch resize images

Get-ChildItem "path/to/images/*" | ForEach-Object { & $magick $.FullName -resize 427x240 "path/to/output/thumb$($_.Name)" }

**Bash (Linux/macOS):**
```bash
Get-ChildItem "path/to/images/*" | ForEach-Object { & $magick $.FullName -resize 427x240 "path/to/output/thumb$($_.Name)" }

**Bash (Linux/macOS):**
```bash

Resize a single image

Resize a single image

magick input.jpg -resize 427x240 output.jpg
magick input.jpg -resize 427x240 output.jpg

Batch resize images

Batch resize images

for img in path/to/images/*; do filename=$(basename "$img") magick "$img" -resize 427x240 "path/to/output/thumb_$filename" done
undefined
for img in path/to/images/*; do filename=$(basename "$img") magick "$img" -resize 427x240 "path/to/output/thumb_$filename" done
undefined

Example 3: Get Detailed Image Information

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

PowerShell (Windows):
powershell
undefined
PowerShell (Windows):
powershell
undefined

Get verbose information about an image

Get verbose information about an image

& $magick identify -verbose path/to/image.jpg

**Bash (Linux/macOS):**
```bash
& $magick identify -verbose path/to/image.jpg

**Bash (Linux/macOS):**
```bash

Get verbose information about an image

Get verbose information about an image

magick identify -verbose path/to/image.jpg
undefined
magick identify -verbose path/to/image.jpg
undefined

Example 4: Process Images Based on Dimensions

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

PowerShell (Windows):
powershell
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 "Processing $($_.Name)"
            & $magick $_.FullName -resize 427x240 "path/to/output/thumb_$($_.Name)"
        }
    }
}
Bash (Linux/macOS):
bash
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 "Processing $filename"
            magick "$img" -resize 427x240 "path/to/output/thumb_$filename"
        fi
    fi
done
PowerShell (Windows):
powershell
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 "Processing $($_.Name)"
            & $magick $_.FullName -resize 427x240 "path/to/output/thumb_$($_.Name)"
        }
    }
}
Bash (Linux/macOS):
bash
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 "Processing $filename"
            magick "$img" -resize 427x240 "path/to/output/thumb_$filename"
        fi
    fi
done

Guidelines

注意事项

  1. Always quote file paths - Use quotes around file paths that might contain spaces
  2. Use the
    &
    operator (PowerShell)
    - Invoke the magick executable using
    &
    in PowerShell
  3. Store the path in a variable (PowerShell) - Assign the ImageMagick path to
    $magick
    for cleaner code
  4. Wrap in loops - When processing multiple files, use
    ForEach-Object
    (PowerShell) or
    for
    loops (Bash)
  5. Verify dimensions first - Check image dimensions before processing to avoid unnecessary operations
  6. Use appropriate resize flags - Consider using
    !
    to force exact dimensions or
    ^
    for minimum dimensions
  1. 始终给文件路径加引号 - 对包含空格的文件路径使用引号包裹
  2. 在PowerShell中使用
    &
    运算符
    - 在PowerShell中通过
    &
    调用magick可执行文件
  3. 将路径存储在变量中(PowerShell) - 将ImageMagick路径赋值给
    $magick
    以简化代码
  4. 使用循环处理 - 处理多个文件时,使用PowerShell的
    ForEach-Object
    或Bash的
    for
    循环
  5. 先验证尺寸 - 处理前检查图像尺寸,避免不必要的操作
  6. 使用合适的缩放标记 - 可使用
    !
    强制设置精确尺寸,或使用
    ^
    设置最小尺寸

Common Patterns

常见模式

PowerShell Patterns

PowerShell 模式

Pattern: Store ImageMagick Path

模式:存储ImageMagick路径

powershell
$magick = (Get-Command magick).Source
powershell
$magick = (Get-Command magick).Source

Pattern: Get Dimensions as Variables

模式:将尺寸存储为变量

powershell
$dimensions = & $magick identify -format "%w,%h" $_.FullName
$width,$height = $dimensions -split ','
powershell
$dimensions = & $magick identify -format "%w,%h" $_.FullName
$width,$height = $dimensions -split ','

Pattern: Conditional Processing

模式:条件处理

powershell
if ([int]$width -gt 1920) {
    & $magick $_.FullName -resize 1920x1080 $outputPath
}
powershell
if ([int]$width -gt 1920) {
    & $magick $_.FullName -resize 1920x1080 $outputPath
}

Pattern: Create Thumbnails

模式:创建缩略图

powershell
& $magick $_.FullName -resize 427x240 "thumbnails/thumb_$($_.Name)"
powershell
& $magick $_.FullName -resize 427x240 "thumbnails/thumb_$($_.Name)"

Bash Patterns

Bash 模式

Pattern: Check ImageMagick Installation

模式:检查ImageMagick安装情况

bash
command -v magick &> /dev/null || { echo "ImageMagick required"; exit 1; }
bash
command -v magick &> /dev/null || { echo "ImageMagick required"; exit 1; }

Pattern: Get Dimensions as Variables

模式:将尺寸存储为变量

bash
dimensions=$(magick identify -format "%w,%h" "$img")
width=$(echo "$dimensions" | cut -d',' -f1)
height=$(echo "$dimensions" | cut -d',' -f2)
bash
dimensions=$(magick identify -format "%w,%h" "$img")
width=$(echo "$dimensions" | cut -d',' -f1)
height=$(echo "$dimensions" | cut -d',' -f2)

Pattern: Conditional Processing

模式:条件处理

bash
if [[ "$width" -gt 1920 ]]; then
    magick "$img" -resize 1920x1080 "$outputPath"
fi
bash
if [[ "$width" -gt 1920 ]]; then
    magick "$img" -resize 1920x1080 "$outputPath"
fi

Pattern: Create Thumbnails

模式:创建缩略图

bash
filename=$(basename "$img")
magick "$img" -resize 427x240 "thumbnails/thumb_$filename"
bash
filename=$(basename "$img")
magick "$img" -resize 427x240 "thumbnails/thumb_$filename"

Limitations

局限性

  • Large batch operations may be memory-intensive
  • Some complex operations may require additional ImageMagick delegates
  • On older Linux systems, use
    convert
    instead of
    magick
    (ImageMagick 6.x vs 7.x)
  • 大规模批量操作可能占用大量内存
  • 部分复杂操作可能需要额外的ImageMagick委托组件
  • 在旧版Linux系统中,需使用
    convert
    而非
    magick
    (ImageMagick 6.x与7.x的差异)