Sort images into folders based on resolution AND size

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?".
Post Reply
snaber
Posts: 8
Joined: 2016-02-04T09:34:31-07:00
Authentication code: 1151

Sort images into folders based on resolution AND size

Post by snaber »

Hello,

New to Imagemagick and struggling to figure this out.

I will have a folder where people submit images. I need Imagemagick to sort the images into "accepted" or "rejected" subfolders based on the following criteria:

1. 1200+ pixel dimension in either height or width
2. 200 or more pixels per inch.

If these criteria are met, the image is moved to the "accepted" folder. If the criteria are not met, the image is moved to the "rejected" folder.

If someone could show me a quick windows batch script that would be absolutely amazing. I'm confident I could then figure the script out and adjust it as I need.

Thanks in advance!
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Sort images into folders based on resolution AND size

Post by GeeMack »

snaber wrote:I will have a folder where people submit images. I need Imagemagick to sort the images into "accepted" or "rejected" subfolders based on the following criteria:

1. 1200+ pixel dimension in either height or width
2. 200 or more pixels per inch.

If these criteria are met, the image is moved to the "accepted" folder. If the criteria are not met, the image is moved to the "rejected" folder.
I'm using "ImageMagick 6.9.3-1 Q16 x64" on Windows 7 64. I built a batch file which seems to do what you're looking for. It checks to see if the directories "accepted" and "rejected" exist, and makes them if they don't. Then it loops through all the JPG files in the current directory to check their width and height, and their horizontal and vertical resolutions, by running an ImageMagick command like this...

Code: Select all

convert filename.jpg -format "%[w] %[h] %[resolution.x] %[resolution.y]" info:
If the file meets any of the dimension or resolution requirements it moves it to the "accepted" directory. If the file still exists after it gets past those tests, it moves it to the "rejected" directory. Inside a batch file all the percent signs "%" need to be doubled "%%" of course.

Here is the batch file that works on my system. Your mileage may vary.

Code: Select all

@ECHO OFF
SETLOCAL

SET ACCEPTDIR=accepted
SET REJECTDIR=rejected

IF NOT EXIST %ACCEPTDIR% MD %ACCEPTDIR%
IF NOT EXIST %REJECTDIR% MD %REJECTDIR%

FOR %%I IN ( *.jpg ) DO (
   FOR /F "tokens=1,2,3,4" %%A IN ( 'convert "%%I" -format "%%[w] %%[h] %%[resolution.x] %%[resolution.y]" info:' ) DO (
      IF EXIST "%%I" IF %%A GTR 11999 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" IF %%B GTR 11999 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" IF %%C GTR 199 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" IF %%D GTR 199 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" MOVE "%%I" %REJECTDIR%
   )
)

EXIT /B
This script doesn't really do any error checking other than to see if the file has already been moved before each next test. It doesn't traverse subdirectories. It doesn't check to see if "accepted" or "rejected" are files instead of directories (which could really wreck your day if they are). That's the bare bones that should get you started in the right direction.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Sort images into folders based on resolution AND size

Post by snibgo »

That should do it, provided the files have resolution in pixels per inch. %U gives the resolution units, which might also be be "Undefined" or "PixelsPerCentimeter".
snibgo's IM pages: im.snibgo.com
snaber
Posts: 8
Joined: 2016-02-04T09:34:31-07:00
Authentication code: 1151

Re: Sort images into folders based on resolution AND size

Post by snaber »

You all nailed it. Working like a charm! Many thanks.
snaber
Posts: 8
Joined: 2016-02-04T09:34:31-07:00
Authentication code: 1151

Re: Sort images into folders based on resolution AND size

Post by snaber »

Another question...

How would I first change all images with resolutions under 200dpi to 200dpi?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sort images into folders based on resolution AND size

Post by fmw42 »

I believe you will need to write a script loop over each image. Get its density and test if under 200 dpi and then use -density to modify it. See string formats %x, %y and %U

Code: Select all

identify -format "%x" yourimage
will return the value for x resolution.

Seems like GeeMack provide the basic code above to do that looping.

Just move the file to the desired folder if it is not under 200 dpi or convert the image with density 200 and save to the other folder if less than 200 dpi

Code: Select all

convert image.jpg -density 200 -units pixelsperinch somefolder/image.jpg
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Sort images into folders based on resolution AND size

Post by GeeMack »

snaber wrote:How would I first change all images with resolutions under 200dpi to 200dpi?
You can change the resolution of all JPGs in the directory to 200dpi with a command like this...

Code: Select all

mogrify -density 200 *.jpg
Of course that will change those with higher resolutions to 200dpi, too. You can modify only those with resolutions under 200dpi by incorporating a mogrify command into a script like the example I gave above. Maybe slip something like this in as the first couple of "IFs" inside that "FOR" loop...

Code: Select all

...
      IF EXIST "%%I" IF %%C LSS 200 mogrify -density 200 "%%I"
      IF EXIST "%%I" IF %%D LSS 200 mogrify -density 200 "%%I"
...
I haven't tested it, but that should get you pretty close.
snaber
Posts: 8
Joined: 2016-02-04T09:34:31-07:00
Authentication code: 1151

Re: Sort images into folders based on resolution AND size

Post by snaber »

Thanks for the help, again!

When I try to add the above code, i get this error: "mogrify.exe: unable to open image 'image.jpg': Permission denied @ error/blob.c/OpenBlob/2702"
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Sort images into folders based on resolution AND size

Post by GeeMack »

snaber wrote:When I try to add the above code, i get this error: "mogrify.exe: unable to open image 'image.jpg': Permission denied @ error/blob.c/OpenBlob/2702"
I can reproduce that error by modifying permissions on the file to deny write access. I'm not too familiar with Windows error messages, so it could be something else, but I think your solution will be with Windows and not with ImageMagick.
Post Reply