[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] 【已解决】利用bat 和nconvert 能否判定一张图片的分辨率奇偶自动裁剪

本帖最后由 tcasdsss 于 2014-9-12 17:45 编辑

就是要把所有奇数分辨率的裁成偶数
比如 899*1299就转化为 898*1298这样
如果本身就是898*1298就不要再变了

我先在能想到的办法就是
先ffmpeg转换一下
然后高-1 删除那些转好的图片和转坏了的mkv
再来一次ffmpeg
然后宽-1 删除那些转好的图片和转坏了的mkv
再来一次 删除那些转好的图片和转坏了的mkv
最后高-1 再来一次ffmpeg
相当的复杂……

TOP

本帖最后由 CrLf 于 2014-9-12 16:10 编辑

这需要频繁调用外部命令,改用 vbs 处理可能会快些,提供一个函数示例:
  1. Const testImage = "F:\test\测试\test.jpg"
  2. If cropImage(testImage,"png") Then
  3.    MsgBox testImage & " 符合条件"
  4. Else
  5.    MsgBox testImage & " 已被跳过"
  6. End If
  7. Function cropImage(imageFile,fileType)
  8.    Dim Img,IP,fso
  9.    Dim cropWidth,cropHeight,outFile,FormatID
  10.    saveFile =imageFile
  11.    
  12.    Set Img = CreateObject("WIA.ImageFile")
  13.    Set IP = CreateObject("WIA.ImageProcess")
  14.    Set fso = WScript.CreateObject("Scripting.Filesystemobject")
  15.    
  16.    Img.LoadFile imageFile
  17.    
  18.    cropWidth = Img.Width And 1
  19.    cropHeight =  Img.Height And 1
  20.    
  21.    If cropWidth Or cropHeight Then
  22.       cropImage = 1
  23.         
  24.       IP.Filters.Add IP.FilterInfos("Crop").FilterID
  25.       IP.Filters(1).Properties("Right") = cropWidth
  26.       IP.Filters(1).Properties("Bottom") = cropHeight
  27.       
  28.       Select Case LCase(fileType)
  29.          Case "bmp": FormatID = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
  30.          Case "png": FormatID = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
  31.          Case "gif": FormatID = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
  32.          Case "jpeg","jpg": FormatID = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
  33.          Case "tiff","tif": FormatID = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"
  34.       End Select
  35.       
  36.         If Len(FormatID)>0 Then
  37.          IP.Filters.Add IP.FilterInfos("Convert").FilterID
  38.          IP.Filters(2).Properties("FormatID").Value = FormatID
  39.            outFile = Replace(imageFile&"*" , fso.GetExtensionName(imageFile)&"*" , fileType)
  40.         End If
  41.         
  42.       Set Img = IP.Apply(Img)
  43.       If imageFile = outFile Then fso.DeleteFile imageFile
  44.       Img.SaveFile outFile
  45.    End If
  46. End Function
复制代码

TOP

本帖最后由 tcasdsss 于 2014-9-12 16:04 编辑

回复 3# CrLf


    怎么才能设定输出的图像为png格式呢 如果用jpg输出2次损失很大
好的 感谢

TOP

回复 3# CrLf


    如果用bat调用这个vbs 怎么才能把变量弄过去呢?

TOP

已修改
用 WScript.Arguments 来读取命令行参数,不过这样一来就违背尽量少调用外部命令的初衷了,建议把要处理的文件列出清单,保存在文本或用管道传递给 vbs,或者完全用 vbs 来实现

TOP

回复 6# CrLf
  1. @Echo Off
  2. dir *.jpg *.png /a /b /s >> list.txt
复制代码
这个导出的list怎么引入vbs?

TOP

  1. @echo off
  2. set "filename=1.jpg"
  3. for /f "tokens=1,3" %%i in ('nconvert.exe -info "%filename%" ^| findstr "Width Height"') do (
  4.     set "%%i=%%j"
  5. )
  6. set /a modW=Width%%2
  7. set /a modH=Height%%2
  8. set "change=0"
  9. if %modW% neq 0 (
  10.     set /a newW=Width-1
  11.     set "change=1"
  12. ) else (
  13.     set newW=%Width%
  14. )
  15. if %modH% neq 0 (
  16.     set /a newH=Height-1
  17.     set "change=1"
  18. ) else (
  19.     set newH=%Height%
  20. )
  21. if %change% equ 1 (
  22.     nconvert -overwrite -resize %newW% %newH% "%filename%"
  23. )
复制代码

TOP

回复 8# DAIC


    这个resize没有楼上的crop好 resize会有更大的损失

TOP

本帖最后由 tcasdsss 于 2014-9-12 16:47 编辑

回复 8# DAIC
  1.     @echo off
  2.     set "filename=1.jpg"
  3.     for /f "tokens=1,3" %%i in ('nconvert.exe -info "%filename%" ^| findstr "Width Height"') do (
  4.         set "%%i=%%j"
  5.     )
  6.     set /a modW=Width%%2
  7.     set /a modH=Height%%2
  8.     set "change=0"
  9.     if %modW% neq 0 (
  10.         set /a newW=Width-1
  11.         set "change=1"
  12.     ) else (
  13.         set newW=%Width%
  14.     )
  15.     if %modH% neq 0 (
  16.         set /a newH=Height-1
  17.         set "change=1"
  18.     ) else (
  19.         set newH=%Height%
  20.     )
  21.     if %change% equ 1 (
  22.         nconvert -overwrite -crop 0 0 %newW% %newH% "%filename%"
  23.     )
复制代码
这个样子可以实现jpg无损crop 可是png怎么办?
去了jpeg就行了…… 都是无损变换了

TOP

回复 8# DAIC


    那该怎么样批量处理呢?
  1.     dir *.jpg  *.png  /a /b /s >> list.txt   
  2.     for /f "delims=" %%a in (list.txt) do (
复制代码
这样没用

TOP

解决了…… 用了个特2*的办法
  1.         @Echo Off
  2.     dir *.jpg  *.png  /a /b /s >> list.txt  
复制代码
先来个列表
  1.         @Echo Off
  2.     for /f "delims=" %%b in (list.txt) do (
  3.     set a=%%b
  4.     more /e +1 list.txt > list.tmp
  5.     del list.txt
  6.     ren list.tmp list.txt
  7.     call 分辨率.bat
  8. )
复制代码
来个变量call过去 第一行删了
  1.     @Echo Off
  2.     set "filename=%a%"
  3.     for /f "tokens=1,3" %%i in ('nconvert.exe -info "%filename%" ^| findstr "Width Height"') do (
  4.         set "%%i=%%j"
  5. )
  6.     set /a modW=Width%%2
  7.     set /a modH=Height%%2
  8.     set "change=0"
  9.     if %modW% neq 0 (
  10.         set /a newW=Width-1
  11.         set "change=1"
  12.     ) else (
  13.         set newW=%Width%
  14.     )
  15.     if %modH% neq 0 (
  16.         set /a newH=Height-1
  17.         set "change=1"
  18.     ) else (
  19.         set newH=%Height%
  20.     )
  21.     if %change% equ 1 (
  22.         nconvert -overwrite -crop 0 0 %newW% %newH% "%filename%"
  23.         
  24.     )
  25.     call 2.bat
  26.     pause
复制代码
改完了再call回来……

TOP

回复 12# tcasdsss
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%a in ('dir /b /s /a-d *.jpg *.png') do (
  4.     for /f "tokens=1,3" %%i in ('nconvert.exe -info "%%a" ^| findstr "Width Height"') do (
  5.         set "%%i=%%j"
  6.     )
  7.     set /a modW=Width%%2
  8.     set /a modH=Height%%2
  9.     set "change=0"
  10.     if !modW! neq 0 (
  11.         set /a newW=Width-1
  12.         set "change=1"
  13.     ) else (
  14.         set newW=!Width!
  15.     )
  16.     if !modH! neq 0 (
  17.         set /a newH=Height-1
  18.         set "change=1"
  19.     ) else (
  20.         set newH=!Height!
  21.     )
  22.     if !change! equ 1 (
  23.         nconvert -overwrite -crop 0 0 !newW! !newH! "%%a"
  24.     )
  25. )
复制代码
1

评分人数

    • neorobin: set /a "n&~1" 不大于n的最大偶数 ..技术 + 1

TOP

回复 13# DAIC


    很好很强大

TOP

返回列表