Page 1 of 1

batch resize of images depends on size

Posted: 2015-08-31T12:41:40-07:00
by schel4ok
Hi,

I want to resize all png files in directory to match heigth 190px, but some images less than 190 and some bigger. So I want to have if statement in bash script like this

Code: Select all

for f in *.*
do 
	h=`identify -format %h $f` | if [ "$h" -gt 190 ]; then convert $f -strip -thumbnail x190 test/$f; fi ;
	convert $f -strip -gravity center -extent x190 -transparent white test/$f;
done
So images that less than 190px height will be enlarged to 190px without increasing in width.
Images that bigger than 190px height will be resized to 190px.

But it seems that I have some problem here [ "$h" -gt 190 ]

Code: Select all

$ sh images.sh
images.sh: line 38: [: : integer expression expected
images.sh: line 38: [: : integer expression expected
images.sh: line 38: [: : integer expression expected
`1.png' -> `../../../public/img/1.png'
`2.png' -> `../../../public/img/2.png'
`3.png' -> `../../../public/img/3.png'

Re: batch resize of images depends on size

Posted: 2015-08-31T12:56:34-07:00
by fmw42
if [ "$h" -gt 190 ]
Remove the quotes around $h, since they are integers and -gt expects number not strings.

so use

Code: Select all

if [ $h -gt 190 ]
Alternately, you can avoid the if tests, if you compute the percent ratio of 100*190/imageheight = pct, so that you use

Code: Select all

-thumbnail 100x$pct%!

Re: batch resize of images depends on size

Posted: 2015-08-31T14:49:43-07:00
by fmw42
So images that less than 190px height will be enlarged to 190px without increasing in width.
Perhaps I misunderstood. If you do what I said above with the resize, then you will distort the look of the images, if you force the width not to change, and resize only the height. Is that what you really want?

Or do you want to resize both dimensions to fit the 190 height and preserve aspect ration, but then crop or pad to the original width? You can do that by simply getting the image width before processing and then resize and extend.

Code: Select all

width=`convert image -format "%w" info:`
convert image -thumbnail x190 -gravity center -background none -extent ${width}x190 result

Re: batch resize of images depends on size

Posted: 2015-08-31T23:15:15-07:00
by schel4ok
I use this code

Code: Select all

if [ "$h" -gt 190 ]
because I checked that when using this

Code: Select all

if [ $h -gt 190 ]
I got following error

Code: Select all

images.sh: line 38: [: -gt: unary operator expected
I don't know why.

Anyway thanks for your help. I found good solution with you. The code below do the job.
A little bit more explanation for you to better understand my target. I have folder with png files with transparency. To have good looking web page I need all of them height 190px.
So if initial height less than 190px I just extent height adding transparent pixels and not distorting initial picture.
And if initial height greater than 190px I resize picture down to 190px height keeping aspect ratio. hope you understand me now.

Code: Select all

for f in *.*
do 
	w=`convert $f -format "%w" info:`
	h=`convert $f -format "%h" info:`
		if [ $h -gt 190 ]; 
			then convert $f -strip -transparent white -thumbnail x190 output/$f; 
			else convert $f -strip -gravity center -transparent white -extent ${w}x190 output/$f; 
		fi
done

Re: batch resize of images depends on size

Posted: 2015-08-31T23:51:48-07:00
by fmw42
You should not need the conditional. Use -thumbnail x190>. The > means if larger than 190.

Try

Code: Select all

convert $f -strip -thumbnail x190> \
-gravity center -background none -extent ${w}x190 \
output/$f
see
http://www.imagemagick.org/script/comma ... p#geometry

Re: batch resize of images depends on size

Posted: 2015-08-31T23:56:20-07:00
by fmw42
This is likely your problem:

Code: Select all

h=`identify -format %h $f` | if [ "$h" -gt 190 ] ...
There is nothing specified to pipe. Try separating it into two statements and using the variable as integer number not character.

Code: Select all

h=`identify -format "%h" $f`
if [ $h -gt 190 ] ...
But this should not be needed, if you use my method above with the -thumbnail x190>

Re: batch resize of images depends on size

Posted: 2015-09-01T00:04:01-07:00
by schel4ok
unfortunately this

Code: Select all

convert $f -strip -transparent white -thumbnail x190> -gravity center -extent ${w}x190 output/$f
gives me an error

Code: Select all

convert.exe: unable to open image `center': No such file or directory @ error/blob.c/OpenBlob/2709.
I think this because ">" assumed like redirection operator

Re: batch resize of images depends on size

Posted: 2015-09-01T00:14:02-07:00
by fmw42
You should remove the -transparent white unless there is white in your input image that you need to replace with transparent. The -background none does the extent with a transparent background.

Do it as exactly the following

Code: Select all

convert $f -strip -thumbnail "x190>" -gravity center -background none -extent ${w}x190 output/$f
or

Code: Select all

convert $f -strip -thumbnail x190\> -gravity center -background none -extent ${w}x190 output/$f
Do either work?

What version of IM are you using and on what platform?

Re: batch resize of images depends on size

Posted: 2015-09-01T00:31:24-07:00
by schel4ok
both works. Thank you.

Code: Select all

$ uname -a
MINGW32_NT-6.1 WIN-SV4R9NF6R3V 1.0.12(0.46/3/2) 2012-07-05 14:56 i686 unknown

Re: batch resize of images depends on size

Posted: 2015-09-01T00:33:51-07:00
by fmw42
OK. Good. But out of curiosity, what version of IM.

Re: batch resize of images depends on size

Posted: 2015-09-01T00:49:22-07:00
by schel4ok
Version: ImageMagick 6.8.9-9 Q16 x86 2014-10-19

it is integrated in openserver package for local development

Re: batch resize of images depends on size

Posted: 2015-09-03T23:37:44-07:00
by schel4ok
could you also help me with this issue. It is very similar with error above

If I do this, it is OK.

Code: Select all

mogrify -path ../out1 -filter Triangle -define filter:support=2 -thumbnail 640 \
-unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 82 \
-define jpeg:fancy-upsampling=off -define png:compression-filter=5 \
-define png:compression-level=9 -define png:compression-strategy=1 \
-define png:exclude-chunk=all -interlace none -colorspace sRGB *
but if I do this

Code: Select all

smartresize() {
   mogrify -path $3 -filter Triangle -define filter:support=2 -thumbnail $2 \
   -unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 82 \
   -define jpeg:fancy-upsampling=off -define png:compression-filter=5 \
   -define png:compression-level=9 -define png:compression-strategy=1 \
   -strip -define png:exclude-chunk=all -interlace none -colorspace sRGB $1
}

smartresize resources/img/* 640 out/
then I have error

Code: Select all

$ sh images.sh
mogrify.exe: invalid argument for option `resources/img/avangard.png': -thumbnail @ error/mogrify.c/MogrifyImageCommand/6200.
I tried

Code: Select all

smartresize resources/img/* 640 out/
smartresize resources/img/* "640" out/
smartresize resources/img/* '640' out/
but always error

Re: batch resize of images depends on size

Posted: 2015-09-03T23:53:01-07:00
by snibgo
This isn't a scripting foum, and I'm no expert on bash, but what arguments is smartresize() receiving? Put an echo at the start to see the first 3 arguments.