sodcalculator

10 Tech How-To

Resize & Convert Images in Linux Terminal

Guide 10-48237 · Updated · Step by step

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: exiftool for metadata work, ffmpeg for video frames, and optipng/jpegoptim for compression.

Step-by-Step

  1. Check if ImageMagick is installed
    Run convert --version or magick --version (newer versions). If not found, install it: sudo apt install imagemagick (Debian/Ubuntu) or sudo dnf install imagemagick (Fedora).
  2. Resize an image
    Use convert 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).
  3. Resize by percentage
    Use convert input.png -resize 50% output.png to halve the dimensions. This is handy for batch processing thumbnails.
  4. Convert between formats
    Simply change the extension: convert image.png image.jpg. For WebP, use convert image.png image.webp. To control quality, add -quality 85 (for JPEG/WebP) before the output file.
  5. Batch process multiple files
    Use a loop: for img in *.jpg; do convert "$img" -resize 800 "resized-$img"; done. This creates new files with the resized- prefix, leaving originals intact.
  6. Rotate and flip
    Rotate 90° clockwise: convert input.jpg -rotate 90 output.jpg. Flip horizontally: convert input.jpg -flop output.jpg. Vertical flip: -flip.
  7. Adjust quality and compression
    For JPEG, use -quality 80 (0-100, lower = smaller file). For PNG, use -strip to remove metadata and -define png:compression-level=9 for maximum compression.
  8. Crop an image
    Use convert input.jpg -crop 400x300+50+20 output.jpg to crop a 400×300 area starting at x=50, y=20. Add +repage after crop to reset the page offset.
  9. Add text or watermarks
    Use convert input.jpg -pointsize 36 -annotate +20+50 "Copyright" output.jpg to add text at position (20,50). For a semi-transparent watermark, combine with -fill rgba(255,255,255,0.5).
  10. Convert to grayscale or apply effects
    Grayscale: convert input.jpg -colorspace Gray output.jpg. Sepia: -sepia-tone 80%. Blur: -blur 0x2.
  11. Use mogrify for 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.
  12. Get image information
    Use identify input.jpg to see dimensions, format, and color space. For detailed metadata, use identify -verbose input.jpg.

Common Mistakes

  • Overwriting originals accidentally—mogrify modifies files in place. Always work on copies or use convert with 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 +repage to fix.

Tips

  • Use find with -exec for complex batch jobs: find . -name "*.jpg" -exec convert {} -resize 800 {} \; (overwrites).
  • Parallelize with xargs -P 4 to speed up large batches: ls *.jpg | xargs -P 4 -I {} convert {} -resize 800 resized-{}.
  • For web use, always strip metadata with -strip to reduce file size and remove EXIF data.
  • Combine operations: convert input.jpg -resize 800 -quality 85 -strip output.jpg.
  • Use mogrify -path outdir to send output to a different directory, keeping originals safe.
  • Check the result with ls -lh to 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 is magick instead of convert.
  • “unable to open image”—Check the file path and permissions. Use ls -l to verify the file exists and is readable.
  • Output is black or corrupted—The input file might be corrupted or in an unsupported format. Try identify to see if ImageMagick can read it. Convert to a known format first.
  • Resize doesn’t work as expected—Remember that -resize preserves 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 sudo only 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

The whole Tech How-To bed →