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
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Some image magic

Postby viking60 » 17 Mar 2013, 16:43

ImageInstall imagemagic it is in your repos (most likely you have it installed already).
Now you can use the Command Line Interface to rapidly do some magic to your images. Yes you can do it with programs like Gimp too but it takes a lot more time.
So let us look at some practical examples.
You have this avatar that you use in forums but this new forum demands it to be max 100x100 pixels.
You need to adjust that humongus Wallpaper that is way to big.
Or you want a series of images to be exactly the same size for your animation.


Why do we need a post here for that it can all be found here?
Well sometimes it is hard to find obvious stuff when you have all those fantastic functions to describe Here is an example:
If I did a correct resize to my resolution of 1366x768:

Code: Select all

convert bakgrunn.png -resize 1366x768 newwp.png

It would get either 1366 or 768 right but not both! I am sure it has something to do with the original proportions of the image.
So to get it right you must do it like this:

Code: Select all

convert bakgrunn.png -resize 1366x768! newwp.png

The difference is the :!:
Now the newwp.png will be exactly 1366x768 not 970x768 or whatever +1

This is very useful for avatars also.
To resize all your jpg images in a directory to 50% you can use wildcards

Code: Select all

convert *.jpg -resize 50% smaller.jpg

You have to provide a name for the new sized images but they will be numbered like this.
smaller-0.jpg smaller-1.jpg etc.

So if you want to convert all your jpg images in a directory to exactly 200x200 pixels and convert them to png you could do it like this:

Code: Select all

convert *.jpg -resize 200x200! 200.png


You would then get a series of images called 200-0.png 200-1.png 200-2.png etc.

So now you can make all the wallpapers in your Wallpaper directory the same correct size +1 in one sec.
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
rolf
Guru-Berserk
Posts: 1107
Joined: 16 Mar 2010, 16:07

Re: Some image magic

Postby rolf » 17 Mar 2013, 17:00

:B Yes, that's very useful for certain situations and I saw that in your wallpaper tip.
I like fotoxx for a graphical program that is very full-featured and intuitive to do quick resizing, cropping, etc. Have you tried it?

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

Re: Some image magic

Postby viking60 » 17 Mar 2013, 17:24

Not yet - Trying now.......Ill get back to it when I am through with the imagemagic.
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 » 17 Mar 2013, 23:01

If your Wallpaper is rather dull and you want to give it that quality feel try this:

Code: Select all

convert MyWallpaper.jpeg -pointsize 100 -font Impact -fill cyan -gravity South  -draw "text 25,40 'Berserk power'"  output.png

It will write text on the "South" of your wall paper you can change it to North etc, pointsize 100 is the font size and Impact is the font play around with it....
And pick a real wallpaper in stead of MyWallpaper.jpeg it will create the modified wallpaper under output.png.
The text will be added to the picture like this:
Image

To rotate an image

Code: Select all

convert MyWallpaper.png -rotate 180 output.png
Comes in handy when you discover that you held the camera upside down... :-D
Image

If you do not want to keep the original and want to change the image directly you can write the changes back to the same image;

Code: Select all

convert output.png -rotate 180 output.png

For brave Berserks who laugh in the face of backups only...

To resize the images themselves instead of writing the changes to a new file you can use mogrify

Code: Select all

mogrify -resize 50% *.jpg

This will reduce the size of all jpg files in the directory.
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 » 18 Mar 2013, 03:54

Many think it is cleaner to put the source image and the output image at the end:

Code: Select all

convert -pointsize 10 -font Impact -fill cyan -gravity North  -draw "text 1,30 'Berserk power'"  guru.png output.png

here is the output
Image

Code: Select all

convert -pointsize 12 -font Arial -fill yellow -gravity North  -draw "text 1,30 'Guru'"  guru.png output.png

Image
But this is a bit small so I did a:

Code: Select all

convert -resize 200%  output.png output2.png

This is output2.png
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 » 18 Mar 2013, 12:26

Tried fotoxx but it keeps chrashing in Mandriva....
Also there seems to be a problem for Mandriva (2010.2) to add the selected fonts when using imagemagic. This Bug says it is fixed but I wonder :think:
In any case Mandriva just picks a default font so it does work (kind of).
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
rolf
Guru-Berserk
Posts: 1107
Joined: 16 Mar 2010, 16:07

Re: Some image magic

Postby rolf » 18 Mar 2013, 14:42

:think: Looking through some of the various manuals in imagemagick-6.7.0.9-1.2-mdv2011.0.x86_64, I see that it has a lot of features, suitable for scripting, for example, on a website. However, while I can intuit that the 'North' switch would put the text toward the top of the output, I wonder if how far 'North' and left-right-centered is somehow automatic or specified by the '1,30' part. :?:

Anyway, I tried the montage command with 9 same-sized gifs :A

Code: Select all

[rolf@localhost montage]$ montage icon* -adjoin montage.gif

Image
A squared whole number of square, same-sized icons fit nicely into a square array, a bit vanilla but I'm lazy to explore the vast capabilities of ImageMagick.

Trying an equal number of animated gifs, differing geometries:

Code: Select all

[rolf@localhost montage]$ montage *gif -adjoin montage2.gif
:A
Image


Well, it didn't montage the animation but, it seems, all the layers of the gifs. I'm sure there are many practical uses for ImageMagick, nonethless. :B

ImageSad about fotoxx crashing. I think I remember having some problem with it but not what or how fixed, if so. :confused If you ran it from a prompt, maybe a message would point the way. Here, ftp://mirror.yandex.ru/mandriva/officia ... x86_64.rpm seems to be working ok. :greetings

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

Re: Some image magic

Postby viking60 » 18 Mar 2013, 15:18

However, while I can intuit that the 'North' switch would put the text toward the top of the output, I wonder if how far 'North' and left-right-centered is somehow automatic or specified by the '1,30' part. :?:


Yes the North is a gravity thing pulling it to the top of the pic. And the 1,30 part are the XY coordinates so they are more spesific. I am not sure how they interact. But you can remove the gravity alltogether and use only XY.
Or you can set the draw to zero:

Code: Select all

-draw "text 0,0 'Über Berserks rule'"
and then you will get the full and predictable effect of North South and NortWest or whatever in your gravity settings.
Like this

Code: Select all

convert -pointsize 14 -gravity North -font Lucida -fill Brown   -draw "text 0,0 'Über'"  viking.png output.png

ImageImageImageImageImage

I only use the xy coordinates to place it more exactly (on the helmet) by error and trial:

Code: Select all

convert -pointsize 14 -gravity North -font Lucida -fill Brown   -draw "text 1,10 'Ûber'"  viking.png output.png

Image

And I think you can animate your gifs again with the animate command:
http://www.imagemagick.org/script/animate.php
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
rolf
Guru-Berserk
Posts: 1107
Joined: 16 Mar 2010, 16:07

Re: Some image magic

Postby rolf » 18 Mar 2013, 15:35

So, it looks like 'North, South, East, West' are already centered wrt their respective orthogonal property. If so, wouldn't '0,30' be the setting to keep that 'North' text centered on the x-axis? :T :?:

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

Re: Some image magic

Postby viking60 » 18 Mar 2013, 16:10

Jup that sounds about right +1 North and South are centered.
I stay on the "10" line and I can adjust (from center) with say 5,10 to the right or even with a negative no -5,10 to the left. Or I move up or down by changing "10" to say 30 which would put the text lower (to the South)
This should make it clear: :-D
Image
What I have used here is the animate command. I put all the vikings above in a directory and ran:

Code: Select all

animate *.jpg

This starts an animation only to show that it works you can rightclick and save. That will give you a series of images . no animation.
So we have to use convert here too:

Code: Select all

convert -delay 30 berserk-0 berserk-1 berserk-2 berserk-3 berserk-4 berserk-5 -loop 0 playme.gif
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 » 18 Mar 2013, 18:03

Same animation as above with different delay

Code: Select all

convert -delay 100 berserk-0 berserk-1 berserk-2 berserk-3 berserk-4 berserk-5 -loop 0 playme.gif

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

Some image magic-framing

Postby viking60 » 19 Mar 2013, 08:55

Ok time for some frames:
To raise your image:

Code: Select all

convert -raise 7x7 viking60.png vikingraised.png

Image
And for a lower frame:

Code: Select all

convert +raise 7x7 viking60.png vikinglowered.png

Image
Now I am officially framed....
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”