Conditional Resizing

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Conditional Resizing

Post by salahuddinonline »

hi
I am facing problem in this, I am using linux ubunto, I have multiple images to work on but do not want to review there canvases and Sizes again again ,
can any one help me out on this,
For Example

if Image is between 475x475 then set canvas as 500x500 with gravity center,
if image size is between 135x135 to 159x159 set gravity center and canvas as 160x160
but do nothing if image size is between 161X161 to 199x199,
and
if image size between 200x200 to 224x224 set gravity center and set canvas as 225x225
but do nothing if image is between 226x226 to 274x274
if image size is between 275x275 to 299x299 set gravity center and canvas as 300x300
but do nothing if image size is between 301X301 to 475x475,

kindly help me out on this,
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional Resizing

Post by fmw42 »

You should write a script to check each condition (if, elif, else) and use -gravity center -background somecolor -extent WxH

see
http://www.imagemagick.org/Usage/crop/#extent

You have not specified what to do if the image is exactly 225 or exactly 300 or larger than 475 or smaller than 226.

Every case must be identified in order to write a complete set of conditional to test your image.

Also what canvas color should be used to fill in the background.

Once I understand all conditions, then I can probably help you will the unix conditional commands to do this.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Conditional Resizing

Post by snibgo »

Confusingly, two threads have been started on this topic, under "Users" and "Developers".

Can a moderator merge them?
snibgo's IM pages: im.snibgo.com
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: Conditional Resizing

Post by salahuddinonline »

ok I try to make it simple to understand
do not work on images which already have sizes of
160
225
300
500
650
900
1100
1300
2400


But
if Canvas Between 135 159 Set Canvas as 160
if Canvas Between 200 224 Set Canvas as 225
if Canvas Between 275 299 Set Canvas as 300
if Canvas Between 475 499 Set Canvas as 500
if Canvas Between 625 649 Set Canvas as 650
if Canvas Between 875 899 Set Canvas as 900
if Canvas Between 1075 1099 Set Canvas as 1100
if Canvas Between 1275 1299 Set Canvas as 1300
if Canvas Between 2375 2399 Set Canvas as 2400

and
Do Nothing if Canvas Size is between 76 134
Do Nothing if Canvas Size is between 161 199
Do Nothing if Canvas Size is between 226 274
Do Nothing if Canvas Size is between 301 474
Do Nothing if Canvas Size is between 501 624
Do Nothing if Canvas Size is between 651 874
Do Nothing if Canvas Size is between 900 1074
Do Nothing if Canvas Size is between 1101 1274
Do Nothing if Canvas Size is between 1301 2374

now I guess I have described it much better..
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional Resizing

Post by fmw42 »

what do we do with images smaller than 76 or larger than 2400?

Is this to be run on one image at a time or on a whole directory?

How should the resulting files be name?

Are all the images jpg and should the results be jpg?

If in a directory are all the files images and no other text files or any other files?
Last edited by fmw42 on 2013-07-14T21:49:00-07:00, edited 1 time in total.
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: Conditional Resizing

Post by salahuddinonline »

it will run on whole directory
Images Larger then 2400 will be Resized at 2400x2400
no Image less then 75 will be processed
name of images will remain same as they were , there Canvases will get change
all files are jpg
no any other file
Last edited by salahuddinonline on 2013-07-14T21:52:44-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional Resizing

Post by fmw42 »

salahuddinonline wrote:it will run on whole directory
Images Larger then 2400 will be Resized at 2400x2400
no Image less then 75 will be processed
name of images will remain same as they were , there Canvases will get change

It would be best to process them into another directory?

Are all files in the directory images and what type (jpg, png, etc) are to be processed and to what format?
Are there any non-images in the directory?
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: Conditional Resizing

Post by salahuddinonline »

there would be no any other file except JPG images
does not matter if we move processed files to another directory
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional Resizing

Post by fmw42 »

salahuddinonline wrote:there would be no any other file except JPG images
does not matter if we move processed files to another directory
What background color do you want for the canvas?
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: Conditional Resizing

Post by salahuddinonline »

white
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional Resizing

Post by fmw42 »

try this. You need to change currentdirectory to the directory with the images. then change path2newdirectory to the path to your already created new empty directory. then copy and paste this into a terminal window.


cd currentdirectory
newdir=path2newdirectory
list=`ls`
for img in $list; do
dim=`convert $img -format "%[fx:max(w,h)]" info:`
if [ $dim -ge 135 -a $dim -le 159 ]; then
convert $img -resize 160x160 -gravity center -background white -extent 160x160 $newdir/$img
elif [ $dim -ge 200 -a $dim -le 224 ]; then
convert $img -resize 225x225 -gravity center -background white -extent 225x225 $newdir/$img
elif [ $dim -ge 275 -a $dim -le 299 ]; then
convert $img -resize 300x300 -gravity center -background white -extent 300x300 $newdir/$img
elif [ $dim -ge 475 -a $dim -le 499 ]; then
convert $img -resize 500x500 -gravity center -background white -extent 500x500 $newdir/$img
elif [ $dim -ge 625 -a $dim -le 649 ]; then
convert $img -resize 650x650 -gravity center -background white -extent 650x650 $newdir/$img
elif [ $dim -ge 875 -a $dim -le 899 ]; then
convert $img -resize 900x900 -gravity center -background white -extent 900x900 $newdir/$img
elif [ $dim -ge 1075 -a $dim -le 1099 ]; then
convert $img -resize 1100x1100 -gravity center -background white -extent 1100x1100 $newdir/$img
elif [ $dim -ge 1275 -a $dim -le 1299 ]; then
convert $img -resize 1300x1300 -gravity center -background white -extent 1300x1300 $newdir/$img
elif [ $dim -ge 2375 -a $dim -le 2399 ]; then
convert $img -resize 2400x2400 -gravity center -background white -extent 2400x2400 $newdir/$img
elif [ $dim -gt 2400 ]; then
convert $img -resize 2400x2400 -gravity center -background white -extent 2400x2400 $newdir/$img
else
cp $img $newdir/$img
fi
done
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: Conditional Resizing

Post by salahuddinonline »

Its not working Fred
May be I am making mistake some where
I am going with Images Directory with
cd /home/sarehman/Desktop/Original/
then replaced all newdir with /home/sarehman/Desktop/processed/
then pasted code but still no working
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional Resizing

Post by fmw42 »

salahuddinonline wrote:Its not working Fred
May be I am making mistake some where
I am going with Images Directory with
cd /home/sarehman/Desktop/Original/
then replaced all newdir with /home/sarehman/Desktop/processed/
then pasted code but still no working
Use

/home/sarehman/Desktop/processed

no trailing / because it is built into the script
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: Conditional Resizing

Post by salahuddinonline »

kindly check this out Fred .....
I have changed directory now


sarehman@sarehman:~/Desktop/Synch folders/Original$ ls
Checking1.jpg Checking2.jpg Checking3.jpg Checking4.jpg Checking5.jpg
sarehman@sarehman:~/Desktop/Synch folders/Original$ Checking1
Checking1: command not found
sarehman@sarehman:~/Desktop/Synch folders/Original$ $list; do
bash: syntax error near unexpected token `do'
sarehman@sarehman:~/Desktop/Synch folders/Original$ dim=`convert $img -format "%[fx:max(w,h)]" info:`
convert: unable to open image `650465406046.jpg': @ error/blob.c/OpenBlob/2587.
convert: missing an image filename `info:' @ error/convert.c/ConvertImageCommand/3011.
sarehman@sarehman:~/Desktop/Synch folders/Original$ if [ $dim -ge 135 -a $dim -le 159 ]; then
> convert $img -resize 160x160 -gravity center -background white -extent 160x160 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> elif [ $dim -ge 200 -a $dim -le 224 ]; then
> convert $img -resize 225x225 -gravity center -background white -extent 225x225 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> elif [ $dim -ge 275 -a $dim -le 299 ]; then
> convert $img -resize 300x300 -gravity center -background white -extent 300x300 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> elif [ $dim -ge 475 -a $dim -le 499 ]; then
> convert $img -resize 500x500 -gravity center -background white -extent 500x500 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> elif [ $dim -ge 625 -a $dim -le 649 ]; then
> convert $img -resize 650x650 -gravity center -background white -extent 650x650 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> elif [ $dim -ge 875 -a $dim -le 899 ]; then
> convert $img -resize 900x900 -gravity center -background white -extent 900x900 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> elif [ $dim -ge 1075 -a $dim -le 1099 ]; then
> convert $img -resize 1100x1100 -gravity center -background white -extent 1100x1100 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> elif [ $dim -ge 1275 -a $dim -le 1299 ]; then
> convert $img -resize 1300x1300 -gravity center -background white -extent 1300x1300 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> elif [ $dim -ge 2375 -a $dim -le 2399 ]; then
> convert $img -resize 2400x2400 -gravity center -background white -extent 2400x2400 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> elif [ $dim -gt 2400 ]; then
> convert $img -resize 2400x2400 -gravity center -background white -extent 2400x2400 $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> else
> cp $img $/home/sarehman/Desktop/Synch\ folders/Maximum/$img
> fi
bash: [: too many arguments
bash: [: too many arguments
bash: [: too many arguments
bash: [: too many arguments
bash: [: too many arguments
bash: [: too many arguments
bash: [: too many arguments
bash: [: too many arguments
bash: [: too many arguments
bash: [: -gt: unary operator expected
cp: cannot stat `650465406046.jpg': No such file or directory
sarehman@sarehman:~/Desktop/Synch folders/Original$ done
bash: syntax error near unexpected token `done'
sarehman@sarehman:~/Desktop/Synch folders/Original$
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional Resizing

Post by fmw42 »

sarehman@sarehman:~/Desktop/Synch folders/Original$ Checking1
Checking1: command not found
I am not sure what you are doing here, but there cannot be spaces between $ and Checking1.

Are you doing a cd to /Desktop/Synch folders/Original/Checking1

On my Mac if there are spaced in folder names they need to be enclosed by quotes. I am not sure about other Linux computers. So

cd /Desktop/"Synch folders"/Original/Checking1


Please post the script as you have modified it here enclosed by the Code button above to make it easier to read.
Post Reply