image-manipulation-image-magick
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseImage 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 (or at
magick)C:\Program Files\ImageMagick-*\magick.exe - Linux/macOS: Bash with ImageMagick installed via package manager (,
apt, etc.)brew
- 系统已安装ImageMagick
- Windows:PowerShell中可通过调用ImageMagick(或路径为
magick)C:\Program Files\ImageMagick-*\magick.exe - Linux/macOS:通过包管理器(、
apt等)在Bash环境中安装ImageMagickbrew
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
magick示例0:定位magick
可执行文件
magickPowerShell (Windows):
powershell
undefinedPowerShell (Windows):
powershell
undefinedPrefer 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):**
```bashif (-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):**
```bashCheck 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
undefinedif ! 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
undefinedExample 1: Get Image Dimensions
示例1:获取图像尺寸
PowerShell (Windows):
powershell
undefinedPowerShell (Windows):
powershell
undefinedFor 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):**
```bashGet-ChildItem "path/to/images/*" | ForEach-Object {
$dimensions = & $magick identify -format "%f: %wx%h`n" $_.FullName
Write-Host $dimensions
}
**Bash (Linux/macOS):**
```bashFor 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
undefinedfor img in path/to/images/*; do
magick identify -format "%f: %wx%h\n" "$img"
done
undefinedExample 2: Resize Images
示例2:调整图像大小
PowerShell (Windows):
powershell
undefinedPowerShell (Windows):
powershell
undefinedResize 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):**
```bashGet-ChildItem "path/to/images/*" | ForEach-Object {
& $magick $.FullName -resize 427x240 "path/to/output/thumb$($_.Name)"
}
**Bash (Linux/macOS):**
```bashResize 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
undefinedfor img in path/to/images/*; do
filename=$(basename "$img")
magick "$img" -resize 427x240 "path/to/output/thumb_$filename"
done
undefinedExample 3: Get Detailed Image Information
示例3:获取详细图像信息
PowerShell (Windows):
powershell
undefinedPowerShell (Windows):
powershell
undefinedGet 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):**
```bashGet verbose information about an image
Get verbose information about an image
magick identify -verbose path/to/image.jpg
undefinedmagick identify -verbose path/to/image.jpg
undefinedExample 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
donePowerShell (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
doneGuidelines
注意事项
- Always quote file paths - Use quotes around file paths that might contain spaces
- Use the operator (PowerShell) - Invoke the magick executable using
&in PowerShell& - Store the path in a variable (PowerShell) - Assign the ImageMagick path to for cleaner code
$magick - Wrap in loops - When processing multiple files, use (PowerShell) or
ForEach-Objectloops (Bash)for - Verify dimensions first - Check image dimensions before processing to avoid unnecessary operations
- Use appropriate resize flags - Consider using to force exact dimensions or
!for minimum dimensions^
- 始终给文件路径加引号 - 对包含空格的文件路径使用引号包裹
- 在PowerShell中使用运算符 - 在PowerShell中通过
&调用magick可执行文件& - 将路径存储在变量中(PowerShell) - 将ImageMagick路径赋值给以简化代码
$magick - 使用循环处理 - 处理多个文件时,使用PowerShell的或Bash的
ForEach-Object循环for - 先验证尺寸 - 处理前检查图像尺寸,避免不必要的操作
- 使用合适的缩放标记 - 可使用强制设置精确尺寸,或使用
!设置最小尺寸^
Common Patterns
常见模式
PowerShell Patterns
PowerShell 模式
Pattern: Store ImageMagick Path
模式:存储ImageMagick路径
powershell
$magick = (Get-Command magick).Sourcepowershell
$magick = (Get-Command magick).SourcePattern: 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"
fibash
if [[ "$width" -gt 1920 ]]; then
magick "$img" -resize 1920x1080 "$outputPath"
fiPattern: 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 instead of
convert(ImageMagick 6.x vs 7.x)magick
- 大规模批量操作可能占用大量内存
- 部分复杂操作可能需要额外的ImageMagick委托组件
- 在旧版Linux系统中,需使用而非
convert(ImageMagick 6.x与7.x的差异)magick