Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Script/command to resize images?

  1. #1
    Join Date
    Jan 2012
    Beans
    753

    Script/command to resize images?

    I have a large collection of images, but I'd like to be able to shrink them to under or equal to a maximum size (in both dimensions and file size). Is there a command/set of commands I could use to do this? I have to be able to shrink every image to under 500kb, and with dimensions with less or equal to 480,000 pixels (the amount in a 800x600 image). What can I do to automate this, because doing it manually would take rougly 80 days at the speed I'd do it? Is there some way I could do it in a script with GraphicsMagick?

    Thanks.

  2. #2
    Join Date
    Oct 2004
    Location
    Switzerland
    Beans
    60

    Re: Script/command to resize images?

    Have a look here:
    http://www.imagemagick.org/script/mogrify.php

    I use mogrify all the time.

    Hope that helps

  3. #3
    Join Date
    Feb 2013
    Beans
    6

    Re: Script/command to resize images?

    You can use nautilus for this job.
    Install the package nautilus-image-converter
    Then you can select images, right clic and choose 'resize image'

  4. #4
    Join Date
    Jan 2012
    Beans
    753

    Re: Script/command to resize images?

    Quote Originally Posted by guyr34 View Post
    You can use nautilus for this job.
    Install the package nautilus-image-converter
    Then you can select images, right clic and choose 'resize image'
    Can that automatically resize only images larger than 480,000 pixels to that size or less automatically? I don't want it to try and resize smaller images to that size, or re-encode images at that size, reducing the quality unnecessarily.

  5. #5
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,906
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script/command to resize images?

    Quote Originally Posted by Fintan View Post
    I use mogrify all the time.
    +1

    First, backup your data because any command line error may be risky. Then:
    Code:
    mogrify -resize 800x600 *
    Regards.

  6. #6
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    9,041
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Script/command to resize images?

    I prefer to use convert instead of mogrify and write the altered images to a separate directory. Then if everything looks correct, you can delete the originals.

    I posted a script here that rotates files in the portrait orientation to landscape and leaves the ones already in the landscape orientation alone. With a couple of tweaks you could adapt it to your needs.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  7. #7
    Join Date
    Jan 2012
    Beans
    753

    Re: Script/command to resize images?

    Quote Originally Posted by papibe View Post
    +1

    First, backup your data because any command line error may be risky. Then:
    Code:
    mogrify -resize 800x600 *
    Regards.
    I don't want to convert everything to exactly 800x600! I want to scale any images that are larger than 480,000 pixels (the equivalent of 800x600) to that size or lower. For example, if an image is 2400x800, it should be resized to 1200x400, not 800x600 (even though they have the same amount of pixels). But if it's 400x300, it should be left alone.

    I tried using "gm identify", but I can't seem to get it to output only the dimensions, and I can't use "awk {'print $number'}" because it won't work correctly if there's a space in the path.

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,403
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script/command to resize images?

    Code:
    #!/bin/bash
    
    src=$HOME/Pictures
    dest=$HOME/resized_files
    limit=480000
    
    mkdir -p "$dest"
    
    for f in "$src"/*.{jpg,png}
    do
      dim=$( identify "$f" | grep -Pow '[0-9]+x[0-9]+' | head -1 )
      w=${dim%x*}
      h=${dim#*x}
    #  echo width:$w height:$h $((w*h))
      if (( w*h>limit ))
      then
        echo "* resizing '$f'"
        echo "width:$w, height:$h, pixel count:$((w*h))"
        scale=$( echo "sqrt( $limit/($w*$h))*100" | bc -l )
        scale=${scale:0:6}%
        echo scale:$scale
        rf=$dest/${f##*/}
        convert -scale "$scale" "$f" "$rf"
        rdim=$( identify "$rf" | grep -Pow '[0-9]+x[0-9]+' | head -1 )
        rw=${rdim%x*}
        rh=${rdim#*x}
        echo "* resized file '$rf'"
        echo "new width:$rw, new height:$rh, pixel count:$((rw*rh))"
        echo
      fi
    done
    i don't know what would happen when the img should be scaled to 2/3 of the original size (tidy fraction but infinite decimal form). Loss of precision from Sqrt() and trimming the $scale var might appear. You might want to try to use more digits ( scale=${scale:0:6}% ) or even skip trimming entirely scale=${scale}% )
    Last edited by Vaphell; March 17th, 2013 at 11:29 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    Jan 2012
    Beans
    753

    Re: Script/command to resize images?

    Quote Originally Posted by Vaphell View Post
    Code:
    #!/bin/bash
    
    src=$HOME/Pictures
    dest=$HOME/resized_files
    limit=480000
    
    mkdir -p "$dest"
    
    for f in "$src"/*.{jpg,png}
    do
      dim=$( identify "$f" | grep -Pow '[0-9]+x[0-9]+' | head -1 )
      w=${dim%x*}
      h=${dim#*x}
    #  echo width:$w height:$h $((w*h))
      if (( w*h>limit ))
      then
        echo "* resizing '$f'"
        echo "width:$w, height:$h, pixel count:$((w*h))"
        scale=$( echo "sqrt( $limit/($w*$h))*100" | bc -l )
        scale=${scale:0:6}% 
        echo scale:$scale
        rf=$dest/${f##*/}
        convert -scale "$scale" "$f" "$rf"
        rdim=$( identify "$rf" | grep -Pow '[0-9]+x[0-9]+' | head -1 )
        rw=${rdim%x*}
        rh=${rdim#*x}
        echo "* resized file '$rf'"
        echo "new width:$rw, new height:$rh, pixel count:$((rw*rh))"
        echo
      fi
    done
    i don't know what would happen when the img should be scaled to 2/3 of the original size (tidy fraction but infinite decimal form). Loss of precision from Sqrt() and trimming the $scale var might appear. You might want to try to use more digits ( scale=${scale:0:6}% ) or even skip trimming entirely scale=${scale}% )
    How do I get it to search recursively? I think this would work but it seems really sloppy:
    Code:
    for f in "$src"/*.{jpg,png} "$src"/*/*.{jpg,png} "$src"/*/*/*.{jpg,png} "$src"/*/*/*/*.{jpg,png}; do
    # code
    done
    Is there a better way to do that?

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,403
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script/command to resize images?

    change to recursive mode is easy, i'll change it. i assume the output dir structure should reflect the structure of the source?

    Code:
    #!/bin/bash
    
    shopt -s globstar
    
    src=$HOME/Obrazy
    dest="$HOME/resized_files"
    limit=480000
    
    mkdir -p "$dest"
    
    
    for f in "$src"/**/*{jpg,png}
    do
      dim=$( identify "$f" | grep -Pow '[0-9]+x[0-9]+' | head -1 )
      w=${dim%x*}
      h=${dim#*x}
      if (( w*h>limit ))
      then
        echo "* resizing '$f'"
        echo "width:$w, height:$h, pixel count:$((w*h))"
        scale=$( echo "sqrt( $limit/($w*$h))*100" | bc -l )
        scale=${scale:0:6}%
        echo scale:$scale
        rf=$dest/${f#$src/}
        convert -scale "$scale" "$f" "$rf"
        rdim=$( identify "$rf" | grep -Pow '[0-9]+x[0-9]+' | head -1 )
        rw=${rdim%x*}
        rh=${rdim#*x}
        echo "* resized file '$rf'"
        echo "new width:$rw, new height:$rh, pixel count:$((rw*rh))"
        echo
      fi
    done
    globstar ** is a bashism that matches 0 or more directories
    alternative would be to use find + while read

    Code:
    while read -rd $'\0' f
    do
       ...
    done < <( find "$src" ... -print0 )
    in case you have images with capitalized extensions (digital cameras often do that) you can use shopt -s nocaseglob so you don't have to explicitly list capitalized forms *{jpg,JPG,png,PNG} and all possible variations Jpg Png etc. or write the globs this way:
    Code:
    for f in "$src"/**/*.[jJ][pP][gG] "$src"/**/*.[pP][nN][gG]
    Last edited by Vaphell; March 17th, 2013 at 12:48 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •