Page 1 of 2

Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-25T04:38:20-07:00
by ManuO
Hello,
My first post on the forum. I wanted to first thank the ImageMagick Team for the fantastic piece of software.

I am using mogrify to resize a set of images on Mac or Ubuntu using the following command:

Code: Select all

mogrify -resize 50% *.jpg
and the resulting images are produced properly.

My question is how do I resize only the images that are larger than 800px in the folder?
I have tried a few combinations without success.
For instance I tried:

Code: Select all

mogrify -resize 50% "800x>" *.jpg
without success.

Thanks for your help

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-25T10:37:50-07:00
by fmw42
Either of these work fine for me on IM 6.8.8.2 Q16 Mac OSX Snow Leopard

I put my images in tmp1 and resize to tmp2 (they were 2 copies of logo: converted to png and one copy of rose: converted to png -- logo is 640x480 and rose is 70x46)

cd desktop/tmp1


mogrify -path ../tmp2 -format png -resize 500x500\> *.png

mogrify -path ../tmp2 -format png -resize "500x500>" *.png


Try 500x500 rather than 500 or 500x. Perhaps it needs the -format argument

In the future, please also identify the version of IM that you are using.

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-25T11:32:04-07:00
by snibgo
I think ManuO wants to do something else.

For images larger than 800x800, he wants to resize them by 50%. For example, an image 3000x2000 will become 1500x1000.

For other images, smaller than 800x800, he doesn't want to resize them.

I don't think this is possible entirely within IM. It needs a script.

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-26T04:00:10-07:00
by ManuO
Thanks snibgo,

You translated my question as intended. Would you know any sample script for achieving the needed result?

I checked Fred's Scripts page and the closest I can see is the aspect http://www.fmwconcepts.com/imagemagick/aspect/index.php, but I don't think it does what I need.

PS: I am using ImageMagick 6.8.7-7.

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-26T06:01:32-07:00
by snibgo
What is your platform? For a Windows bat file:

Code: Select all

FOR /F "usebackq" %%D ^
IN (`identify -format "%%[fx:w>800&&h>800]" %1`) ^
DO if %%D==1 convert %1 -resize 50%% out.png
If you save that as possResize.bat, then typing ...

Code: Select all

possResize.bat x.jpg
... will resize x.jpg to make out.png if x.jpg is both higher and wider than 800. For smaller files, it does nothing.

This works on one file at a time. You could put that within a Windows For loop to process multiple files.

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-26T19:48:59-07:00
by ManuO
Thanks Snibgo,

I just tried the batch script without success. When I run it there is no out.png file created even though I don't see an error.

I am running Windows 7. I can also run on Mac OS 10.9 or Ubuntu 13.10.

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-26T20:06:45-07:00
by snibgo
Put these two lines at the top of your bat file.

Code: Select all

identify %1

identify -format "%%[fx:w>800&&h>800]" %1
What outputs do they give?

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-26T20:37:07-07:00
by ManuO
Thanks Snibgo,

Now I am getting the desired result based on the size of the image. How could I apply to a bunch of files at the same time?

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-26T20:49:25-07:00
by snibgo
Well, that's a scripting question, nothing to do with IM. But you could stick it inside an outer loop that loops through your files. Something like this:

Code: Select all

FOR %%F in (*.jpg) ^
DO FOR /F "usebackq" %%D ^
IN (`%IM%identify -format "%%[fx:w>800&&h>800]" %%F`) ^
DO if %%D==1 %IM%convert %%F -resize 50%% small\%%F

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-26T22:31:33-07:00
by fmw42
Here is an IM only approach that uses mogrify for all png images in a folder and calculations within +distort srt to resize by 1/2=0.5 and 0 rotation, if both w and h are greater than 800.

#create new folder folder2 to hold the results from folder1, since mogrify will not autocreate the folder.

cd folder1
mogrify -path path2/folder2 -format png +distort srt "%[fx:(w>800 && h>800)?0.5:1] 0" *.png

If you want to resize if only w or h are greater than 800, then do

cd folder1
mogrify -path path2/folder2 -format png +distort srt "%[fx:(w>800 || h>800)?0.5:1] 0" *.png

see
http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/distorts/#srt

only distort allows in-line calculations

If the result is too blurry (from the EWA resampling), you can set the filter to point and optionally use one of -interpolate methods.

Code: Select all

Average
Average4
Average9
Average16
Background
Bilinear
Blend
Integer
Mesh
Nearest
NearestNeighbor
Spline
see
http://www.imagemagick.org/Usage/distorts/#lookup

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-27T05:05:50-07:00
by ManuO
Thanks to both Snibgo and fmw42,

Both solutions worked fine.

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-28T07:24:04-07:00
by ingafreeman
Thanks a lot it help me too. Cool infooo

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-29T09:24:13-07:00
by ManuO
Hello again Snibgo,

Can you help with a last modification to my batch script?

Code: Select all

identify %1

identify -format "%%[fx:w>400]" %1

FOR /R %%a in (*.bmp) ^
DO FOR /F "usebackq" %%D ^
IN (`identify -format "%%[fx:w>400]" %%a`) ^
DO if %%D==1 mogrify "%%~fa" -resize 50%% %%a
The above works fine with folder and subfolder names not containing space but once there is a space it does not.

I get the following errors:

C:\imgs\test2>FOR /F "usebackq" %D IN (`identify -format "%[fx:w>400]" C:\imgs\test2\New folder\test3.bmp`) DO if %D == 1 mogrify "C:\imgs\test2\New folder\test3.bmp" -resize 50% C:\imgs\test2\New folder\test3.bmp
identify.exe: unable to open image `C:\imgs\test2\New': No such file or directory @ error/blob.c/OpenBlob/2643.
identify.exe: no decode delegate for this image format `C:\imgs\test2\New' @ error/constitute.c/ReadImage/555.
identify.exe: unable to open image `folder\test3.bmp': No such file or directory @ error/blob.c/OpenBlob/2643.

Would you know how to handle the folder name with spaces.

Thanks

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-29T10:37:17-07:00
by fmw42
probably put double quotes around the folder names, at least that works on Mac.

Re: Mogrify resize to 50 percent images larger than 800px

Posted: 2014-01-29T11:06:14-07:00
by snibgo
Yes, double quotes around the filenames.

Code: Select all

FOR /R %%a in (*.bmp) ^
DO FOR /F "usebackq" %%D ^
IN (`identify -format "%%[fx:w>400]" "%%a"`) ^
DO if %%D==1 mogrify "%%~fa" -resize 50%% "%%a"
As a rule, I ensure folders and filenames don't have spaces.