Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #recursive convert include subdirs
- # Set convert ratio on %
- ratio="2048x2048";
- res="0";
- max_px="2047"
- ident="/usr/bin/identify";
- optns=" -interlace line -filter Lanczos";
- ###################
- function convert_img() {
- echo "Entering dir $new_name";
- cd "$new_name";
- ls -1 | while read name;
- do
- # echo "Working on $name";
- # sleep 1s;
- # FILES
- START_F=$(date +%s)
- if [ -f "$name" ]; then
- if [ "${name##*.}" == "jpg" ]; then
- cur_res=$( $ident -format %wx%h "$name" );
- cur_h=$( $ident -format %h "$name" );
- cur_w=$( $ident -format %w "$name" );
- cur_size=$(stat -c%s "$name");
- echo "Current $name resolution: $cur_res size: $cur_size";
- if [ "$cur_h" -gt "$max_px" ]; then
- # echo "Is hight $cur_h >= $max_px";
- let res="1";
- elif [ "$cur_w" -gt "$max_px" ]; then
- # echo "Is width $cur_w > $max_px";
- let res="1";
- fi;
- if [ $res -gt 0 ]; then
- echo " $cur_res is greater then $max_px Resizing...";
- res=0;
- # echo "convert $name $optns -resize $ratio -quality 95 tmp_$name";
- # echo "Converting...";
- convert "$name" $optns -resize $ratio -quality 90 "tmp_$name";
- # replace the original file ONLY IF the resizing was successful
- if [ `echo $?` == "0" ]; then
- # replace the original file with the scaled file
- rm -f "$name"
- if [ $? -eq "0" ]; then
- mv "tmp_$name" "$name"
- cur_res=$( $ident -format %wx%h "$name" );
- new_size=$(stat -c%s "$name");
- diff_size=$(( $cur_size - $new_size ))
- echo "New $name resolution: $cur_res size: $new_size diff: $diff_size";
- fi
- else
- echo "Failed to resize image $name"
- fi;
- fi;
- else
- echo "NOTHING TO DO!";
- fi;
- END_F=$(date +%s)
- DIFF_F=$(( $END_F - $START_F ))
- echo "It took $DIFF_F seconds"
- # DIRS
- elif [ -d "$name" ]
- then
- # Run function
- new_name="$name";
- echo $new_name;
- local START_D=$(date +%s)
- convert_img
- local END_D=$(date +%s)
- local DIFF_D=$(( $END_D - $START_D ))
- echo "It took $DIFF_D seconds"
- else
- echo "Exiting dir $new_name";
- fi
- done;
- cd ../;
- }
- ##############
- ls -1 | while read new_name;
- do
- echo "Working on $new_name";
- if [ -d "$new_name" ];
- then
- convert_img
- fi
- done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement