Some image magic

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
Snorkasaurus
Berserk
Posts: 587
Joined: 30 Dec 2013, 19:19
Contact:

Re: Some image magic

Postby Snorkasaurus » 05 Jul 2014, 02:40

Wow... that rounded corner stuff is way more complicated than it has to be. Not that GIMP is any better, it is almost as bloated as the application it is supposed to replace. :(

S.

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Some image magic

Postby viking60 » 05 Jul 2014, 11:40

If you mean in Gimp - yes that is a time consuming procedure.
That is why I use Image Magic; it is way faster and can treat a lot of pics at the same time.

If you mean my descriptions you must remember that I have described several ways of creating rounded corners, and the results vary.
You can use "one command to rule them all" as I will show below:

If you put all the images you want to edit in one directory like "work" you can then provide all jpg pictures or png pictures with a soft edge and rounded corner effect by simply using *.jpg or *.png

Like this:

Code: Select all

convert  *.jpg -alpha set -virtual-pixel transparent -channel A -blur 0x8  -level 50%,100% +channel  soft_edge.png


It will create a new set of pics (off all *.jpg pics) called soft-edge-0.png, soft-edge-1.png, soft-edge-2.png - and so on. The all will have the soft edges and a rounded corner effect - and if you don like it delete all beginning with "soft-edge", alter the parameters and have another go..

If you want to convert all your .png pics simply change *.jpg to *.png,
Before
Image
After
Image

If you want a softer edge you can change the parameter 0x8 to say.. 0x18 in the command above it will look like this:
:A
Image

But you only want rounded corners and never asked for those soft edges you say? (See? I know you +1)
Try changing -level above with -threshold
:A

Code: Select all

convert  *.png -alpha set -virtual-pixel transparent -channel A -blur 0x8  -threshold 50%,100% +channel  soft_edge.png

Image

It will take a few seconds to do it with 50 pics, and it would take a year to treat them one by one in Gimp.

You could make an alias of it like rcornerjpg or roundedcornerpng etc; whatever you will remember...

After that you can take the best one and give it that final touch in Gimp.
One line - one command - some adjusted parameters..... and all those fantastic results.
Even a baby can do it:
Image
:lol:
It is that easy!
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!"

User avatar
Snorkasaurus
Berserk
Posts: 587
Joined: 30 Dec 2013, 19:19
Contact:

Re: Some image magic

Postby Snorkasaurus » 05 Jul 2014, 16:53

I actually meant the applications... even ImageMagick.

viking60 wrote:If you mean in Gimp - yes that is a time consuming procedure.

So when I say I don't like GIMP I mean that it is trying to be a replacement for Photoshop. Which in my mind is about as crazy as trying to be a replacement for forum spam... who needs that? GIMP is big, really big. And some of the features are very non-intuitive. I haven't done rounded corners in GIMP in a long time but if I recall correctly it was reasonably easy... but who the hell needs that much application for such a straightforward task?

When I said that even ImageMagick was over complicated I was referring to the process of creating template transparent corners, positioning them over the original image and then flattening it. However...

viking60 wrote:But you only want rounded corners and never asked for those soft edges you say? (See? I know you +1)

Code: Select all

convert  *.png -alpha set -virtual-pixel transparent -channel A -blur 0x8  -threshold 50%,100% +channel  soft_edge.png
Now this I can work with... no transparent templates to keep handy, easy to automate with a script, and as an added bonus PNG files already have the EXIF information stripped. Dude, I am sold. :berserkf
Image


S.

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Some image magic

Postby viking60 » 05 Jul 2014, 18:04

Yup That looks great - you have got the hang of it. Those masking techniques come in handy when making buttons etc., corners may not be the most practical use.
ImageImageImage
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!"

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Some image magic

Postby viking60 » 07 Jul 2014, 23:03

If you want to convert a picture to png (or whatever format) you can use imagemagic.
You could make a python script like this:
convert.py

Code: Select all

#! /usr/bin/env python
import subprocess
import os
path=input('Enter your working directory - full path to where your pics are:  ')

os.chdir(path)



from subprocess import call
call([ "ls", "-l"])

#import subprocess
pic=input ('Please enter a picture including the extension: ')
out =input ('Please enter the destination, including the extension you want to convert to: ')

params = ['convert', pic, out]
#subprocess.check_call(params)
print ('Congratulations! Everything went fine,', out , 'was created in', path)

Wait= input('PRESS ENTER TO CONTINUE')


After that you can simply type ./convert.py and enter the path to where your picture is and convert it to png or tiff or whatever.

The script will ask for the full path to where the pic to convert is (your working directory) /home snork/Pictures/workingdirectory
It will list the content of that directory and ask you to write in the file you want to convert (snork,jpg - which must exist there)
Then it will ask you what the out file should be (supersnork.png - which will be created)


:A
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!"

User avatar
Snorkasaurus
Berserk
Posts: 587
Joined: 30 Dec 2013, 19:19
Contact:

Re: Some image magic

Postby Snorkasaurus » 07 Jul 2014, 23:25

Actually I try to avoid interpreters like Python if I can... that is one of my disappointments with modern Linux is that they seem to be packed with those kinds of apps. :-(

Not to say that Microsoft hasn't just shoved .NET in to Windows. :C

S.

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Some image magic

Postby viking60 » 08 Jul 2014, 11:57

In that case you can simply put this bash script in your working directory:
rounded.sh

Code: Select all

#!/bin/bash

 echo 'Please enter a picture including the extension and the desired out file: '
read PIC OUT

 string="convert  ${PIC} -alpha set -virtual-pixel transparent -channel A -blur 0x8  -threshold 50%,100% +channel  ${OUT}"
$string


Simply enter the in file and the out file with a space between them (baby.jpg mybaby.png) and it will create mybaby.png with rounded corners (of course baby.jpg has to exist in that directory).

You get both the conversion from jpg to png and rounded corners - two for the price of one.
If you rename it to rounded and copy it to /usr/bin and chmod it:

Code: Select all

sudo chmod +x /usr/bin/rounded

You can convert any picture in any directory by typing rounded in a terminal and enter the pic to convert and the out file.
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!"

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Some image magic

Postby viking60 » 10 Jul 2014, 16:43

Ok I realize that this has become a long thread and can be somewhat complicated so I gathered a few of the functions described here in a script.
:A

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


The best way to use it is to put it in your /usr/local/bin
and chmod it to execute

Code: Select all

sudo chmod +x /usr/local/bin/berserkimg

I have called the script berserkimg but you may call it whatever you want.

After you have done that you can simply type berserkimg in a terminal:
Image

You can create pics with rounded corners, flip a pic in whatever angel you care to enter (90 and 180 would be the most common ones but you may enter 45 or whatever) or convert pdf to png nicely structured per page.

Wildcards are accepted. So this thing is actually useful.

If you want to flip the current baby picture 90 degrees you would enter something like this:

Code: Select all

baby.jpg 90 test.png

simply enter the parameters with a space in-between them.
In the case of rounded corners and flip pictures the result will be shown to you and the script will continue once you close it.
Video coming up....
:A

Edit: added some new functions to the menu
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”