Pipe multiline powershell results into multiple arguments in one line

xdhmoore

I'm trying to write a oneliner that gets unique ids of tif files in a directory, and then runs ImageMajick's convert to combine them together into multipage tifs:

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

This works, but I'm trying to automate it for a bunch of ids that have several tifs each. For now, I'm just using one id and using "echo" instead of convert:

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

But I'm having trouble turning the list of files into a list of arguments. I'm getting the following (I've manually made the paths relative for online posting):

"
.\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

Before I added the double quotes, convert was interpreting all of the final $_ as one argument. I'm adding quotes in order to force convert to see them as multiple arguments. But I can't seem to get rid of that newline. I think it might be caused by the final echo, but if I replace it with convert, I get the following:

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.
Mark Setchell

Whilst I don't understand Powershell's mad syntax, there are two aspects to ImageMagick that may help you out...

Aspect 1 - Filenames on stdin

Firstly, you can pass a list of filenames to merge into a single TIF on ImageMagick's stdin rather than as parameters. So, if you have 3 pages of a book, in 3 files called p1.tif, p2.tif and p3.tif, rather than do:

convert p*.tif book.tif

you can also do

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

Whilst I am generating the names above using DIR /B/S, I expect you would want to replace that with your Powershell commands.

Aspect 2 - Multiple images down single pipe

Secondly, you can pipe multiple images from multiple invocations of convert into a further final invocation of convert using "Magick Image File Format" (MIFF) streaming:

Here is one example, where two invocations of convert both send an image each to a final convert that joins the images from its MIFF input stream:

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

Here is a slightly different example of the same thing:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Powershell pipe and command line arguments

From Dev

Watch + pipe + multiple arguments

From Dev

Powershell, pipe list of arguments to a command

From Dev

Splitting one line of a Csv into multiple lines in PowerShell

From Dev

JS - multiple eventlisteners and dynamic arguments in one line of code

From Dev

How to pipe multiple results into a command?

From Dev

Multiple arguments for powershell command

From Dev

Multiple command line arguments

From Dev

How do I run multiple commands on one line in PowerShell?

From Dev

traceback shows only one line of a multiline command

From Dev

Multiline command : comment out one line

From Dev

Multiline TextView and Singleline TextView in one line

From Dev

angular2 pipe for multiple arguments

From Dev

Pipe `%>%` the results of a regression `lm`and return one of the attributes

From Dev

Multiple Command Line Arguments in Python

From Dev

pipe ls output to one line perl script

From Dev

Pipe input to another process one line at a time

From Dev

grep pipe searching for one word, not line

From Dev

pipe ls output to one line perl script

From Dev

list grep results in one line?

From Dev

batch file - results in one line

From Dev

list grep results in one line?

From Dev

In Powershell how can I pass an array as an argument, which is one of multiple arguments to Start-Process -ArgumentList

From Dev

pipe as arguments

From Dev

Multiple assignment in one line

From Dev

Multiple cases in one line

From Dev

Multiple if statements on one line

From Dev

Multiple answers on one line

From Dev

Multiple answers on one line