Sort images into folders based on resolution AND size
Sort images into folders based on resolution AND size
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!
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!
- 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
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...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.
Code: Select all
convert filename.jpg -format "%[w] %[h] %[resolution.x] %[resolution.y]" info:
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
-
- 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
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
Re: Sort images into folders based on resolution AND size
You all nailed it. Working like a charm! Many thanks.
Re: Sort images into folders based on resolution AND size
Another question...
How would I first change all images with resolutions under 200dpi to 200dpi?
How would I first change all images with resolutions under 200dpi to 200dpi?
- 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
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
identify -format "%x" yourimage
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
- 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
You can change the resolution of all JPGs in the directory to 200dpi with a command like this...snaber wrote:How would I first change all images with resolutions under 200dpi to 200dpi?
Code: Select all
mogrify -density 200 *.jpg
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"
...
Re: Sort images into folders based on resolution AND size
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"
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"
- 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
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.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"