Board logo

标题: [文件操作] 批处理如何在近万张图片中找到分辨率最大的一张图片? [打印本页]

作者: tcasdsss    时间: 2014-9-13 15:19     标题: 批处理如何在近万张图片中找到分辨率最大的一张图片?

  1.       @echo off
  2.         set %mW%=0
  3.         set %mH%=0
  4.         for /f "delims=" %%a in ('dir /b /s /a-d *.jpg *.png *.bmp') do (
  5.         for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (        
  6.         set  %W%=Width
  7.         set  %H%=Height
  8.         if %W% gtr %mW%
  9.         set  %mW%=Width
  10.         if %H% gtr %mH%
  11.         set  %mH%=Height      
  12.         )
  13.         )
  14.         pause
复制代码
我写的是这样的 可是无法运行……
作者: tcasdsss    时间: 2014-9-13 15:41

  1.     @echo off
  2.     setlocal enabledelayedexpansion
  3.         for /f "delims=" %%a in ('dir /b /s /a-d *.jpg *.png *.bmp') do (
  4.         for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (
  5. set "%%i=%%j"      
  6.         set  %W%=Width
  7.         set  %H%=Height
  8.         if %W% gtr %mW%
  9.         set  %mW%=Width
  10.         if %H% gtr %mH%
  11.         set  %mH%=Height      
  12.         )        
  13.         pause
复制代码
这样子也不行……
作者: neorobin    时间: 2014-9-13 16:14

回复 2# tcasdsss

%var% 不是 BAT 里变量必须采用的命名方式, 当然这样的变量名也是合法的. 实际上变量名比较自由

绝大多数字符都可以用于命名变量, 诸如此类: @#$%  甚至汉字, 甚至可以用数字作为起始字符(当然不好了)

%var% 是变量扩展, 要取变量 var 的值时可以用 %var% 为其赋值时这样: set var=something

分辨率最大如何理解? 应该算面积吧. 即  WIDTH * HEIGHT 这个乘积最大的吧

在找到最大分辨率时, 应该把该文件的路径给存下来吧, 所以可以设一个变量 set theFile=%%a

另外 WINDOWS 系统详细查看方式中, 可以选出 尺寸 列, 点击一下列标头, 即可排序, 可以试试.
作者: tcasdsss    时间: 2014-9-13 16:19

回复 3# neorobin


    我说错了…… 我要找的是 宽和高的最大值
现在最悲剧的是无法运行……
作者: tcasdsss    时间: 2014-9-13 16:22

回复 3# neorobin
  1.         set  W=Width
  2.         set  H=Height
  3. if  %W% gtr %mW%  
  4. set mW=Width
  5. if  %H% gtr %mH%  
  6. set mH=Height
复制代码
这样子赋值是不行的……
作者: neorobin    时间: 2014-9-13 16:31

回复 5# tcasdsss

nconvert.exe -info imagefile.jpg 输出信息格式是怎样的?
作者: tcasdsss    时间: 2014-9-13 16:41

本帖最后由 tcasdsss 于 2014-9-13 16:44 编辑

回复 6# neorobin
  1. for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (
复制代码
这句我是借鉴别人的……
他的方法输出的 宽和高是正确的

[attach]7670[/attach]
作者: CrLf    时间: 2014-9-13 17:12

  1. @set @n=0/*&echo off
  2. for /f "delims=" %%a in ('dir /b *.jpg ^| cscript -nologo -e:jscript "%~0"') do (
  3. echo 在这里对 %%a 做你想做的操作...
  4. )
  5. pause &exit /b */
  6. var Img = new ActiveXObject("WIA.ImageFile")
  7. var px
  8. var list = {max:0,files:[]}
  9. while(!WScript.StdIn.AtEndOfStream){
  10.     var file = WScript.StdIn.ReadLine()
  11. Img.LoadFile(file)
  12. px = Img.Width * Img.Height
  13. if(px > list.max){
  14. list.max = px
  15. list.files = [file]
  16. } else {
  17. list.files.push(file)
  18. }
  19. }
  20. for(var i in list.files){
  21. WScript.Echo(list.files[i])
  22. }
复制代码

作者: tcasdsss    时间: 2014-9-13 17:16

回复 8# CrLf


    不懂……
好像一部分不是批处理
这个怎么才能获得最大宽度 和高度的数据呢?
作者: tcasdsss    时间: 2014-9-13 17:20

回复 8# CrLf


    我现在想法是利用一个bat先找到这些图片中最大宽度和高度的数据 再call 另一个bat文件将变量传过去 通过nconvert来将图片分变率用canvas调整到最大值
作者: neorobin    时间: 2014-9-13 17:34

回复 7# tcasdsss

你的代码中有一个全角括号
  1. @echo off & setlocal enabledelayedexpansion
  2. set /a Width=0, Height=0
  3. for /f "delims=" %%a in ('dir /b /s /a-d *.jpg *.png *.bmp') do (
  4. for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (
  5. if %%j gtr !%%i! set "%%i=%%j"
  6. )
  7. )
  8. set Width
  9. set Height
  10. pause
复制代码

作者: CrLf    时间: 2014-9-13 17:39

虽然赶脚不需要我了,但还是改着玩玩...
  1. @set @n=0/*&echo off
  2. dir /b *.jpg | cscript -nologo -e:jscript "%~0" "格式化输出:高度[$height] 宽度[$width] 像素[$px] 文件[\'$file\']"
  3. rem 按指定格式输出取得的信息
  4. pause
  5. for /f "delims=" %%a in ('dir /b *.jpg ^| cscript -nologo -e:jscript "%~0"') do (
  6. echo 在这里对 %%a 做你想做的操作...
  7. )
  8. rem 处理示例
  9. pause &exit /b */
  10. var format = '$file'
  11. if(WScript.Arguments.length){
  12. format = WScript.Arguments.Item(0).replace(/\$\$/g,'{$$$$}')
  13. format = format.replace(/\\r/g,'\r')
  14. format = format.replace(/\\n/g,'\n')
  15. format = format.replace(/\\t/g,'\t')
  16. format = format.replace(/\\'/g,'"')
  17. }
  18. var Img = new ActiveXObject("WIA.ImageFile")
  19. var px
  20. var list = {px:0,files:[]}
  21. while(!WScript.StdIn.AtEndOfStream){
  22.     var file = WScript.StdIn.ReadLine()
  23. Img.LoadFile(file)
  24. px = Img.Width * Img.Height
  25. if(px > list.px){
  26. list = {px:px,width:Img.Width,height:Img.Height,files:[file]}
  27. } else {
  28. list.files.push(file)
  29. }
  30. }
  31. var output = format.replace(/\$px/gi,list.px)
  32. output = output.replace(/\$height/gi,list.height)
  33. output = output.replace(/\$width/gi,list.width)
  34. for(var i in list.files){
  35. output = output.replace(/\$file/gi,list.files[i])
  36. WScript.Echo(output.replace(/\{\$\$\}/gi,'$$$$'))
  37. }
复制代码
支持简单的格式化输出
作者: CrLf    时间: 2014-9-13 17:42

话说楼主你把 height 和 width 分开判断,怎么能保证是同一张图片的高和宽呢?
作者: tcasdsss    时间: 2014-9-13 17:46

回复 13# CrLf


    我现在目的是要把一批图片压缩成hevc流文件 以提高压缩率 为了保证视频可以符合hevc流的要求 又不损失数据 我们就要把 图片中的最长最高的值得到 从而方便hevc压缩
作者: tcasdsss    时间: 2014-9-13 17:47

回复 11# neorobin


    吊…… 比我的简洁好多……
作者: CrLf    时间: 2014-9-13 17:48

回复 14# tcasdsss


    噢,怪不得,那当我啥都没发好了...
作者: tcasdsss    时间: 2014-9-13 17:53

本帖最后由 tcasdsss 于 2014-9-13 18:02 编辑

回复 11# neorobin
  1.     @echo off & setlocal enabledelayedexpansion
  2.     set /a Width=0, Height=0
  3.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i     "jpg$ png$ bmp$"') do (
  4.      for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^|          findstr "Width Height"') do (
  5.                     if %%j gtr !%%i! set "%%i=%%j"
  6.             )
  7.     )
  8.     set Width
  9.     set Height
  10. call 1.bat
  11.     pause
复制代码
这样就不能用了…… 加入了别人的检索代码之后

我知道哪错了……
作者: tcasdsss    时间: 2014-9-13 17:55

回复 16# CrLf


    我准备把他合成为一个转图片 音频的bat集 以后没准你也会用得上
作者: tcasdsss    时间: 2014-9-13 17:59

我其实没做什么 就是一直在问而已……
作者: tcasdsss    时间: 2014-9-13 18:01

回复 11# neorobin


    现在这个命令只能在一个目录下才可以 我希望他可以弄到所有子目录下 就完美了
作者: neorobin    时间: 2014-9-13 18:02

回复 17# tcasdsss


第 3 行 的 %%i   和   第 4 行 的 %%a    不对应,    当然出问题了,  %%a  无定义了!
作者: tcasdsss    时间: 2014-9-13 18:03

回复 21# neorobin


    %folder%我也错了……
作者: neorobin    时间: 2014-9-13 18:05

回复 22# tcasdsss


folder  你事先给它赋上正确的值就 OK
作者: tcasdsss    时间: 2014-9-13 18:07

本帖最后由 tcasdsss 于 2014-9-13 18:13 编辑

回复 23# neorobin


    利用跳转 可不可以把所有的bat合成一个? 我怕变量不对

  可以! 那边量怎么清空呢? 直接赋值为0?
作者: tcasdsss    时间: 2014-9-13 18:44

本帖最后由 tcasdsss 于 2014-9-13 19:34 编辑

回复 21# neorobin


    搞定了
虽然毫无美感……
  1. @echo off
  2. :start
  3. echo 目录及子目录下所有:分辨率至偶数(1)图片改名(2) 解压图片(3)
  4. echo 解压音频(4) 解压视频流(5) 强制分辨率为最大(6)
  5. echo 压缩图片(7) 压缩音频(8) 压缩为视频流(9)
  6. echo 删除hevc (q) 删除图片(e) 删除opus(r)
  7. echo 删除音频(t)
  8. echo 相同或不同分辨率图片转为视频流 并删除图片(z)
  9. echo 分辨率至偶数再单张压缩图片 并删除图片 (x)
  10. set choice=
  11. set /p choice=
  12. if /i "%choice%"=="1" goto 1
  13. if /i "%choice%"=="2" goto 2
  14. if /i "%choice%"=="3" goto 3
  15. if /i "%choice%"=="4" goto 4
  16. if /i "%choice%"=="5" goto 5
  17. if /i "%choice%"=="6" goto 6
  18. if /i "%choice%"=="7" goto 7
  19. if /i "%choice%"=="8" goto 8
  20. if /i "%choice%"=="9" goto 9
  21. if /i "%choice%"=="q" goto q
  22. if /i "%choice%"=="e" goto e
  23. if /i "%choice%"=="r" goto r
  24. if /i "%choice%"=="t" goto t
  25. if /i "%choice%"=="z" goto z
  26. if /i "%choice%"=="x" goto x
  27. goto start
  28. :1
  29.     setlocal enabledelayedexpansion
  30.     for /f "delims=" %%a in ('dir /b /s /a-d *.jpg *.png *.bmp') do (
  31.         for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (
  32.             set "%%i=%%j"
  33.         )
  34.         set /a modW=Width%%2
  35.         set /a modH=Height%%2
  36.         set "change=0"
  37.         if !modW! neq 0 (
  38.             set /a newW=Width-1
  39.             set "change=1"
  40.         ) else (
  41.             set newW=!Width!
  42.         )
  43.         if !modH! neq 0 (
  44.             set /a newH=Height-1
  45.             set "change=1"
  46.         ) else (
  47.             set newH=!Height!
  48.         )
  49.         if !change! equ 1 (
  50.             nconvert -overwrite -crop 0 0 !newW! !newH! "%%a"
  51.         )
  52.     )
  53. goto start
  54. :2
  55. setlocal enabledelayedexpansion
  56. set n=0
  57. set "folder=%cd%"
  58. for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "jpg$ png$ bmp$"') do (
  59.     set /a n+=1
  60.     set "%%i=!n!"
  61.     set mod=0000!n!
  62.     ren "%%~fi" !mod:~-5!%%~xi
  63. )
  64. goto start
  65. :3
  66.     set "PATH=%PATH%;%cd%\bin"
  67.     set "folder=%cd%"
  68.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "hevc$"') do (
  69.         cd /d "%%~dpi"
  70.         ffmpeg.exe -i "%%~nxi"  "%%~ni.png"
  71.     )
  72. goto start
  73. :4
  74.     set "PATH=%PATH%;%cd%\bin"
  75.     set "folder=%cd%"
  76.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "opus$"') do (
  77.         cd /d "%%~dpi"
  78.         ffmpeg.exe -i "%%~nxi"  "%%~ni.flac"
  79.     )
  80. goto start
  81. :5
  82.     set "PATH=%PATH%;%cd%\bin"
  83.     set "folder=%cd%"
  84.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "hevc$"') do (
  85.         cd /d "%%~dpi"
  86.         ffmpeg.exe -i "%%~nxi"  "%%05d.png"
  87.     )
  88. goto start
  89. :6
  90.     set /a Width=0, Height=0
  91.     for /f "delims=" %%a in ('dir /s /a-d /b "%cd%" ^| findstr /i     "jpg$ png$ bmp$"') do (
  92.      for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^|          findstr "Width Height"') do (
  93.                     if %%j gtr !%%i! set "%%i=%%j"
  94.             )
  95.     )
  96.     set Width
  97.     set Height
  98.    
  99.     setlocal enabledelayedexpansion
  100.     for /f "delims=" %%a in ('dir /b /s /a-d *.jpg *.png *.bmp') do (
  101.         for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (
  102.             set "%%i=%%j"
  103.         )
  104.             
  105.             nconvert -overwrite -canvas %Width% %Height% center "%%a"
  106.         )
  107.     )
  108. goto start
  109. :7
  110.     set "PATH=%PATH%;%cd%\bin"
  111.     set "folder=%cd%"
  112.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "jpg$ png$ bmp$"') do (
  113.         cd /d "%%~dpi"
  114.         ffmpeg.exe -i "%%~nxi" -pix_fmt yuv420p -vcodec libx265 -preset placebo -x265-params qp=30    "%%~ni.hevc"
  115.     )
  116. goto start
  117. :8
  118.     set "PATH=%PATH%;%cd%\bin"
  119.     set "folder=%cd%"
  120.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "wav$ ogg$ mp3$ flac$"') do (
  121.         cd /d "%%~dpi"
  122.         ffmpeg.exe  -i "%%~nxi" -vcodec libopus -ab 64k   "%%~ni.opus"
  123.     )
  124. goto start
  125. :9
  126.     set "PATH=%PATH%;%cd%\bin"
  127.     set "folder=%cd%"
  128.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "png$ jpg$"') do (
  129.         cd /d "%%~dpi"
  130.         ffmpeg.exe  -i "%%~nxi"  "%%~ni.bmp"
  131.     )
  132.     set "PATH=%PATH%;%cd%\bin"
  133.     set "folder=%cd%"
  134.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "bmp$"') do (
  135.         cd /d "%%~dpi"
  136.         ffmpeg.exe -f image2 -i "%%05d.bmp" -pix_fmt yuv420p -vcodec libx265 -preset placebo -x265-params qp=25    "%folder%\1.hevc"
  137. goto start  
  138. )
  139. :q
  140. del /f /s *.hevc >nul
  141. goto start
  142. :e
  143. del /f /s *.jpg *.bmp *.png >nul
  144. goto start
  145. :r
  146. del /f /s *.opus >nul
  147. goto start
  148. :t
  149. del /f /s *.wav *.ogg *.mp3 *.flac >nul
  150. goto start
  151. :z
  152.     setlocal enabledelayedexpansion
  153. set n=0
  154. set "folder=%cd%"
  155. for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "jpg$ png$ bmp$"') do (
  156.     set /a n+=1
  157.     set "%%i=!n!"
  158.     set mod=0000!n!
  159.     ren "%%~fi" !mod:~-5!%%~xi
  160. )
  161.     setlocal enabledelayedexpansion
  162.     for /f "delims=" %%a in ('dir /b /s /a-d *.jpg *.png *.bmp') do (
  163.         for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (
  164.             set "%%i=%%j"
  165.         )
  166.         set /a modW=Width%%2
  167.         set /a modH=Height%%2
  168.         set "change=0"
  169.         if !modW! neq 0 (
  170.             set /a newW=Width-1
  171.             set "change=1"
  172.         ) else (
  173.             set newW=!Width!
  174.         )
  175.         if !modH! neq 0 (
  176.             set /a newH=Height-1
  177.             set "change=1"
  178.         ) else (
  179.             set newH=!Height!
  180.         )
  181.         if !change! equ 1 (
  182.             nconvert -overwrite -crop 0 0 !newW! !newH! "%%a"
  183.         )
  184.     )
  185.     set /a Width=0, Height=0
  186.     for /f "delims=" %%a in ('dir /s /a-d /b "%cd%" ^| findstr /i     "jpg$ png$ bmp$"') do (
  187.      for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^|          findstr "Width Height"') do (
  188.                     if %%j gtr !%%i! set "%%i=%%j"
  189.             )
  190.     )
  191.     set Width
  192.     set Height
  193.    
  194.     setlocal enabledelayedexpansion
  195.     for /f "delims=" %%a in ('dir /b /s /a-d *.jpg *.png *.bmp') do (
  196.         for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (
  197.             set "%%i=%%j"
  198.         )
  199.             
  200.             nconvert -overwrite -canvas %Width% %Height% center "%%a"
  201.         )
  202.     )
  203.     set "PATH=%PATH%;%cd%\bin"
  204.     set "folder=%cd%"
  205.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "png$ jpg$"') do (
  206.         cd /d "%%~dpi"
  207.         ffmpeg.exe  -i "%%~nxi"  "%%~ni.bmp"
  208.     )
  209.     set "PATH=%PATH%;%cd%\bin"
  210.     set "folder=%cd%"
  211.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i   "bmp$"') do (
  212.         cd /d "%%~dpi"
  213.         ffmpeg.exe -f image2 -i "%%05d.bmp" -pix_fmt yuv420p -vcodec      libx265 -preset placebo -x265-params qp=25    "%folder%\1.hevc"
  214. goto e
  215.     )
  216. :x
  217.     setlocal enabledelayedexpansion
  218. set n=0
  219. set "folder=%cd%"
  220. for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "jpg$ png$ bmp$"') do (
  221.     set /a n+=1
  222.     set "%%i=!n!"
  223.     set mod=0000!n!
  224.     ren "%%~fi" !mod:~-5!%%~xi
  225. )
  226.     setlocal enabledelayedexpansion
  227.     for /f "delims=" %%a in ('dir /b /s /a-d *.jpg *.png *.bmp') do (
  228.         for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (
  229.             set "%%i=%%j"
  230.         )
  231.         set /a modW=Width%%2
  232.         set /a modH=Height%%2
  233.         set "change=0"
  234.         if !modW! neq 0 (
  235.             set /a newW=Width-1
  236.             set "change=1"
  237.         ) else (
  238.             set newW=!Width!
  239.         )
  240.         if !modH! neq 0 (
  241.             set /a newH=Height-1
  242.             set "change=1"
  243.         ) else (
  244.             set newH=!Height!
  245.         )
  246.         if !change! equ 1 (
  247.             nconvert -overwrite -crop 0 0 !newW! !newH! "%%a"
  248.         )
  249.     )
  250.     set "PATH=%PATH%;%cd%\bin"
  251.     set "folder=%cd%"
  252.     for /f "delims=" %%i in ('dir /s /a-d /b "%folder%" ^| findstr /i "jpg$ png$ bmp$"') do (
  253.         cd /d "%%~dpi"
  254.         ffmpeg.exe -i "%%~nxi" -pix_fmt yuv420p -vcodec libx265 -preset placebo -x265-params qp=30    "%%~ni.hevc"
  255.     )
  256.   del /f /s *.jpg *.bmp *.png >nul
  257.   goto start
复制代码
问题解决 就差精简代码了……
作者: neorobin    时间: 2014-9-13 18:49

回复 24# tcasdsss


set "var="  即可, 双引号不是必要的,  但要保证 = 后面没有尾随空格之类的字符
作者: tcasdsss    时间: 2014-9-13 19:00

本帖最后由 tcasdsss 于 2014-9-13 19:31 编辑

回复 26# neorobin


    你试一下 现在我的代码有点毛病 压缩视频流会变成 压缩完后还会重复压……
没问题了……




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2