Image Resizer 0.2

Here you can discuss every aspect of Debian. Note: not for support requests!

Image Resizer 0.2

Postby canci » 2011-02-02 18:47

.

I made an Image Resizer in bash which requires Imagemagick and Zenity.

USAGE:

Copy the following code and save it as a text file (e.g. resize_img.sh) and make it executable.

Code: Select all
#!/bin/bash
#
# resize_img.sh  --  An Imagemagick Resize Script
#
#
# Author: canci <cancivolonter@gmail.com>
# License: GPL. See http://www.gnu.org/licenses/gpl.html
#
# Dependencies: Bash (duh!), Imagemagick, zenity
#
#


# 1. IFS and other delcarations
#

# Saving the old one
#
SAVEIFS=$IFS

# Declaring the new one
#
IFS=$(echo -en "\n\b")

# Files
#
FILES=`ls -1 $@`

# Directory name in case files are not being overwritten
#
NEW_DIR=RESIZED__`date +%d-%b-%Y_%T`

# Integer file count for the progress bar
#
n=1



# 2. Get desired resolution; Exit if cancel button pressed, otherwise move on
#
RES=`zenity --title="Please enter a resoultion" --entry --text="Image Resizer -- Please enter a resoultion (e.g. 800x600)"`
   if [ $? == 1 ]; then

      exit
      
   else

# 2.1. Should the resize disregard the current aspect ratio and resize strictly to the given resolution?
#
      Q1=`zenity --title="Disregard aspect ratio?" --question --text="Disregard aspect ratio? If Yes, the images will be resized to the exact resolution you provided, which might skew images if the aspect ratio differs. If No, the aspect ratio will be preserved and only the width will be resized accurately."`
         if [ $? == 0 ]; then

# 2.1.1. Should the file be overridden or should a new one be created? mogrify or convert will be used accordingly.
#
            Q2=`zenity --title="Overwrite file?" --question --text="Overwrite file? If No, the resized images will be put inside a new directory called RESIZED with the date of creation appended to it."`

               if [ $? == 0 ]; then

                    for f in $FILES
                    do

                     mogrify -resize `echo $RES`! $f
                     echo $n
                     echo "# Processing file: $f"
                     let "n = n+1"
                     
                    done | (zenity  --progress --title "Resizing..." --percentage=0 --auto-close --auto-kill)

                    zenity --info --text="Done!"

               else
         
                    mkdir $NEW_DIR
                    for f in $FILES
                    do

                     convert -resize `echo $RES`! $f $NEW_DIR/$f
                     echo $n
                     echo "# Processing file: $f"
                     let "n = n+1"
                     
                    done | (zenity  --progress --title "Resizing..." --percentage=0 --auto-close --auto-kill)

                    zenity --info --text="Done!"

               fi
      
      
# WHAT ELSE? HUH?      

         else



# 2.1.2. Same as in 2.1.1., but for aspect ratio resize.
#
            Q3=`zenity --title="Overwrite file?" --question --text="Overwrite file? If No, the resized images will be put inside a new directory called RESIZED with the date of creation appended to it."`


               if [ $? == 0 ]; then

                    for f in $FILES
                    do
            
                     mogrify -resize $RES $f
                     echo $n
                     echo "# Processing file: $f"
                     let "n = n+1"

                    done | (zenity  --progress --title "Resizing..." --percentage=0 --auto-close --auto-kill)

                    zenity --info --text="Done!"

               else

                    mkdir $NEW_DIR
                    for f in $FILES
                    do         

                     convert -resize $RES $f $NEW_DIR/$f
                     echo $n
                     echo "# Processing file: $f"
                     let "n = n+1"
                     
                    done | (zenity  --progress --title "Resizing..." --percentage=0 --auto-close --auto-kill)
            
                    zenity --info --text="Done!"
            
               fi
      
# Ending 2.1.
         fi


# Ending 2
   fi
   
   
# 3. Restore IFS (for possible future use)
#

IFS=$SAVEIFS

# EOF
exit 0


Then just copy it into the path where you keep your executables (/usr/local/bin or ~/.bin). When you start the script, it will ask you

1. what resolution you want
2. whether you want to resize according to aspect ratio or if you want to disregard that
3. if you want to overwrite the files or make new ones in a subdirectory called RESIZED
4. it will show a nice progress bar

Image

It's meant to be called as a custom action from Thunar (please use %N, not %F, or else it won't work!; instructions here) or Nautilus (no clue how it's installed there, so, if anyone can help?), where you would mark the files, right click and call the script via a custom action. You could mark one, or several. It does not work on directories though, meaning that it won't consider files inside directories. But you can just call it from the command line, with just one file or with many:


Code: Select all
resize_img.sh cow.png

Will only resize cow.png

Code: Select all
resize_img.sh *

will convert all the images in the directory.

Code: Select all
resize_img.sh 1.jpg 2.jpg foo.png

will convert images with those names

Code: Select all
resize_img.sh *.jpg

will convert only .jpgs

Code: Select all
resize_img.sh screenshot*

will convert only images with names starting with screenshot

etc.



The code is GPL.


I hope you'll enjoy it.


CHANGELOG

v0.2 --- 3rd Feb 2011 --- If you choose not to overwrite your files, a directory is created with the name RESIZED_$DATE to prevent the script from overwriting images in an existing RESIZED directory (Big thanks to nadir for this suggestion).
Last edited by canci on 2011-02-03 12:41, edited 2 times in total.
I ken hez kinvulf beck? I ken hez riesnible modereyshn? Kthnxbai!

No need to discuss this topic? Offended by this signature? Remove it:

Mez wrote:You can easily just disable viewing signatures

Click here - and set "Display Signatures" to No
User avatar
canci
 
Posts: 1705
Joined: 2006-09-24 11:28
Location: Bosnia & Herzegovina

Re: Image Resizer 0.2

Postby canci » 2011-02-03 12:35

UPDATE

v0.2 --- 3rd Feb 2011 --- If you choose not to overwrite your files, a directory is created with the name RESIZED_$DATE to prevent the script from overwriting images in an existing RESIZED directory (Big thanks to nadir for this suggestion).
I ken hez kinvulf beck? I ken hez riesnible modereyshn? Kthnxbai!

No need to discuss this topic? Offended by this signature? Remove it:

Mez wrote:You can easily just disable viewing signatures

Click here - and set "Display Signatures" to No
User avatar
canci
 
Posts: 1705
Joined: 2006-09-24 11:28
Location: Bosnia & Herzegovina

Re: Image Resizer 0.2

Postby secipolla » 2011-04-04 21:53

It gives a syntax error here (unexpected token 'then'):
Code: Select all
/usr/local/bin/resize-img.sh: line 42: erro de sintaxe próximo do `token' não esperado `then'
/usr/local/bin/resize-img.sh: line 42: `   if [ $? == 1 ]; then'
secipolla
 
Posts: 1127
Joined: 2010-06-21 14:20

Re: Image Resizer 0.2

Postby aicardi » 2011-04-05 18:11

This is what i use with thunar actions.
Code: Select all
#!/bin/sh                                                                                                   

mkdir -p ./Resized/$1             
for file
    do
   if [ ! -e "$file" ]
       then
       continue
   fi
    name=$( echo $file | cut -f1 -d.)
    convert -geometry $1x$1 -quality 100 $file ./Resized/$1/${name}_$1.jpg
done

Highlight any or all images in a directory.
it will create a directory called Resized.
It will then create a directory with the desired sized images. eg 640
It will append the image size to the filename. eg image_640
Jessie/Xfce
User avatar
aicardi
 
Posts: 388
Joined: 2009-11-18 01:30
Location: Chicago


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable