Mogrify resize to 50 percent images larger than 800px

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
ManuO
Posts: 7
Joined: 2014-01-24T18:06:19-07:00
Authentication code: 6789

Mogrify resize to 50 percent images larger than 800px

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
ManuO
Posts: 7
Joined: 2014-01-24T18:06:19-07:00
Authentication code: 6789

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
ManuO
Posts: 7
Joined: 2014-01-24T18:06:19-07:00
Authentication code: 6789

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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?
snibgo's IM pages: im.snibgo.com
ManuO
Posts: 7
Joined: 2014-01-24T18:06:19-07:00
Authentication code: 6789

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
ManuO
Posts: 7
Joined: 2014-01-24T18:06:19-07:00
Authentication code: 6789

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

Post by ManuO »

Thanks to both Snibgo and fmw42,

Both solutions worked fine.
ingafreeman
Posts: 1
Joined: 2014-01-28T07:18:48-07:00
Authentication code: 6789

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

Post by ingafreeman »

Thanks a lot it help me too. Cool infooo
ManuO
Posts: 7
Joined: 2014-01-24T18:06:19-07:00
Authentication code: 6789

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

probably put double quotes around the folder names, at least that works on Mac.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply