Page 1 of 1

Script shell ubuntu for muliptle actions

Posted: 2010-08-27T05:57:19-07:00
by jackisbak
Hello guys !

First I have to thank you, imagemagick devs for your work !
I haven't already used imagemagick but it seems very powerful !

I am on ubuntu (a tech guy installed it for me because i'm not an expert at computers) and I have installed imagemagick from here (http://doc.ubuntu-fr.org/imagemagick)
My problem is every day, I have to add some photos to a website. But those photos have to be transformed to fit with the website in the database, otherwise, the website send an error.
I used to transform each photo with photoshop but it takes me huge time when I have 50 photos...

So can you help me create a shell script to do some transformations ? there are just few.
- all photos in jpg, 72ppp
- if the width is bigger than the length, resize to 480px of width with ratio kept
- if the length is bigger than the width, resize to 560px of length with ratio kept
- make a thumbnail with the prefix "tn_"
- for all thumbnails resize the bigger size to 120px with ratio kept.

and that' all !
For each photo it takes me two or three minutes ! And I have twenty or thirty photos each day to resize !
Can you help me ?

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-27T10:24:37-07:00
by fmw42
- all photos in jpg, 72ppp
- if the width is bigger than the length, resize to 480px of width with ratio kept
- if the length is bigger than the width, resize to 560px of length with ratio kept
- make a thumbnail with the prefix "tn_"
- for all thumbnails resize the bigger size to 120px with ratio kept.
What is ppp? pixel per inch, pixels per meter, pixels per centimeter?

there is no ppp as far as I know.

Is this the input density/resolution or the desired output density?

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-27T10:52:03-07:00
by fmw42
Here is a quick cut:

You have to cd to your own directory. The results will be put into that directory. Note I added _new to the files as I already had jpg images in that directory and did not want to overwrite. If you don't care, then remove the _new. If you filenames have spaces in them, then this will fail and other additions need to be made to the file to change your spaces to something else and then back to spaces. Or you can modify any file with a space in the name to change it to something like a hyphen or underscore. I used an array of image names found from the unix command ls as I found it generally works better than just a list of filenames.


cd "yourdirectory"
listArr=(`ls`)
num=${#listArr[*]}
for ((i=0; i<$num; i++)); do
inname=`convert ${listArr[$i]} -ping -format "%t" info:`
echo $inname
aspecttest=`convert ${listArr[$i]} -ping -format "%[fx:w/h>=1?1:0]" info:`
if [ $aspecttest -eq 1 ]; then
# width is greater than or equal to height
size=480x480
else
# width is less than height
size=560x560
fi
convert ${listArr[$i]} -resize $size -density 72 -units pixelsperinch ${inname}_new.jpg
convert ${inname}_new.jpg -thumbnail 120x120 -density 72 -units pixelsperinch tn_${inname}_new.jpg
done

If you are on Windows, then this won't work and you need to change according to http://www.imagemagick.org/Usage/windows/

For details on commands, see

-resize http://www.imagemagick.org/script/comma ... php#resize
-thumbnail http://www.imagemagick.org/script/comma ... #thumbnail
image geometry http://www.imagemagick.org/script/comma ... p#geometry
-density http://www.imagemagick.org/script/comma ... hp#density
-units http://www.imagemagick.org/script/comma ... .php#units

string formats at http://www.imagemagick.org/script/escape.php
fx calcs at http://www.imagemagick.org/Usage/transform/#fx_escapes

resizing at http://www.imagemagick.org/Usage/resize/

The rest is unix bash script and you will need to google that to learn more

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-28T13:17:35-07:00
by jackisbak
I'm really sorry, ppp is in french, ppi in english =D

I googled a little about shell script and if I'm right, there is a "#/bin/bash/" missing at the beginning ! =D
Sorry if I'm wrong, I just begin in creating scripts =S

I have seen all your scripts in your website fred's magick scripts but they look like chinese for me :(
So I tested it and it works well by copying all with "#/bin/bash" in a sh file !! Thank you very much !!!

But there is just a little thing I forgot... I don't know if it is possible to do it but I still ask:
is it possible to limit the size in KB ?
I remember when I save an image on photoshop, i use "save for web" option to adjust image file to around 30ko (and around 10ko for thumbnail) because some photos won't display if they are too heavy...
Is it possible to limit the file size ?

edit: yeeees!!!! it is possible !!!
I have found this in your scripts !!
http://www.fmwconcepts.com/imagemagick/ ... /index.php
But I don't know how to add it in the script already done...
Can you help me ?

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-28T14:57:25-07:00
by fmw42
I googled a little about shell script and if I'm right, there is a "#/bin/bash/" missing at the beginning ! =D
Sorry if I'm wrong, I just begin in creating scripts =S
As a formal script fille you need to add at the top:

#!/bin/bash

but you can copy and paste my code into a terminal window and it will run

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-28T15:04:26-07:00
by fmw42
But there is just a little thing I forgot... I don't know if it is possible to do it but I still ask:
is it possible to limit the size in KB ?
Yes, but what I did in my script you found was basically estimate the size, resize, then check the size and adjust the size again, and iterate until the result is within some tolerance. It is rather tricky. But you can call one script from within another. So you could do your processing, then call my script to get the right size and return the result to your script. However, I am not sure you want to change the size to get the required filesize. You probably want to do something similar but change the jpg compression.

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-29T00:54:28-07:00
by jackisbak
fmw42 wrote:However, I am not sure you want to change the size to get the required filesize. You probably want to do something similar but change the jpg compression.
Yes, the dimension of the photos have to stay the same as at the end of the script you made. Only the filesize need to change.
Is it possible to the same thing as downsize but with jpg compression ? The trick like : estimate the size, downgrade the compression of 5%, then check the size and if not lower than 35ko, adjust the jpg compression and iterate until the resultis within the 35ko ?

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-29T10:54:33-07:00
by fmw42
jackisbak wrote:
fmw42 wrote:However, I am not sure you want to change the size to get the required filesize. You probably want to do something similar but change the jpg compression.
Yes, the dimension of the photos have to stay the same as at the end of the script you made. Only the filesize need to change.
Is it possible to the same thing as downsize but with jpg compression ? The trick like : estimate the size, downgrade the compression of 5%, then check the size and if not lower than 35ko, adjust the jpg compression and iterate until the resultis within the 35ko ?
My script downsize used -resize so that it was general for any image type and not just jpg. But it should be possible to change the jpg compression to achieve similar results if the image is always jpg. However, I have not coded/tested that and there will be size limits per the restrictions on jpg compression.

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-29T14:15:50-07:00
by jackisbak
fmw42 wrote:there will be size limits per the restrictions on jpg compression.
It is evident that a 560x560, even in 100% jpeg compression will not be over 10 Mo... =)
A bitmap of 560x560 by 24 bits is around 7 Mo !
jackisbak wrote:But it should be possible to change the jpg compression to achieve similar results if the image is always jpg
Great ! I hope you will help me when you can...
I saw the inside of the script downsize. It is soooo long !!!
Bravo ! I couldn't do it, even if I want to !
Tomorrow, I will be the same... Photo processing all day long... =D
See you tomorrow night.

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-29T14:40:56-07:00
by fmw42
Great ! I hope you will help me when you can...
I saw the inside of the script downsize. It is soooo long !!!
Bravo ! I couldn't do it, even if I want to !
Tomorrow, I will be the same... Photo processing all day long... =D
See you tomorrow night.
Sorry, but I won't be able to look into this for a week or so and then can make no commitments about implementation.

Re: Script shell ubuntu for muliptle actions

Posted: 2010-08-30T13:52:31-07:00
by jackisbak
Don't worry, you help me so much !

Thank you again !