Page 1 of 1

Adding DPI conversion to windows batch file

Posted: 2017-01-03T10:26:28-07:00
by snaber
Hello Magicians,

I have a windows batch file that I use to sort images based on their height/width and DPI. Currently, I run a photoshop script on the images first to convert them all to 200dpi. What I'd like to do, is remove that step from the process and have the batch file convert the images to 200 DPI for me. Is that possible? Here's what the batch file looks like:

Code: Select all

@ECHO OFF
SETLOCAL

SET ACCEPTDIR=accepted
SET REJECTDIR=rejected
SET ACCEPTDIRWEB=accepted_web

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

FOR %%I IN ( *.jpeg *.jpg *.tiff *.tif *.png *.bmp) 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 1199 IF %%D GTR 199 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" IF %%B GTR 1199 IF %%D GTR 199 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" IF %%A GTR 1199 IF %%C GTR 199 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" IF %%B GTR 1199 IF %%C GTR 199 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" IF %%A GTR 378 IF %%D GTR 199 MOVE "%%I" %ACCEPTDIRWEB%
      IF EXIST "%%I" IF %%B GTR 378 IF %%D GTR 199 MOVE "%%I" %ACCEPTDIRWEB%
      IF EXIST "%%I" IF %%A GTR 378 IF %%C GTR 199 MOVE "%%I" %ACCEPTDIRWEB%
      IF EXIST "%%I" IF %%B GTR 378 IF %%C GTR 199 MOVE "%%I" %ACCEPTDIRWEB%
      IF EXIST "%%I" MOVE "%%I" %REJECTDIR%
   )
)
FOR %%I IN ( *.eps *.gif *.pdf *.doc) DO (
      IF EXIST "%%I" MOVE "%%I" %REJECTDIR%
   )

EXIT /B

Re: Adding DPI conversion to windows batch file

Posted: 2017-01-03T10:33:46-07:00
by fmw42
For each image use,

Code: Select all

convert image -density 200 -units pixelsperinch image

Re: Adding DPI conversion to windows batch file

Posted: 2017-01-03T10:52:02-07:00
by snaber
Thanks for the reply, Fred! Where exactly would I put that in the code example that I provided?

Re: Adding DPI conversion to windows batch file

Posted: 2017-01-03T12:08:26-07:00
by fmw42
I do not script on Windows. But I think it should work by adding it as follows

convert "%%I" -density 200 -units pixelsperinch -format "%%[w] %%[h] %%[resolution.x] %%[resolution.y]" info:'

But if you fix the density to 200, then your resolution will always be 200 and then why do you need to list if from the above information? The sort on density will be meaningless since it is always 200 dpi.

Re: Adding DPI conversion to windows batch file

Posted: 2017-01-03T15:19:41-07:00
by snaber
Thanks!

Yeah, I've removed the DPI check as it was not longer necessary. Here's what I came up with that seems to be working.

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 ( *.jpeg *.jpg *.tiff *.tif *.png *.bmp *.PNG *.gif) DO (
   FOR /F "tokens=1,2" %%A IN ( 'convert "%%I" -format "%%[w] %%[h]" info:' ) DO (
      IF EXIST "%%I" mogrify -units pixelsperinch -density 200x200 -fuzz 1%% -trim +repage "%%I"
      IF EXIST "%%I" IF %%A GTR 799 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" IF %%B GTR 799 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" MOVE "%%I" %REJECTDIR%
   )
)

FOR %%I IN ( *.eps *.gif *.pdf ) DO (
      IF EXIST "%%I" MOVE "%%I" %REJECTDIR%
   )

EXIT /B

Re: Adding DPI conversion to windows batch file

Posted: 2017-01-03T17:15:19-07:00
by fmw42
That seems reasonable.

Or

convert "%%I" -density 200 -units pixelsperinch -format "%%[w] %%[h]" -write info: "%%I"

Re: Adding DPI conversion to windows batch file

Posted: 2017-01-19T11:44:39-07:00
by snaber
Thanks so much for the guidance thus far!

My work has thrown be a curveball and now wants to add an additional sort of the images. Here's what needs to happen:

1. Convert images to 300dpi
2. If greater than 1799 pixels in height or width, move to accepted DIR
3. If not greater than 1799 pixels, convert to 72dpi
4. If greater than 799 pixels in height or width, move to accepted_web DIR
5. If not, move to rejected folder

How would I go about adding that second layer? Something like this?

Code: Select all

@ECHO OFF
SETLOCAL

SET ACCEPTDIR=accepted
SET REJECTDIR=rejected
SET ACCEPTDIRWEB=accepted_web

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

FOR %%I IN ( *.jpeg *.jpg *.tiff *.tif *.png *.bmp *.gif ) DO (
   FOR /F "tokens=1,2" %%A IN ( 'convert "%%I" -format "%%[w] %%[h]" info:' ) DO (
      IF EXIST "%%I" mogrify -strip -units pixelsperinch -density 300x300 -fuzz 1%% -trim +repage "%%I"
      IF EXIST "%%I" IF %%A GTR 1799 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" IF %%B GTR 1799 MOVE "%%I" %ACCEPTDIR%
      IF EXIST "%%I" mogrify -strip -units pixelsperinch -density 72x72 "%%I"
      IF EXIST "%%I" IF %%A GTR 799 MOVE "%%I" %ACCEPTDIRWEB%
      IF EXIST "%%I" IF %%B GTR 799 MOVE "%%I" %ACCEPTDIRWEB%
      IF EXIST "%%I" MOVE "%%I" %REJECTDIR%
   )
)
FOR %%I IN ( *.eps *.pdf ) DO (
      IF EXIST "%%I" MOVE "%%I" %REJECTDIR%
   )

EXIT /B