Imagemagick scripts for easy image manipulation.

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Imagemagick scripts for easy image manipulation.

Postby viking60 » 04 Mar 2015, 15:37

Imagemagick is..magic but it has a somewhat steep learning curve. It is well worth learning about it and probably you won't bother anyway so we made a script to jump to the good stuff without having to understand it.

Code: Select all

#!/bin/bash
# Colors
ESC_SEQ="\x1B["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
COL_YELLOW=$ESC_SEQ"33;01m"
COL_BLUE=$ESC_SEQ"34;01m"
COL_MAGENTA=$ESC_SEQ"35;01m"
COL_CYAN=$ESC_SEQ"36;01m"
#Legger inn fet og normal skrift aktiveres med bold og normal
bold=`tput bold`
normal=`tput sgr0`

#Setter mine funksjoner
#lager bilde med runde hjørner
function rounded() {
 echo -e "$COL_RED#############################################################################$COL_RESET"
 echo -e "$COL_RED#$COL_RESET ${bold} Please enter a picture including the extension and the desired out-file:${normal} $COL_RED#$COL_RESET"
 echo -e "$COL_RED################################4#############################################$COL_RESET"
read PIC OUT

 string="convert  ${PIC} -alpha set -virtual-pixel transparent -channel A -blur 0x8  -threshold 50%,100% +channel  ${OUT}"
$string
}
#roterer bildet i ønsket valgfri vinkel
function rotate() {
 echo -e "$COL_MAGENTA#############################################################################$COL_RESET"
 echo -e "$COL_MAGENTA#$COL_RESET ${bold}Please enter a picture to rotate and the angle (90 180) and the out-file:${normal} $COL_MAGENTA#$COL_RESET"
 echo -e "$COL_MAGENTA#############################################################################$COL_RESET"
read PIC ANG OUT
   string="convert ${PIC} -rotate ${ANG} ${OUT}"
   
$string
}
#konverterer pdf til png bilder
function pdf(){
 echo -e "$COL_BLUE#####################################################################$COL_RESET"
 echo -e "$COL_BLUE#$COL_RESET ${bold}Please enter the pdf file to convert (without the.pdf extension):${normal} $COL_BLUE#$COL_RESET"
 echo -e "$COL_BLUE#####################################################################$COL_RESET"
read PDF
   string="convert ${PDF}.pdf pages-%03d.png"
$string
   }
   #Myke kanter
function soft(){
    echo -e "$COL_YELLOW#############################################################################$COL_RESET"
 echo -e "$COL_CYAN#$COL_RESET ${bold}Please enter the img file you want to give a Soft edge, and the out-file:${normal} $COL_CYAN#$COL_RESET"
 echo -e "$COL_YELLOW#############################################################################$COL_RESET"
   read PIC OUT
   string="convert  ${PIC} -alpha set -virtual-pixel transparent -channel A -blur 0x8  -level 50%,100% +channel  ${OUT}"
   $string
   }
   #Silent Movie
function silentm(){
    echo -e "$COL_CYAN#####################################################################################$COL_RESET"
 echo -e "$COL_CYAN#$COL_RESET ${bold}Please enter the img file you want to give a Silent movie look, and the out-file:${normal} $COL_CYAN#$COL_RESET"
 echo -e "$COL_CYAN#####################################################################################$COL_RESET"
   read PIC OUT
   string="convert  ${PIC} -alpha set -background none -vignette 0x4  ${OUT}"
   $string
   }
   
   function vis_meny() {

      echo -e '\E[37;44m'"\033[1m            Berserk ImageMagick menu           \033[0m"
      echo -e "$COL_RED 1)$COL_RESET  Rounded corners\n$COL_MAGENTA 2)$COL_RESET  Rotate picture \n$COL_BLUE 3)$COL_RESET  Convert pdf to png image\n$COL_YELLOW 4)$COL_RESET  Soft edges  \n$COL_CYAN 5)$COL_RESET  Silent Movie\n 6)  Quit"
      #column -t -c2 '1) Rounded corners\n2) Rotate picture\n,3) Convert pdf to png image", "4) Soft edges $COL_CYAN 5)$COL_RESET  Silent Movie 6)  Quit'
       }

   
   
#Menysystemet
echo -e '\E[37;44m'"\033[1m            Berserk ImageMagick menu           \033[0m"
PS3='Please enter your choice: '
options=("Rounded corners" "Rotate picture" "Convert pdf to png image" "Soft edges" "Silent Movie" "Quit")

select opt in "${options[@]}"
do
    case $opt in
        "Rounded corners")
        echo 'Listing the images in this directory:'
            find . -type f -exec file {} \; | grep -o -P '^.+: \w+ image'
rounded
echo "This is the result - close the picture to continue"
 display $OUT ;vis_meny
            ;;
        "Rotate picture")
        echo -e "$COL_MAGENTA Listing the images in this directory:$COL_RESET"
find . -type f -exec file {} \; | grep -o -P '^.+: \w+ image'
        rotate
echo "This is the result - close the picture to continue"
display $OUT ;vis_meny
           
            ;;
           
        "Convert pdf to png image")
            pdf
            echo -e "$COL_GREEN ...done Your images are named pages-000.png, pages-001.png, pages-002.png - and so on $COL_RESET"
           
           ;;
           "Soft edges")
           echo -e "$COL_CYAN4 Listing the images in this directory:$COL_RESET"
            find . -type f -exec file {} \; | grep -o -P '^.+: \w+ image'
           soft
           echo "This is the result - close the picture to continue"
display $OUT ;vis_meny

           ;;
            "Silent Movie")
           echo -e "$COL_CYAN4 Listing the images in this directory:$COL_RESET"
            find . -type f -exec file {} \; | grep -o -P '^.+: \w+ image'
           silentm
           echo "This is the result - close the picture to continue"
display $OUT ;vis_meny

           ;;           
        "Quit")
        echo -e "$COL_GREEN Thank you for using this script from bjoernvold.com/forum. See you in the forums$COL_RESET"
            break
            ;;
        *) echo invalid option;;
    esac
done

Image

You will find the discussion here

You can copy the script above and put it in a file called berserkimg.
Then put berserkimg in /usr/local/bin and chmod it to 777
as root:

Code: Select all

chmod 777 /usr/local/bin/berserkimg

After that you can start the script in a terminal by simply typing

Code: Select all

berserkimg

Using it is simple:
Go to a directory where you have pics that you want to work with.
Then start berserkimg and pick a function like rounded corners by entering 1.

The first thing the script does is to list all your images in that directory just to show you what you have to work with.
Image

Here you find the picture to edit and give a name and extension to the output file.

So if want to manipulate baby.jpg with rounded corners we enter 1 and write:

Code: Select all

baby.jpg rounded_baby.png

make one space between the entries and that is it - the magic will happen and pop up the result on your screen.
Image
Once you close that pic you are back to the menu and can work with other pictures.

You can always go to the directory and see the fruits of your work in rounded_baby.png.

You can call the out file whatever you want and give it other formats than png, of course.

Here is a pic made with the silent movie effect:
Image
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

Return to “Tips & Tricks”