slyfox1186

imagemagick_optimize_small_or_medium_jpg_files.bat

Sep 17th, 2021 (edited)
719
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @ECHO OFF
  2. SETLOCAL ENABLEEXTENSIONS
  3. COLOR 0A
  4. TITLE USE IMAGEMAGICK’S CACHE FILES TO OPTIMIZE SMALL AND MEDIUM-SIZED JPG FILES
  5.  
  6. :----------------------------------------------------------------------------------
  7.  
  8. REM FOR USE ON JPG SIZES 1-5 MEGABYTES
  9. REM USE MY OTHER SCRIPTS FOR < 5 MEGABYTE JPG FILES
  10.  
  11. REM PLACE THIS SCRIPT IN THE FOLDER CONTAINING THE JPG FILES
  12. REM THE OPTIMIZED IMAGES WILL BE PLACED IN A SUB FOLDER NAMED "OUTPUT"
  13.  
  14. REM MAKE SURE TO POINT THE VARIABLE "CONVERT" BELOW TO THE FULL PATH
  15. REM OF IMAGEMAGICK'S "CONVERT.EXE" FILE.
  16.  
  17. REM SAVE THIS SCRIPT USING A .BAT EXTENSION
  18.  
  19. :----------------------------------------------------------------------------------
  20.  
  21. PUSHD "%~dp0"
  22. IF NOT "%1"=="MAX" START /MAX CMD /D /C %0 MAX & GOTO :EOF
  23.  
  24. :----------------------------------------------------------------------------------
  25.  
  26. SET CONVERT=%ProgramFiles%\ImageMagick\convert.exe
  27.  
  28. :----------------------------------------------------------------------------------
  29.  
  30. REM CREATE OUTPUT FOLDERS
  31. IF NOT EXIST "Cache_Files" MD "Cache_Files"
  32. IF NOT EXIST "Output" MD "Output"
  33.  
  34. :----------------------------------------------------------------------------------
  35.  
  36. REM FIND ALL JPG FILES AND CONVERT THEM TO MPC FORMAT
  37. SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
  38. SET CNT=0
  39. FOR %%G IN (*.jpg) DO (
  40.     FOR /F "TOKENS=1-2" %%H IN ('identify +ping -format "%%w %%h" "%%G"') DO (
  41.         SET /A CNT+=1
  42.         ECHO=
  43.         ECHO [!CNT!] CACHING FILES: %%~nG.mpc ^+ %%~nG.cache
  44.         ECHO=
  45.         ECHO :----------------------------------------------------------------------------------
  46.         ECHO=
  47.         "%CONVERT%" "%%G" -monitor -filter Triangle -define filter:support=2 -thumbnail "%%Hx%%I" -strip -unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 91 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB "Cache_Files\%%~nG.mpc"
  48.         CLS
  49.     )
  50. )
  51.  
  52. :----------------------------------------------------------------------------------
  53. :MAKE_JPG
  54. :----------------------------------------------------------------------------------
  55.  
  56. REM CONVERT CACHED FILES INTO OPTIMIZED JPG IMAGES
  57. SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
  58. SET CNT=0
  59. FOR %%J IN ("Cache_Files\*.mpc") DO (
  60.     SET /A CNT+=1
  61.     ECHO=
  62.     ECHO [!CNT!] CREATING JPG: %%~nJ.jpg
  63.     ECHO=
  64.     "%CONVERT%" "%%J" -monitor "Output\%%~nJ.jpg"
  65.     CLS
  66.     )
  67. )
  68.  
  69. :----------------------------------------------------------------------------------
  70.  
  71. REM DELETE THE TEMP CACHE FOLDER AND OPEN THE OUTPUT FOLDER IN EXPLORER
  72. :END
  73. RD /S /Q "Cache_Files"
  74. START "" /MAX EXPLORER "Output"
RAW Paste Data