2016-12-03 15 views
0

我想寫一個oneliner可推動在一個目錄的TIF文件的唯一ID,然後運行ImageMajick的轉換,以將它們組合成多頁TIFS:管多PowerShell的成果轉化爲多個參數在一行

convert ..\image_data\ua-nws_00000991_001.tif ..\image_data\ua-nws_00000991_002.tif ..\image_data\ua-nws_00000991_003.tif ..\image_data\ua-nws_00000991_004.tif ua-nws_00000991_all.tif 

這可以工作,但我試圖自動化一堆每個都有幾個tif的ID。現在,我只是用一個ID,並使用「回聲」,而不是轉換:

echo "ua-nws_00000991" | % { $id=$_; ls ../image_data | ? { $_.FullName -match $id } | % { 
    echo $_.FullName } | Join-String -Separator '" "' | % { echo '"'$_'"' $id"_all.tif" } 
} 

但我無法打開的文件列表到的參數列表。我得到以下(我手動進行相對的在網上發帖的路徑):

" 
.\image_data\ua-nws_00000991_001.tif" ".\image_data\ua-nws_00000991_002.tif" ".\image_data\ua-nws_00000991_003.tif" "C:\Daniels_Stuff\Classes\Current\grad_ai\project\repo\article-reconstruction\image_data\ua-nws_00000991_004.tif" 
ua-nws_00000991_all.tif 

之前我加了雙引號,轉換正在演繹所有最終$_作爲一個參數。我添加引號是爲了強制轉換將它們看作多個參數。但我似乎無法擺脫那條新線。我想這可能是由最終echo引起的,但如果我用轉換代替它,我得到如下:

convert.exe: unable to open module file `C:\Program Files\ImageMagick-6.9.2-Q16\modules\coders\IM_MOD_RL_\ABSOLUTE\PATH\TO\MY\IMAGES\IMAGE_DATA\UA-NWS_00000991_001.TIF C_.dll': No such file or directory @ warning/module.c/GetMagickModulePath/674. 
convert.exe: no decode delegate for this image format `\ABSOLUTE\PATH\TO\MY\IMAGES\IMAGE_DATA\UA-NWS_00000991_001.TIF C' @ error/constitute.c/ReadImage/501. 
convert.exe: no images defined `ua-nws_00000991_all.tif' @ error/convert.c/ConvertImageCommand/3241. 

回答

1

雖然我不明白Powershell的瘋狂語法,但有兩個方面ImageMagick可能會幫助你... ...

看點1 - 文件名標準輸入

首先,你可以通過文件名的列表合併成一個單一的TIF上ImageMagick的標準輸入而不是作爲參數。所以,如果你有3頁的一本書,在3個文件名爲p1.tifp2.tifp3.tif,而不是做:

convert p*.tif book.tif 

,你也可以做

dir /b/s p*tif | convert @- book.tif 

雖然我生成上面的名稱使用DIR /B/S,我希望你想用你的Powershell命令替換它。

看點2 - 多個圖像向下單管

其次,可以從convert多次調用管的多個圖像進convert另外的最終調用使用「Magick圖像文件格式」MIFF)流:

這裏是一個例子,其中的兩個convert兩個調用每個發送圖像到最終convert從其MIFF輸入流加入圖片:

(convert p1.tif MIFF:- & convert p2.tif MIFF:-) | convert MIFF:- book.tif 

以下爲同樣的事情稍微不同的實施例:

FOR files *.TIF; DO 
    IF this file has the correct id 
     convert $this MIFF:- 
    ENDIF 
ENDDO | convert MIFF:- book.tif 
+0

謝謝。不知道這是可能的。我也可以這樣做,我認爲它更簡單(我認爲更快?):'gci .. \ image_data \ *。tif | group {$ _。Name -replace'_ \ d + \。tif $'} | %{$ _。Group.FullName |轉換「@ - 」「$($ _。Name)_all.tif」}' – xdhmoore

-1

你可以嘗試以下方法:在

get-childitem "image_data" -recurse -filter ua-nws_00000991*.tif | % { convert $_.Fullname } 

返回所有TIF文件「 image_data「目錄,併爲每個找到的tif文件調用convert

希望幫助

1

我不知道在哪裏Join-String從何而來,但我只能猜測,它的分解 - 因爲我沒有看到可以在其他地方換行的來源。

將一些測試文件的文件夾中:

Name      
----      
ua-nws_00000991_001.tif 
ua-nws_00000991_002.tif 
ua-nws_00000991_003.tif 
ua-nws_00000991_004.tif 
ua-nws_00000992_001.tif 
ua-nws_00000992_002.tif 
ua-nws_00000992_003.tif 
ua-nws_00000992_004.tif 

Get-ChildItemgci)列出它們,並通過從文件名剝離尾隨數他們組(Group-Object,又名group):

PS D:\t\New folder> gci *.tif | group { $_.Name -replace '_\d+\.tif$' } 

Count Name      Group                          
----- ----      -----                          
    4 ua-nws_00000991   {D:\t\New folder\ua-nws_00000991_001.tif, D:\t\New folder\ua-nws_00000991_002.tif, D:\t\New folder\ua-nws... 
    4 ua-nws_00000992   {D:\t\New folder\ua-nws_00000992_001.tif, D:\t\New folder\ua-nws_00000992_002.tif, D:\t\New folder\ua-nws... 

現在我們有組,其中組名是圖像ID,該ID的文件列表是組成員。整齊。現在運行轉換一次foreach%)組通過端與_full.tif完全佔據文件名作爲參數來進行轉換,並生成新文件名從組ID($_.name):

gci *.tif | group { $_.Name -replace '_\d+\.tif$' } |% { Start-Process convert -ArgumentList (@($_.group.fullname) + @("$($_.Name)_full.tif"))} 

這可能不會按原樣工作,我沒有安裝convert來嘗試它,並且您需要調整路徑。但我認爲這種方法可行。

+0

很好的解決方案。在大多數環境下,無路徑轉換將解析爲'C:\ Windows \ system32 \ convert.exe'並嘗試創建一個NTFS卷。 – LotPings

+0

謝謝,這工作完美!我還了解了'group',splat操作符和'Start-Process'! – xdhmoore

+0

此外,'Join-String'來自[powershell社區擴展](https://pscx.codeplex.com/)。看起來有一段時間沒有更新,所以也許它們只適用於較老的powershell版本。我在PS 4上。 – xdhmoore