Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: [EN] Who could help: Script to resize and watermark images  (Read 7666 times)

Offline Lanzi

  • User
  • Posts: 1.775
[EN] Who could help: Script to resize and watermark images
« on: 2016/03/09, 00:31:52 »
I usually resize and watermakr imaghes on the console, but every step savesd the jpg and I think it would be wise only to save at the end to have better quality.

So I would like to have a script that does the following:

1. Change JPG to jpg
for file in *.JPG; do mv $file ${file%.*}.jpg; done

2. Resize:
for i in *.jpg; do convert -resize 1200x1200 -quality 80 $i `basename $i .jpg`-.jpg; done

3. Add watermark 1:
for i in *.jpg; do composite -gravity SouthEast /path/to/watermark1.png $i `basename $i .jpg`-water.jpg; done


4. Add Watermark 2
for i in *.jpg; do composite -gravity Southwest /path/to/watermark2.png $i `basename $i .jpg`-water.jpg; done


As a bonus, for the motivated, I would like it to do it on subfolders and save everything somewhere else (to not overwirte the original files). But thats not that important (would be nice)

Ralul once made me a cool script to resize a folder with all subfolfers .
Maybe it helps.

Code: [Select]
#!/bin/bash
case $1 in
 -e|--edit) #                                                    this script #@
    [ -f /usr/bin/mcedit ] &&MyEditor=/usr/bin/mcedit ||MyEditor=/usr/bin/nano
    exec $MyEditor $0
 ;;
 -c|--convert) #                                                    pictures #@
#@
# Variables to edit:                            #@
#
#        ExcludeNames single quotes!   
  export ExcludeNames='*junk* doubl*'           #@
  export MaxDepth="8"                           #@
  export SaveTo=~/optimizedPictures             #@   "" for saving to workdir
  export OldRemove="N"                          #@
  export UseExif="N"                            #@
  export ConvertApp="/usr/bin/convert"          #@
  export Action="-resize 1920x -quality 98"     #@
##@
##@ Examples:
##@        resize to your monitor hight and keep aspect ratio and good quality:
##@ export Action="-resize x1024 -compress JPEG2000 -quality 98"
##@        resize to your monitor width keeping aspect ratio:
##@ export Action="-resize 1280x -quality 64"
##@        delete 240 pixel of both sides from 1920x1080 to get to 4/3 ratio:
##@ export Action="-crop 1440x1080+240+0"
##@        two Actions: resize wide image to 1920 and crop to 4/3 format
##@ export Action="-resize 1920x -crop 1400x1050+260+15"
##@
    if [ "$SaveTo" = "" ] ; then
        export SaveTo="_$( date +%Y%m%d%H%M ).jpg"
        export externSave="0"
    else
        [ -d "$SaveTo" ] || mkdir -p "$SaveTo" || exit 1
        SaveTo="$( readlink -e $SaveTo )" # if link then dereference
        export externSave="1"
    fi
    if [ "$2" != "" ] ; then
        cd "$2" || exit 1 # workdir was given!
    fi
    cd "$( readlink -e "$PWD" )" # if link then dereference
    export startDir="$( dirname $PWD )"
    [ "${SaveTo}" != "${SaveTo#$PWD}" ] \
        && echo "Error - workDir contains SaveTo: $SaveTo" \
            && exit 1
    echo " "
    echo " ConvertApp Action: $ConvertApp $Action "
    echo " in:                $PWD "
    echo " MaxDepth:          $MaxDepth "
    echo " ExcludeNames:      $ExcludeNames"
    echo " SaveTo:            $SaveTo "
    echo " OldRemove:         $OldRemove "
    echo " UseExif:           $UseExif "
    read -t 44 -p " convert pictures? [y|n] " doit
    [ "$doit" != "y" ] && echo "  --noAction " && exit 0
    echo ". "
    find . -maxdepth $MaxDepth -type d -execdir "$0" convertWorker '{}' ';'
    [ "$externSave" = "1" ] && echo -n -e "\n converted pictures in: $SaveTo \n"
;;
########################################
convertWorker)
    # we must cd into
    cd "$2" || exit 1
#   This script does in subdirs: #@
    for i in *.[Jj][Pp][Ee][Gg] ; do [ -f "$i" ] && mv "$i" "${i%.[Jj][Pp][Ee][Gg]}.jpg"; done
    for i in *.J[Pp][Gg]        ; do [ -f "$i" ] && mv "$i" "${i%.J[Pp][Gg]}.jpg"; done
    # clean special letters  in filenames
    #for i in *{\+,\&,\(,\[,\ }*.jpg ; do
    #    [ -f "$i" ] && mv "$i" "$( echo $i|tr '()[]&+[:blank:]' '----___' )"
    #done
    # SaveTo ends with a slash - otherwise it is a tempName
    if [ "$externSave" = "0" ] ; then
       # datetime tempname without trailing slash
       actual="${SaveTo}" # actual for the tmp_filename
    else
       for i in * ; do
           if [ -f $i ] ; then  # if there is a file make SaveTo/thisDirName/
               m=${PWD#$startDir}
               actual="${SaveTo}/${m#/}/"
               mkdir -p "${actual}" || exit 4
               break
           fi
       done
    fi
    echo -n -e "\n $PWD \n"
    for i in *.jpg ; do #                                   convert pictures #@
        [ -f "$i" ] || continue 1 # if no file for returns *.jpg
        echo -n "   $i "
        for j in $ExcludeNames ; do #                 but exclude some files #@
            if [ "$i" = "$j" ] ; then
               echo -n "--exlcuded "
               continue 2
            fi
        done
        if [ "$UseExif" = "Y" -o "$UseExif" = "y" ] ; then
            exif -m  "$i" >/dev/null 2>&1
            if (( $? > 0 )) ; then
                echo "-NoExif-------------------------- "
                continue 1
            fi
            if (( ${xnew} < $( exif -m "$i"|sed -e '/PixelXDimension/s/PixelXDimension.//p' -e 'd' ) )) ; then
                exif_seem_to_indicate_size=good
            else
                echo "-WasTooLittle----------------------"
                continue 1
            fi
        fi
        [ "$externSave" = "1" ] && s="${actual}$i" || s="${i%.jpg}${actual}"
        $ConvertApp "$i" $Action "$s"
        if [ "$?" = "0" -a -f "$i" -a -f "$s" ] ; then
          if [ "$externSave" = "0" ] ; then
            if [ "$OldRemove" = "Y" -o "$OldRemove" = "y" ] ; then
                rm "$i" && mv "$s" "$i"
            fi
          fi
        fi
    done
    echo " "
;;
*)
    echo -n -e  " Help: \n  $( basename $0 ) -e|-c [dir] \n \n"
#   print Helplines - containing a comment # followed by @
    grep -e '\#\@' $0 | sed -e 's/\#\@//'
    exit 0
;;
esac
exit 0
##@   Maintainer:  eulenreich _@_ gmx _._ de



Thanks for your help! Such a script would be really cool and a help to every photographer!


Edit: When I think about it, it would be nice to rename the files i a first step by defining a name like "monday" with the result, that the photos will have filenames like "monday-001.jpg, monday-002.jpg, monday-003.jpg" etc
« Last Edit: 2016/03/09, 00:36:43 by Lanzi »

Offline Lanzi

  • User
  • Posts: 1.775
Re: Who could help: Script to resize and watermark images
« Reply #1 on: 2016/03/18, 17:26:40 »
ANybody has a hint, at least for the first part?

dal

  • Guest
Re: Who could help: Script to resize and watermark images
« Reply #2 on: 2016/03/18, 18:51:02 »
This might not be exactly what you want, but: have you tried The "Script-Fu" in GIMP? I'm pretty sure you could write your own script there that only saves the image once at the end.
Don't know about the renaming/moving stuff, but I guess if GIMP cannot do that, you could always have a bash script do all of that in a second step.

Offline Lanzi

  • User
  • Posts: 1.775
Re: Who could help: Script to resize and watermark images
« Reply #3 on: 2016/03/19, 13:45:14 »
hmmm, its at least another option.
But I never worked with script FU (but I work a lot with gimp), so it might help...

dal

  • Guest
Re: Who could help: Script to resize and watermark images
« Reply #4 on: 2016/03/19, 14:09:07 »
I've only used it once myself for a purpose similar to yours. Although I had no experience at all it didn't take me more than an hour.

I just opened an image in GIMP, clicked on Filters > Script-Fu > Console, and used the "Browse..." button to find out the names of the functions I needed (e.g. "gimp-image-resize"). Then I googled the functions to find out the correct syntax for my script.

GIMP also has a "batch mode" that allows you to run your scripts from the command line.
https://www.gimp.org/tutorials/Basic_Batch/

Offline pit

  • User
  • Posts: 237
Re: Who could help: Script to resize and watermark images
« Reply #5 on: 2016/03/19, 18:03:28 »
@lanzi

I wrote this one a while ago:

http://paste.siduction.org/20160319163342

It does:
  • Ask for the desired width
  • Resizing
  • Showing aperature, shutter speed, creation time (exif), focal length, name of your camera inside the image (visable)
  • Writing Comment inside the image (optional)
  • Renaming files (Date-Time/exif)
  • Saving into subdirectory ./$resolution
  • Correcting orientation (exif)
It doesn't:
  • touch the original files
  • work recursively
How to use:
  • Save as /usr/local/bin/resizepics
  • (as root) chmod +x resizepics
  • cd image directory
  • run:
  • resizepics [comment]
Requirements:
  • metacam
  • perl
  • exif
  • imagemagick
  • FreeSans.ttf
  • Purisa-Bold.ttf
To Do:
  • If there is more then one image with identical date/time there should be a uniq suffix in the target filename. Any ideas?
« Last Edit: 2016/03/19, 18:10:26 by pit »