Adding DPI conversion to windows batch file

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

Adding DPI conversion to windows batch file

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

Re: Adding DPI conversion to windows batch file

Post by fmw42 »

For each image use,

Code: Select all

convert image -density 200 -units pixelsperinch image
snaber
Posts: 8
Joined: 2016-02-04T09:34:31-07:00
Authentication code: 1151

Re: Adding DPI conversion to windows batch file

Post by snaber »

Thanks for the reply, Fred! Where exactly would I put that in the code example that I provided?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding DPI conversion to windows batch file

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

Re: Adding DPI conversion to windows batch file

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

Re: Adding DPI conversion to windows batch file

Post by fmw42 »

That seems reasonable.

Or

convert "%%I" -density 200 -units pixelsperinch -format "%%[w] %%[h]" -write info: "%%I"
snaber
Posts: 8
Joined: 2016-02-04T09:34:31-07:00
Authentication code: 1151

Re: Adding DPI conversion to windows batch file

Post 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
Post Reply