This guide shows you how to resize, convert, and modify images directly from the Linux command line using tools like ImageMagick and others. You’ll learn fast, scriptable methods that work on single files or entire folders, without opening a GUI editor.
What You’ll Need
- A Linux system with a terminal.
- ImageMagick installed (most distros include it; if not, install via your package manager, e.g.,
sudo apt install imagemagick). - Optional:
exiftoolfor metadata work,ffmpegfor video frames, andoptipng/jpegoptimfor compression.
Step-by-Step
- Check if ImageMagick is installed
Runconvert --versionormagick --version(newer versions). If not found, install it:sudo apt install imagemagick(Debian/Ubuntu) orsudo dnf install imagemagick(Fedora). - Resize an image
Useconvert input.jpg -resize 800x600 output.jpg. This changes the image to exactly 800×600, possibly distorting it. To keep the aspect ratio, specify only one dimension:-resize 800(width) or-resize x600(height). - Resize by percentage
Useconvert input.png -resize 50% output.pngto halve the dimensions. This is handy for batch processing thumbnails. - Convert between formats
Simply change the extension:convert image.png image.jpg. For WebP, useconvert image.png image.webp. To control quality, add-quality 85(for JPEG/WebP) before the output file. - Batch process multiple files
Use a loop:for img in *.jpg; do convert "$img" -resize 800 "resized-$img"; done. This creates new files with theresized-prefix, leaving originals intact. - Rotate and flip
Rotate 90° clockwise:convert input.jpg -rotate 90 output.jpg. Flip horizontally:convert input.jpg -flop output.jpg. Vertical flip:-flip. - Adjust quality and compression
For JPEG, use-quality 80(0-100, lower = smaller file). For PNG, use-stripto remove metadata and-define png:compression-level=9for maximum compression. - Crop an image
Useconvert input.jpg -crop 400x300+50+20 output.jpgto crop a 400×300 area starting at x=50, y=20. Add+repageafter crop to reset the page offset. - Add text or watermarks
Useconvert input.jpg -pointsize 36 -annotate +20+50 "Copyright" output.jpgto add text at position (20,50). For a semi-transparent watermark, combine with-fill rgba(255,255,255,0.5). - Convert to grayscale or apply effects
Grayscale:convert input.jpg -colorspace Gray output.jpg. Sepia:-sepia-tone 80%. Blur:-blur 0x2. - Use
mogrifyfor in-place batch edits
To resize all JPEGs in a folder (overwrites originals):mogrify -resize 800 *.jpg. To convert all PNGs to JPEG:mogrify -format jpg *.png. Be careful—this modifies original files; test on copies first. - Get image information
Useidentify input.jpgto see dimensions, format, and color space. For detailed metadata, useidentify -verbose input.jpg.
Common Mistakes
- Overwriting originals accidentally—
mogrifymodifies files in place. Always work on copies or useconvertwith a new output name. - Ignoring aspect ratio—Specifying both width and height without
!will distort the image. Use-resize 800x600!to force exact dimensions, or only one dimension to keep proportions. - Forgetting to escape special characters in filenames—Spaces and parentheses break commands. Quote filenames:
convert "my image.jpg" out.png. - Using the wrong quality value—For PNG, quality means compression level (0-100) but not visual quality; PNG is lossless. For JPEG, quality directly affects file size and clarity.
- Not resetting page after crop—Cropping can leave a virtual canvas offset; add
+repageto fix.
Tips
- Use
findwith-execfor complex batch jobs:find . -name "*.jpg" -exec convert {} -resize 800 {} \;(overwrites). - Parallelize with
xargs -P 4to speed up large batches:ls *.jpg | xargs -P 4 -I {} convert {} -resize 800 resized-{}. - For web use, always strip metadata with
-stripto reduce file size and remove EXIF data. - Combine operations:
convert input.jpg -resize 800 -quality 85 -strip output.jpg. - Use
mogrify -path outdirto send output to a different directory, keeping originals safe. - Check the result with
ls -lhto see file sizes before/after.
Troubleshooting
- Command not found—Install ImageMagick or use the full path (e.g.,
/usr/bin/convert). On some systems, the command ismagickinstead ofconvert. - “unable to open image”—Check the file path and permissions. Use
ls -lto verify the file exists and is readable. - Output is black or corrupted—The input file might be corrupted or in an unsupported format. Try
identifyto see if ImageMagick can read it. Convert to a known format first. - Resize doesn’t work as expected—Remember that
-resizepreserves aspect ratio unless you use!. If you need exact dimensions, use-resize 800x600!. - Batch loop fails on filenames with spaces—Always quote variables:
for img in *.jpg; do convert "$img" ...; done. - Permission denied—Ensure you have write permission to the output directory. Use
sudoonly if necessary, but prefer changing directory permissions.
Frequently Asked Questions
Can I resize an image without losing quality?
Downscaling (making smaller) always loses some detail, but using a high-quality filter like -filter Lanczos can help. Upscaling cannot create new detail; it just interpolates.
How do I convert HEIC to JPEG?
ImageMagick may not support HEIC by default. Install libheif-examples and use heif-convert input.heic output.jpg, or use convert input.heic output.jpg if ImageMagick is compiled with HEIC support.
What’s the difference between convert and mogrify?
convert creates a new output file, leaving the original untouched. mogrify modifies files in place (or writes to a specified directory with -path). Use convert for single files, mogrify for batch edits.
How do I batch convert all PNGs to JPGs?
Use mogrify -format jpg *.png. This creates a .jpg for each .png, but leaves the original .png files. To remove originals, delete them after verifying the output.
Can I resize an image to a specific file size?
ImageMagick doesn’t have a direct “target file size” option. You can loop, adjusting quality until the size is under a limit, or use -define jpeg:extent=100kb for JPEG output (ImageMagick 6.9.8+).
Is there a way to preview changes before saving?
Yes, use display output.jpg to view the result. For a quick check, you can also create a small thumbnail and open it.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Gmail Notifications Not Working? Fixes That Work
- Fix Thumbnails Not Showing on Network Drives in Windows 11
- Stop Skype Auto-Start in Windows: 5 Fixes
- SQL Injection Prevention: Step-by-Step Guide
- Fix Windows PowerShell Popping Up
- Print iMessage or Text Conversations on iPhone & iPad