Advertisement
slyfox1186

imagemagick-mogrify-optimize-image-ultra-settings.bat

Jun 2nd, 2021 (edited)
1,787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.66 KB | None | 0 0
  1. @ECHO OFF
  2. SETLOCAL ENABLEEXTENSIONS
  3. COLOR 0A
  4. TITLE ImageMagick - Optimize jpg files
  5.  
  6. :------------------------------------------------------------------------------------------------
  7.  
  8. REM Created By: SlyFox1186
  9. REM https://pastebin.com/u/slyfox1186
  10.  
  11. REM ImageMagick
  12. REM Optimize Image: Ultra Settings
  13.  
  14. REM This is my favorite settings for optimizing (shrinking) an image's
  15. REM file size with very little quality loss.
  16.  
  17. REM The script will prompt the user to select the
  18. REM input file type, choose either jpg or png.
  19.  
  20. REM Save this with a .bat extension and place it in
  21. REM the folder with your images.
  22.  
  23. REM You must do a test run and confirm the results are
  24. REM to your satisfaction before running on your favorite
  25. REM photos. We only do smart around here, right!
  26.  
  27. REM IMPORTANT! This Requires Imagemagick's mogrify.exe
  28.  
  29. REM You can install mogrify.exe by downloading the DLL
  30. REM version of ImageMagick (not the static version due
  31. REM to mogrify.exe not being included with it).
  32.  
  33. REM During the installation make sure to check
  34. REM "Install legacy programs" for mogrify.exe to be installed.
  35. REM Link: https://imagemagick.org/script/download.php
  36. REM Screenshot: https://i.imgur.com/fnMWY9Q.jpg
  37.  
  38. :------------------------------------------------------------------------------------------------
  39.  
  40. PUSHD "%~dp0"
  41. IF NOT "%1"=="MAX" START /MAX CMD /D /C %0 MAX & GOTO :EOF
  42.  
  43. :------------------------------------------------------------------------------------------------
  44.  
  45. REM Please change these two variable value's as needed
  46. REM Set the 'EDITOR_NAME' variable to whatever you want
  47. SET EDITOR_NAME=NOTEPAD
  48. REM SET the 'EDITOR_PATH' variable to the exe file path of your editor.
  49. SET EDITOR_PATH="%windir%\notepad.exe"
  50.  
  51. :------------------------------------------------------------------------------------------------
  52.  
  53. REM If detected prompt the user to delete all files from a prior run
  54. IF EXIST "Output" (
  55.     ECHO=
  56.     ECHO Output files detected: Delete them? & ECHO=
  57.     ECHO [1] Yes
  58.     ECHO [2] No & ECHO=
  59.     CHOICE /C 12 /N & CLS & ECHO=
  60.     IF ERRORLEVEL 2 GOTO NO_DELETE
  61.     IF ERRORLEVEL 1 RD /S /Q "Output"
  62. )
  63.  
  64. REM Create the output folder if missing
  65. IF NOT EXIST "Output" MKDIR "Output"
  66.  
  67. :------------------------------------------------------------------------------------------------
  68.  
  69. :NO_DELETE
  70. REM Prompt the user to choose the input file type
  71. CLS & ECHO=
  72. ECHO CHOOSE INPUT FILE TYPE & ECHO=
  73. ECHO [1] JPG
  74. ECHO [2] PNG
  75. ECHO [3] EXIT & ECHO=
  76.  
  77. CHOICE /C 123 /N & CLS & ECHO=
  78.  
  79. IF ERRORLEVEL 3 GOTO :EOF
  80. IF ERRORLEVEL 2 GOTO RUN_PNG
  81. IF ERRORLEVEL 1 GOTO RUN_JPG
  82.  
  83. :------------------------------------------------------------------------------------------------
  84. :RUN_PNG
  85. SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
  86. :------------------------------------------------------------------------------------------------
  87.  
  88. REM Run mogrify on all png files in script's working directory
  89. SET CNT=0
  90. FOR %%G IN (*.png) DO (
  91.     FOR /F "TOKENS=*" %%H IN ('MAGICK identify -format "%%[fx:w]x%%[fx:h]" "*.png"') DO (
  92.         SET /A CNT+=1
  93.         SET "file!CNT!=%%~nxG"
  94.         ECHO=
  95.         CALL ECHO [!CNT!] %%file!CNT!%%
  96.         ECHO=
  97.         START "" /B /WAIT /ABOVENORMAL MOGRIFY -monitor -path Output/ -filter Triangle -define filter:support=2 -thumbnail "%%H" -unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 82 -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 -format png "*.png"
  98.         IF "%%G"=="" ENDLOCAL
  99.     )
  100. )
  101.  
  102. REM If the script was successful open the file using the default program otherwise continue to error
  103. DIR /A-D "Output\*.png" >NUL 2>&1
  104. IF NOT ERRORLEVEL 1 (
  105.     COLOR 03
  106.     ECHO= & ECHO=
  107.     ECHO Mogrify Completed
  108.     ECHO=
  109.     TIMEOUT 5 >NUL
  110.     PUSHD "Output"
  111.     FOR %%I IN (*.png) DO (
  112.         START "" "%%~fI" & GOTO :EOF
  113.     )
  114. )
  115.  
  116. REM Jump to the error section to alert the user of a problem
  117. GOTO ERROR
  118.  
  119. :------------------------------------------------------------------------------------------------
  120. :RUN_JPG
  121. SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
  122. :------------------------------------------------------------------------------------------------
  123.  
  124. REM Run mogrify on all jpg files in script's working directory
  125. SET CNT=0
  126. FOR %%G IN (*.jpg) DO (
  127.     FOR /F "TOKENS=*" %%H IN ('MAGICK identify -format "%%[fx:w]x%%[fx:h]" "*.jpg"') DO (
  128.         SET /A CNT+=1
  129.         SET "file!CNT!=%%~nxG"
  130.         ECHO=
  131.         CALL ECHO [!CNT!] %%file!CNT!%%
  132.         ECHO=
  133.         START "" /B /WAIT /ABOVENORMAL MOGRIFY -monitor -path Output/ -filter Triangle -define filter:support=2 -thumbnail "%%H" -unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 82 -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 -format jpg "*.jpg"
  134.         IF "%%G"=="" ENDLOCAL
  135.     )
  136. )
  137.  
  138. REM If the script was successful open the file using the default program otherwise continue
  139. DIR /A-D "Output\*.jpg" >NUL 2>&1
  140. IF NOT ERRORLEVEL 1 (
  141.     COLOR 03
  142.     ECHO= & ECHO=
  143.     ECHO Mogrify Completed
  144.     ECHO=
  145.     TIMEOUT 5 >NUL
  146.     PUSHD "Output"
  147.     FOR %%I IN (*.jpg) DO (
  148.         START "" "%%~fI" & GOTO :EOF
  149.     )
  150. )
  151.  
  152. :------------------------------------------------------------------------------------------------
  153. :ERROR
  154. :------------------------------------------------------------------------------------------------
  155.  
  156. REM Echo an error message and open the script by using the 'EDITOR_PATH' variable
  157. CLS & ECHO=
  158. ECHO Error: Mogrify failed to convert the file^(s^). & ECHO=
  159. SET /P "dummy=Press [Enter] to open the script in: %EDITOR_NAME% "
  160. START "" /MAX %EDITOR_PATH% "%0"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement