Page 1 of 1
Sort images into folders based on resolution AND size
Posted: 2016-02-04T09:39:38-07:00
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!
Re: Sort images into folders based on resolution AND size
Posted: 2016-02-04T21:48:26-07:00
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.
Re: Sort images into folders based on resolution AND size
Posted: 2016-02-04T23:19:55-07:00
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".
Re: Sort images into folders based on resolution AND size
Posted: 2016-02-05T06:32:06-07:00
by snaber
You all nailed it. Working like a charm! Many thanks.
Re: Sort images into folders based on resolution AND size
Posted: 2016-02-25T07:25:37-07:00
by snaber
Another question...
How would I first change all images with resolutions under 200dpi to 200dpi?
Re: Sort images into folders based on resolution AND size
Posted: 2016-02-25T10:05:38-07:00
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
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
Re: Sort images into folders based on resolution AND size
Posted: 2016-02-25T10:40:15-07:00
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...
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.
Re: Sort images into folders based on resolution AND size
Posted: 2016-02-25T14:20:30-07:00
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"
Re: Sort images into folders based on resolution AND size
Posted: 2016-02-25T20:41:54-07:00
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.