Quick tip: Batch resize and convert images in Linux

Image magick icon
You can batch resize and convert images in Linux from the terminal using ImageMagick. The first step is to install ImageMagick. You can download the ImageMagick from the ImageMagick homepage or ImageMagick is widely available in the repositories and can be downloaded using apt-get install imagemagick.

There are a ton of commands available from the ImageMagick guide. I will walk you through the most commonly used commands: resize, convert, and quality (compression).

Enter the following command to resize all JPEG images in a directory to 750×500 pixels. Obviously, you can change the 750×500 to whatever dimensions you prefer:
convert -resize 750×500 *.jpg

Enter the following command to convert a JPEG into a GIF. You can convert any format image, it doesn’t have to just be JPEG to GIF:
convert image.jpg newImage.gif

Enter the following command to reduce the quality of every JPEG in a directory down to 80%. You can change the 80% to whatever percentage you prefer:
convert -quality 80% *.jpg

Please visit the ImageMagick homepage to learn about the thousands of other commands available.

Note: You can also combine commands.  For example:
convert -resize 750×500 -quality 80% *.jpg

Trackbacks & Pings

Comments

  1. sorry but this command:

    convert -resize 750×500 -quality 80% *.jpg

    only convert the last jpeg file, not all them.

    I’m on Kubuntu edgy.

  2. try it like this
    save the following into a simple script file and edit as desired, save, ensure it’s executable, and then run

    for i in ls -C1 *.jpg
    do
    convert -verbose -resize 600×900 -comment “(c) Paul Sahlin Tiffany’s” $i optional_preffix_$i_optional_suffix
    done

  3. you can also use

    mogrify -resize 400×400 ./*.jpg

    that’s what i had to do on mandriva 2007.1

  4. And what would be the command to convert *.gif in a directory into .jpg files?

  5. @Evert Meulie: That’s one of the very clever things about ImageMagick. You just use the extension of the file that you want it to be converted to. Example:

    $ convert img1.gif img1.jpg
    $ for img in *gif; do
    > base=`basename $img .gif`
    > convert “$img” “$base.jpg”
    > done

  6. I have no idea what “convert -quality 30% *.JPG” does on my machine right now. The only thing I know is that it’s consuming all my system’s ressources and doesn’t even touch my JPGs…
    It works quite well with 1 file, but the wildcard gets it to bug around…

  7. Resize Multiple Images chek this link:

    http://albertux.ayalasoft.com/2008/10/20/resize-multiple-images/

  8. I fancy this command, which i have “invented” for several purposes :-)

    ls *.jpg | tr ‘\n’ ‘00′ | xargs -0 -i{} convert -resize 800×600 “{}” “resampled/{}”

  9. When i ran the command:
    convert -resize 576×576 *.jpg
    it took a while to convert all the *.jpg files
    but they are renamed as
    lastFileName-0.jpg
    lastFileName-1.jpg
    lastFileName-3.jpg …and so on.

  10. if you want to restrain only by height (as i wanted) this works :D

    mogrify -resize ×100 *.jpg

  11. I just love how Wordpress decides it must mangle text to be pretty typographically. All of the “x”s (in 400×400, etc.)(as well as all the single and double quotes) in this article and comments are “broken”, and have to be replaced with their plain ASCII versions.

  12. Hi,
    I need a help to run this batch file using php exec in linux environment.

    Thanks,
    Vijay

  13. keep it easy

    for multiple file conversion that works

    mogrify -resize 25% *.jpg

    simple

  14. This is brilliant! Now I can resize all my photos like a pro Linux user.

    This page is soo bookmarked!

  15. Great page, thank you

    It’s much better to do a loop than let convert deal with the wild cards on the command line

    for ff in *.jpg
    do
    convert -verbose -resize 50% -quality 75% $ff X_$ff
    done

    Just saved my day. :-)

    Remember always work over with copies of the images

  16. The script written by Abschnitt5 works fine. Thanks.

  17. WARNING:

    for does not work with files that have spaces in their names, in such case use:

    find -type f -name “*.jpg” -exec convert ‘-verbise -resize 50% -quality 75%’ ‘{}’ X_’{}’ \;

    see man find

  18. Hi, I am having problems with the following command, run in a bash shell (ubuntu 12.1 desktop):

    for i in ls -C1 *.jpg
    do
    convert -resize 480×270>
    done

    error: convert.im6: option requires an argument `-resize’ @ error/convert.c/ConvertImageCommand/2380.

    No matter if I quote the resize parameters, escaped or non, i recieve the same error. Any help is appreciated.

  19. sorry i fixed that myself. stupid, was missing my bash variables.

Post a Comment


Your email is never published nor shared. Required fields are marked *



© 2006-2007 Maxim Software Corp.  All rights reserved.