Advertisement
Guest User

marchelly

a guest
Feb 4th, 2013
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.59 KB | None | 0 0
  1. #!/bin/bash
  2. #recursive convert include subdirs
  3. # Set convert ratio on %
  4. ratio="2048x2048";
  5. res="0";
  6. max_px="2047"
  7. ident="/usr/bin/identify";
  8. optns=" -interlace line -filter Lanczos";
  9. ###################
  10.  
  11. function convert_img() {
  12.                 echo "Entering dir $new_name";
  13.                 cd "$new_name";
  14.                         ls -1 | while read name;
  15.                         do
  16. #                       echo "Working on $name";
  17. #                       sleep 1s;
  18. # FILES
  19.                         START_F=$(date +%s)
  20.                                 if [ -f "$name" ]; then
  21.                                         if [ "${name##*.}" == "jpg" ];  then
  22.                                         cur_res=$( $ident -format %wx%h "$name" );
  23.                                         cur_h=$( $ident -format %h "$name" );
  24.                                         cur_w=$( $ident -format %w "$name" );
  25.                                         cur_size=$(stat -c%s "$name");
  26.                                         echo "Current $name resolution: $cur_res size: $cur_size";
  27.                                                 if [ "$cur_h" -gt "$max_px" ]; then
  28. #                                               echo "Is hight $cur_h >= $max_px";
  29.                                                 let res="1";
  30.                                                 elif [ "$cur_w" -gt "$max_px" ]; then
  31. #                                               echo "Is width $cur_w > $max_px";
  32.                                                 let res="1";
  33.                                                 fi;
  34.  
  35.                                                 if [ $res -gt 0 ]; then
  36.                                                 echo " $cur_res is greater then $max_px Resizing...";
  37.                                                 res=0;
  38. #                                               echo "convert $name $optns -resize $ratio -quality 95 tmp_$name";
  39. #                                               echo "Converting...";
  40.                                                 convert "$name" $optns -resize $ratio -quality 90 "tmp_$name";
  41.                                                 # replace the original file ONLY IF the resizing was successful
  42.                                                         if [ `echo $?` == "0" ]; then
  43.                                                         # replace the original file with the scaled file
  44.                                                         rm -f "$name"
  45.                                                                 if [ $? -eq "0" ]; then
  46.                                                                 mv "tmp_$name" "$name"
  47.                                                                 cur_res=$( $ident -format %wx%h "$name" );
  48.                                                                 new_size=$(stat -c%s "$name");
  49.                                                                 diff_size=$(( $cur_size - $new_size ))
  50.                                                                 echo "New $name resolution: $cur_res size: $new_size diff: $diff_size";
  51.                                                                 fi
  52.                                                         else
  53.                                                         echo "Failed to resize image $name"
  54.                                                         fi;
  55.                                                 fi;
  56.                                         else
  57.                                         echo "NOTHING TO DO!";
  58.                                         fi;
  59.                                 END_F=$(date +%s)
  60.                                 DIFF_F=$(( $END_F - $START_F ))
  61.                                 echo "It took $DIFF_F seconds"
  62. # DIRS
  63.                                 elif [ -d "$name" ]
  64.                                 then
  65. # Run function
  66.                                 new_name="$name";
  67. echo $new_name;
  68.                                 local START_D=$(date +%s)
  69.                                 convert_img
  70.                                 local END_D=$(date +%s)
  71.                                 local DIFF_D=$(( $END_D - $START_D ))
  72.                                 echo "It took $DIFF_D seconds"
  73.  
  74.                                 else
  75.                                 echo "Exiting dir $new_name";
  76.                                 fi
  77.                         done;
  78.                         cd ../;
  79. }
  80. ##############
  81.  
  82. ls -1 | while read new_name;
  83. do
  84.         echo "Working on $new_name";
  85.                 if [ -d "$new_name" ];
  86.                 then
  87.                 convert_img
  88.                 fi
  89. done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement