20

I have huge, 12GB, gallery on the server, full of images in various subfolders. Those files are too big and not used in full resolution. I need to resize all the images down to 820px wide (keeping proportions). So my question is - how can I create some kind of crawling script which would resize all images bigger then 820px and save them back overwriting original file?

Hope you can help me :-) Thank you in advance.

1
  • 4
    find . -name "*.jpg" -exec mogrify -resize 820x {} + will do them all; if you don't want to process the "less than 820px" ones, there are some options here. Commented Apr 15, 2015 at 14:00

2 Answers 2

26

ImageMagick tools convert or mogrify will do the job.

You can get them via your package manager or the source/rpms here.

Basic usage (will overwrite the original file):

$ mogrify -resize 820x ./*.jpg

If you need recursion:

find . -name '*.jpg' -execdir mogrify -resize 820x {} +
7
  • 1
    I don't think that "820px wide (keeping proportions)" means "resizing to 820x820". It's rather more like "820px wide and whatever-the-proportion-is high" ;) Plus this does not handle recursiveness. Commented Apr 15, 2015 at 15:42
  • 1
    Good point. Edited! Commented Apr 15, 2015 at 19:22
  • 2
    If you want to make it so that it only resizes if they are larger than set dimensions and still keep the ratios, you can change it to this: find . -name '*.jpg' -execdir mogrify -resize '820x620>' {} \; Commented Dec 8, 2018 at 22:30
  • 5
    Caution: This will overwrite your existing images. Commented Aug 9, 2019 at 21:31
  • Related: How do I control the compression rate of mogrify? (Super User) Commented Nov 29, 2019 at 19:37
0

The script to resize all .png files in a folder to 30%. The original file can either be removed (if delete_original is true), or stays, in that case the resized file would get a name postfix rename_postfix:

#!/bin/bash

rename_postfix="-small"
delete_original=true
substr=".png"
for str in *.png; do
    #reverse strings
    reverse_str=$(echo $str | rev)
    reverse_substr=$(echo $substr | rev)
    
    #find index of reversed substring in reversed string
    prefix=${reverse_str%%$reverse_substr*}
    reverse_index=${#prefix}
    
    #calculate last index
    index=$(( ${#str} - ${#substr} - $reverse_index ))
    
    # Extract the filename without extension
    filename="${str:0:index}"
    if $delete_original; then
        # the name can remain, because the original will be removed
        new_filename="${str}"
    else
        # need a new name, two files will remain
        new_filename="${filename}${rename_postfix}${substr}"
    fi

    echo "extension = ${substr}, original = ${str}, filename = ${filename}, new = ${new_filename}"
    
    # Resize and convert the file to .jpg
    convert -resize 30% "${str}" "${new_filename}"
done

I think you should be able to write a function with the above body that would accept a directory argument, etc (if you want to make it go into subfolders)

The convert function is from the imagemagick library:

sudo apt install imagemagick

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.