1
---
2
name: image-manipulation-image-magick
3
description: Process and manipulate images using ImageMagick. Supports resizing, format conversion, batch processing, and retrieving image metadata. Use when working with images, creating thumbnails, resizing wallpapers, or performing batch image operations.
4
compatibility: Requires ImageMagick installed and available as `magick` on PATH. Cross-platform examples provided for PowerShell (Windows) and Bash (Linux/macOS).
5
---
6
7
# Image Manipulation with ImageMagick
8
9
This skill enables image processing and manipulation tasks using ImageMagick
10
across Windows, Linux, and macOS systems.
11
12
## When to Use This Skill
13
14
Use this skill when you need to:
15
16
- Resize images (single or batch)
17
- Get image dimensions and metadata
18
- Convert between image formats
19
- Create thumbnails
20
- Process wallpapers for different screen sizes
21
- Batch process multiple images with specific criteria
22
23
## Prerequisites
24
25
- ImageMagick installed on the system
26
- **Windows**: PowerShell with ImageMagick available as `magick` (or at `C:\Program Files\ImageMagick-*\magick.exe`)
27
- **Linux/macOS**: Bash with ImageMagick installed via package manager (`apt`, `brew`, etc.)
28
29
## Core Capabilities
30
31
### 1. Image Information
32
33
- Get image dimensions (width x height)
34
- Retrieve detailed metadata (format, color space, etc.)
35
- Identify image format
36
37
### 2. Image Resizing
38
39
- Resize single images
40
- Batch resize multiple images
41
- Create thumbnails with specific dimensions
42
- Maintain aspect ratios
43
44
### 3. Batch Processing
45
46
- Process images based on dimensions
47
- Filter and process specific file types
48
- Apply transformations to multiple files
49
50
## Usage Examples
51
52
### Example 0: Resolve `magick` executable
53
54
**PowerShell (Windows):**
55
```powershell
56
# Prefer ImageMagick on PATH
57
$magick = (Get-Command magick -ErrorAction SilentlyContinue)?.Source
58
59
# Fallback: common install pattern under Program Files
60
if (-not $magick) {
61
$magick = Get-ChildItem "C:\\Program Files\\ImageMagick-*\\magick.exe" -ErrorAction SilentlyContinue |
62
Select-Object -First 1 -ExpandProperty FullName
63
}
64
65
if (-not $magick) {
66
throw "ImageMagick not found. Install it and/or add 'magick' to PATH."
67
}
68
```
69
70
**Bash (Linux/macOS):**
71
```bash
72
# Check if magick is available on PATH
73
if ! command -v magick &> /dev/null; then
74
echo "ImageMagick not found. Install it using your package manager:"
75
echo " Ubuntu/Debian: sudo apt install imagemagick"
76
echo " macOS: brew install imagemagick"
77
exit 1
78
fi
79
```
80
81
### Example 1: Get Image Dimensions
82
83
**PowerShell (Windows):**
84
```powershell
85
# For a single image
86
& $magick identify -format "%wx%h" path/to/image.jpg
87
88
# For multiple images
89
Get-ChildItem "path/to/images/*" | ForEach-Object {
90
$dimensions = & $magick identify -format "%f: %wx%h`n" $_.FullName
91
Write-Host $dimensions
92
}
93
```
94
95
**Bash (Linux/macOS):**
96
```bash
97
# For a single image
98
magick identify -format "%wx%h" path/to/image.jpg
99
100
# For multiple images
101
for img in path/to/images/*; do
102
magick identify -format "%f: %wx%h\n" "$img"
103
done
104
```
105
106
### Example 2: Resize Images
107
108
**PowerShell (Windows):**
109
```powershell
110
# Resize a single image
111
& $magick input.jpg -resize 427x240 output.jpg
112
113
# Batch resize images
114
Get-ChildItem "path/to/images/*" | ForEach-Object {
115
& $magick $_.FullName -resize 427x240 "path/to/output/thumb_$($_.Name)"
116
}
117
```
118
119
**Bash (Linux/macOS):**
120
```bash
121
# Resize a single image
122
magick input.jpg -resize 427x240 output.jpg
123
124
# Batch resize images
125
for img in path/to/images/*; do
126
filename=$(basename "$img")
127
magick "$img" -resize 427x240 "path/to/output/thumb_$filename"
128
done
129
```
130
131
### Example 3: Get Detailed Image Information
132
133
**PowerShell (Windows):**
134
```powershell
135
# Get verbose information about an image
136
& $magick identify -verbose path/to/image.jpg
137
```
138
139
**Bash (Linux/macOS):**
140
```bash
141
# Get verbose information about an image
142
magick identify -verbose path/to/image.jpg
143
```
144
145
### Example 4: Process Images Based on Dimensions
146
147
**PowerShell (Windows):**
148
```powershell
149
Get-ChildItem "path/to/images/*" | ForEach-Object {
150
$dimensions = & $magick identify -format "%w,%h" $_.FullName
151
if ($dimensions) {
152
$width,$height = $dimensions -split ','
153
if ([int]$width -eq 2560 -or [int]$height -eq 1440) {
154
Write-Host "Processing $($_.Name)"
155
& $magick $_.FullName -resize 427x240 "path/to/output/thumb_$($_.Name)"
156
}
157
}
158
}
159
```
160
161
**Bash (Linux/macOS):**
162
```bash
163
for img in path/to/images/*; do
164
dimensions=$(magick identify -format "%w,%h" "$img")
165
if [[ -n "$dimensions" ]]; then
166
width=$(echo "$dimensions" | cut -d',' -f1)
167
height=$(echo "$dimensions" | cut -d',' -f2)
168
if [[ "$width" -eq 2560 || "$height" -eq 1440 ]]; then
169
filename=$(basename "$img")
170
echo "Processing $filename"
171
magick "$img" -resize 427x240 "path/to/output/thumb_$filename"
172
fi
173
fi
174
done
175
```
176
177
## Guidelines
178
179
1. **Always quote file paths** - Use quotes around file paths that might contain spaces
180
2. **Use the `&` operator (PowerShell)** - Invoke the magick executable using `&` in PowerShell
181
3. **Store the path in a variable (PowerShell)** - Assign the ImageMagick path to `$magick` for cleaner code
182
4. **Wrap in loops** - When processing multiple files, use `ForEach-Object` (PowerShell) or `for` loops (Bash)
183
5. **Verify dimensions first** - Check image dimensions before processing to avoid unnecessary operations
184
6. **Use appropriate resize flags** - Consider using `!` to force exact dimensions or `^` for minimum dimensions
185
186
## Common Patterns
187
188
### PowerShell Patterns
189
190
#### Pattern: Store ImageMagick Path
191
192
```powershell
193
$magick = (Get-Command magick).Source
194
```
195
196
#### Pattern: Get Dimensions as Variables
197
198
```powershell
199
$dimensions = & $magick identify -format "%w,%h" $_.FullName
200
$width,$height = $dimensions -split ','
201
```
202
203
#### Pattern: Conditional Processing
204
205
```powershell
206
if ([int]$width -gt 1920) {
207
& $magick $_.FullName -resize 1920x1080 $outputPath
208
}
209
```
210
211
#### Pattern: Create Thumbnails
212
213
```powershell
214
& $magick $_.FullName -resize 427x240 "thumbnails/thumb_$($_.Name)"
215
```
216
217
### Bash Patterns
218
219
#### Pattern: Check ImageMagick Installation
220
221
```bash
222
command -v magick &> /dev/null || { echo "ImageMagick required"; exit 1; }
223
```
224
225
#### Pattern: Get Dimensions as Variables
226
227
```bash
228
dimensions=$(magick identify -format "%w,%h" "$img")
229
width=$(echo "$dimensions" | cut -d',' -f1)
230
height=$(echo "$dimensions" | cut -d',' -f2)
231
```
232
233
#### Pattern: Conditional Processing
234
235
```bash
236
if [[ "$width" -gt 1920 ]]; then
237
magick "$img" -resize 1920x1080 "$outputPath"
238
fi
239
```
240
241
#### Pattern: Create Thumbnails
242
243
```bash
244
filename=$(basename "$img")
245
magick "$img" -resize 427x240 "thumbnails/thumb_$filename"
246
```
247
248
## Limitations
249
250
- Large batch operations may be memory-intensive
251
- Some complex operations may require additional ImageMagick delegates
252
- On older Linux systems, use `convert` instead of `magick` (ImageMagick 6.x vs 7.x)
253