ImageMagick命令在PowerShell窗口中无法运行,但可以在cmd窗口中运行。

huangapple go评论82阅读模式
英文:

ImageMagick command not running in Powershell window but can run in cmd window

问题

这个命令是用来给图片添加水印的。

magick nature.jpg -set option:watermarkWidth "%[fx:int(w*0.25)]" -alpha set -background none ( -fill "#FFFFFF80" -stroke "#FF000080" -strokeWidth 3 -undercolor "#FF000080" -size "%[watermarkWidth]x" label:"THIS IS WATERMARK" -gravity center -geometry +10+10 -rotate -30 ) -composite -quality 40 nature_wm.jpg

它可以在Windows的cmd窗口中运行。

但是在Powershell窗口中无法运行(见错误截图)。

我还尝试在()之前添加\(这在macOS或Linux上应该这样做)。

magick nature.jpg -set option:watermarkWidth "%[fx:int(w*0.25)]" -alpha set -background none \( -fill "#FFFFFF80" -stroke "#FF000080" -strokeWidth 3 -undercolor "#FF000080" -size "%[watermarkWidth]x" label:"THIS IS WATERMARK" -gravity center -geometry +10+10 -rotate -30 \) -composite -quality 40 nature_wm.jpg

但是它抛出了相同的错误。

> 错误翻译:-fill: -fill 无法被识别为 cmdlet、函数、脚本文件或可运行的程序。请检查名称的拼写,如果包括路径,请确保路径正确,然后重试。

我确定magick指令在环境变量中。

看起来Powershell可以运行简单的命令,但不能运行复杂的命令(下面的命令可以运行,没有错误)。

magick nature.jpg -fill yellow nature.png

有谁知道如何解决这个问题吗?

实际上,我需要用golang运行它,使用cmd = exec.Command("cmd", "/k", cmdStr),但如果cmdStr不能在Powershell中运行,它就无法工作,因为我用golang构建的可执行文件也需要在Powershell中运行(我不想在cmd窗口中运行,因为与Powershell相比,它太原始了)。

英文:

This command is to add a watermark to an image

magick nature.jpg -set option:watermarkWidth "%[fx:int(w*0.25)]" -alpha set -background none ( -fill "#FFFFFF80" -stroke "#FF000080" -strokeWidth 3 -undercolor "#FF000080" -size "%[watermarkWidth]x" label:"THIS IS WATERMARK" -gravity center -geometry +10+10 -rotate -30 ) -composite -quality 40 nature_wm.jpg

it can be run on Windows cmd window
ImageMagick命令在PowerShell窗口中无法运行,但可以在cmd窗口中运行。

But it cannot be run in Powershell window(see the error screenshot)
ImageMagick命令在PowerShell窗口中无法运行,但可以在cmd窗口中运行。

I also tried to add \ before ( and ) (which should do on macOS or Linux)

magick nature.jpg -set option:watermarkWidth "%[fx:int(w*0.25)]" -alpha set -background none \( -fill "#FFFFFF80" -stroke "#FF000080" -strokeWidth 3 -undercolor "#FF000080" -size "%[watermarkWidth]x" label:"THIS IS WATERMARK" -gravity center -geometry +10+10 -rotate -30 \) -composite -quality 40 nature_wm.jpg

But it throws the same error
ImageMagick命令在PowerShell窗口中无法运行,但可以在cmd窗口中运行。
> error translation: -fill: -fill can not be recognized as cmdlet, function, script file or runnable program. Please check the spelling of the name, and if you include a path, make sure the path is correct, then try again.

I'm sure that the magick directive is in the environment variable
ImageMagick命令在PowerShell窗口中无法运行,但可以在cmd窗口中运行。

It seems that Powershell can run simple command but not complex command(the following command can be run, no error)

magick nature.jpg -fill yellow nature.png

ImageMagick命令在PowerShell窗口中无法运行,但可以在cmd窗口中运行。

Anyone who knows how to solve this problem?

Actually I need to run it with golang, by using cmd = exec.Command("cmd", "/k", cmdStr), but it doesn't work if the "cmdStr" cannot be run in Powershell, because the executable the I build with golang need to run on Powershell too(I don't want to run on cmd window, because it's so primitive compared to Powershell).

答案1

得分: 3

TL;DR

本文介绍了在不同环境下使用ImageMagick命令时需要注意的引号、括号和转义字符的问题。同时提供了在bash、CMD32和Powershell中编写ImageMagick脚本的示例。

在bash中,需要注意以下问题:

  • #字符在ImageMagick中表示十六进制颜色,在bash中表示注释,需要使用单引号或双引号进行转义。
  • ()括号在ImageMagick中用于对特定图像应用处理,在bash中用于子进程,需要使用反斜杠进行转义。
  • %字符在ImageMagick中表示百分比大小,但在Windows BATCH文件中需要使用两个百分号进行转义。
  • <>字符在ImageMagick中用于指定大小范围,但在bash中用于输入和输出重定向,需要使用单引号或双引号进行转义。

在Windows CMD32中,需要注意以下问题:

  • ^字符用作转义字符和换行符。
  • 单引号在CMD32中不被支持,通常需要将单引号替换为双引号。
  • 百分号在BATCH文件中需要使用两个百分号进行转义。
  • 括号不需要进行转义。

在Powershell中,需要注意以下问题:

  • 反引号用作转义字符和换行符。
  • 括号需要使用反引号进行转义。

如果想要编写跨平台的ImageMagick脚本,可以将命令保存在一个文件中,然后通过magick -script script.mgk来执行。这样可以避免引号、百分号、括号等字符在不同环境下的问题。

请注意,以上内容是对原文的翻译,可能存在理解上的出入。

英文:

TL;DR

There's a very simple, quoting-independent, cross-platform way of writing ImageMagick scripts given at the end of this post...


On quotes and quoting and escapes and escaping ImageMagick commands in bash, CMD32 and Powershell

As a result of its sheer versatility and power, ImageMagick offers a far richer palette of options, switches and parameters than most command-line programs (around 300 options and switches are listed here) and it uses characters and symbols to allow users to express things in a very natural way. As a result, some degree of caution is required when using ImageMagick in the many environments in which it can run, namely:

  • under bash or other Unix/Linux shells such as zsh, ksh, tcsh,
  • under Windows CMD32 and in Windows BATCH files,
  • under Powershell
  • under things like MinGW, MSYS, Cygwin

For example, in and of itself, disregarding the shell, ImageMagick understands the following:

  • # or hash, a.k.a. pound sign is used to express hexadecimal colours, in a natural way, like -fill #ff0000 for a red fill colour. However, bash interprets that same character as introducing a comment, so in bash you would normally write -fill &#39;#ff0000&#39;

  • () or parentheses. ImageMagick uses parentheses to apply processing to a specific image in its stack, also called "aside processing". So, this command loads two images and resizes both magick IMAGE1.PNG IMAGE2.PNG -resize 800x600 ... but if you want to resize only the second, you would do magick IMAGE1.PNG ( IMAGE2.PNG -resize 800x600 ) ... However, bash uses parentheses for sub-processes, so it will think you want to run a sub-process called IMAGE2.PNG unless you escape the parentheses with magick IMAGE1.PNG \( IMAGE2.PNG -resize 800x600 \) .... Likewise, Powershell will object to the parentheses, and you must put a backtick before opening and closing parentheses. CMD32 doesn't treat parentheses as special at all, so they need no escaping in that environment.

  • () or parentheses. ImageMagick uses these to introduce hex or hsl colours, e.g. -fill RGBA(255,0,0) or -fill hsl(50,60,70). Again, bash will dislike that, thinking you want a sub-shell, so folks write -fill &quot;rgb(255,0,0)&quot;. I assume Powershell will not like it either.

  • % or percent. ImageMagick commonly uses that, and similar, to resize an image to 50% of its original size -resize 50%. But, if you use percent signs in a Windows BATCH file, you need to double them up else it thinks you are referring to its command-line parameters.

  • &lt; and &gt;, or less than and greater than. ImageMagick uses &gt; to mean you only want a resize applied to images greater than a certain size, e.g. -resize 800&gt; which means you only want images larger than 800 scaled down to 800, but you don't want images under 800 scaled up. Likewise with &lt;. However, bash uses those characters for redirection of input and output, so in bash you would normally write -resize &#39;800&gt;&#39; Likewise in CMD32, you will need to escape both &lt; and &gt; by preceding with a caret ^. And precede with backticks in Powershell.

  • ! or exclamation mark, a.k.a. "bang". ImageMagick uses that to mean "Just do it!". So, for example, -resize 800x600 says you want to resize such that the width doesn't exceed 800 and the height doesn't exceed 600 and the aspect ratio should be respected. However, when you add the bang like this -resize 800x600! you will get exactly 800x600 pixels even if that distorts your image horribly. The shell can interpret the exclamation as introducing some manipulation of its history of previous commands, so you will often see that escaped

  • [ and ], or square brackets. ImageMagick uses these to refer to a page or subset of pages in a multipage document such as a PDF or a TIFF. For example, the following means the first and last page of a PDF, magick DOCUMENT.PDF[0,-1] ... That can get confused with shell syntax for an alternation if you don't handle it with care

  • * or asterisk. ImageMagick understands the asterisk as a globbing character to expand the list of all matching files, e.g. *.tif meaning all the TIFF files in the current directory. That is the same as bash, but there are nuances here. If you use magick *.tif ..., the list of TIFFs will be expanded by your shell in bash and that will be subject to your system's ARGMAX. But if you do magick &#39;*.TIF&#39; ... it will be expanded internally by ImageMagick and not be subject to such limits.


General hints for bash and Unix/Linux shells

  • The line continuation character is the backslash

  • Opening and closing parentheses must be escaped with backslashes immediately in front of them

  • Hashes must be within double or single quoted strings

Example of bash command

magick IMAGE1.PNG \
   \( IMAGE2.PNG -resize 50% -fill &#39;#ff0000&#39; -colorize 100% \) \
  -composite -transparent &#39;hsl(40,50,60)&#39; result.png

General hints for Windows CMD32

  • the caret ^ is used as the escape character

  • CMD32 generally dislikes any usage of single quotes.
    Generally, if translating from a bash incantation of ImageMagick, try replacing single quotes with double quotes. The exception to this is when already inside double quotes, where you can use single quotes, e.g. -draw &quot;text 100,100 &#39;Works like magick&#39;&quot;

  • the caret is used as the line continuation character and may not be followed by any spaces when used that way

  • percent signs must be doubled up in BATCH

  • parentheses do not require escaping

Example of Windows CMD32 BATCH command

magick IMAGE1.PNG ^
   ( IMAGE2.PNG -resize 50%% -fill &quot;#ff0000&quot; -colorize 100% ) ^
  -composite -transparent &quot;hsl(40,50,60)&quot; result.png

General hints for Powershell

  • the backtick is used as the escape character, and the line continuation character
  • opening and closing parentheses must be escaped with a preceding backtick

Example of Powershell command

magick IMAGE1.PNG `
   `( IMAGE2.PNG -resize 50% -fill &quot;#ff0000&quot; -colorize 100% `) `
  -composite -transparent &quot;hsl(40,50,60)&quot; result.png

TL;DR

If you want a cross-platform, or platform independent way of writing ImageMagick scripts, the easiest is to put all the commands in a file whose contents are only read by ImageMagick itself rather than being dependent on your shell. So, write a script like this, which is pure and has no quoting, and save it as script.mgk:

-size 640x480 xc:#ffff00
( foreground.png -resize 50% )
-gravity center -composite -write result.png

Then invoke it with:

magick -script script.mgk

and your shell, whatever that may be, doesn't even see the quotes, percent signs, hash signs, line-continuations or parentheses:

Compare and contrast with bash where you would need:

magick -size 640x480 xc:&#39;#ffff00&#39;    \
   \( foreground.png -resize 50% \)  \
   -gravity centre -composite result.png

and Windows CMD32.EXE where you would need:

magick -size 640x480 xc:#ffff00     ^
   ( foreground.png -resize 50%% )  ^
   -gravity centre -composite result.png    

答案2

得分: 3

在PowerShell中测试EXE文件时,EchoArgs.exe是一个非常有用的工具。这个工具的原始来源似乎已经消失了,但你仍然可以从ss64.com的这个页面上下载它。

在处理EXE文件的复杂命令行时,"Stop-parsing token (--%)"是一个非常有用的特殊解析标记。

使用以下命令行执行EchoArgs和参数:

EchoArgs nature.jpg -set option:watermarkWidth &quot;%[fx:int(w*0.25)]&quot; -alpha set -background none ( -fill &quot;#FFFFFF80&quot; -stroke &quot;#FF000080&quot; -strokeWidth 3 -undercolor &quot;#FF000080&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;THIS IS WATERMARK&quot; -gravity center -geometry +10+10 -rotate -30 ) -composite -quality 40 nature_wm.jpg

记录你的结果:

Arg 0 is &lt;nature.jpg&gt;
Arg 1 is &lt;-set&gt;
Arg 2 is &lt;option:watermarkWidth&gt;
Arg 3 is &lt;%[fx:int(w*0.25)]&gt;
Arg 4 is &lt;-alpha&gt;
Arg 5 is &lt;set&gt;
Arg 6 is &lt;-background&gt;
Arg 7 is &lt;none&gt;
Arg 8 is &lt;(&gt;
Arg 9 is &lt;-fill&gt;
Arg 10 is &lt;#FFFFFF80&gt;
Arg 11 is &lt;-stroke&gt;
Arg 12 is &lt;#FF000080&gt;
Arg 13 is &lt;-strokeWidth&gt;
Arg 14 is &lt;3&gt;
Arg 15 is &lt;-undercolor&gt;
Arg 16 is &lt;#FF000080&gt;
Arg 17 is &lt;-size&gt;
Arg 18 is &lt;%[watermarkWidth]x&gt;
Arg 19 is &lt;label:THIS IS WATERMARK&gt;
Arg 20 is &lt;-gravity&gt;
Arg 21 is &lt;center&gt;
Arg 22 is &lt;-geometry&gt;
Arg 23 is &lt;+10+10&gt;
Arg 24 is &lt;-rotate&gt;
Arg 25 is &lt;-30&gt;
Arg 26 is &lt;)&gt;
Arg 27 is &lt;-composite&gt;
Arg 28 is &lt;-quality&gt;
Arg 29 is &lt;40&gt;
Arg 30 is &lt;nature_wm.jpg&gt;

为了避免问题,在命令行变得复杂的地方,在PowerShell中使用--%:

EchoArgs nature.jpg -set option:watermarkWidth --% &quot;%[fx:int(w*0.25)]&quot; -alpha set -background none ( -fill &quot;#FFFFFF80&quot; -stroke &quot;#FF000080&quot; -strokeWidth 3 -undercolor &quot;#FF000080&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;THIS IS WATERMARK&quot; -gravity center -geometry +10+10 -rotate -30 ) -composite -quality 40 nature_wm.jpg

检查你的结果:

Arg 0 is &lt;nature.jpg&gt;
Arg 1 is &lt;-set&gt;
Arg 2 is &lt;option:watermarkWidth&gt;
Arg 3 is &lt;%[fx:int(w*0.25)]&gt;
Arg 4 is &lt;-alpha&gt;
Arg 5 is &lt;set&gt;
Arg 6 is &lt;-background&gt;
Arg 7 is &lt;none&gt;
Arg 8 is &lt;(&gt;
Arg 9 is &lt;-fill&gt;
Arg 10 is &lt;#FFFFFF80&gt;
Arg 11 is &lt;-stroke&gt;
Arg 12 is &lt;#FF000080&gt;
Arg 13 is &lt;-strokeWidth&gt;
Arg 14 is &lt;3&gt;
Arg 15 is &lt;-undercolor&gt;
Arg 16 is &lt;#FF000080&gt;
Arg 17 is &lt;-size&gt;
Arg 18 is &lt;%[watermarkWidth]x&gt;
Arg 19 is &lt;label:THIS IS WATERMARK&gt;
Arg 20 is &lt;-gravity&gt;
Arg 21 is &lt;center&gt;
Arg 22 is &lt;-geometry&gt;
Arg 23 is &lt;+10+10&gt;
Arg 24 is &lt;-rotate&gt;
Arg 25 is &lt;-30&gt;
Arg 26 is &lt;)&gt;
Arg 27 is &lt;-composite&gt;
Arg 28 is &lt;-quality&gt;
Arg 29 is &lt;40&gt;
Arg 30 is &lt;nature_wm.jpg&gt;

但是,如果你需要在"Stop-parsing token (--%)"之后动态替换部分参数怎么办?这可以通过环境变量来实现:

$Env:FirstValue = '&#39;%[fx:int(w*0.25)]&#39;'
$Env:Alpha = '&#39;set&#39;'
$Env:Background = '&#39;none&#39;'
$Env:Fill = '&#39;#FFFFFF80&#39;'
$Env:Stroke = '&#39;#FF000080&#39;'
$Env:StrokeWidth = '&#39;3&#39;'
$Env:Undercolor = '&#39;#FF000080&#39;'
$Env:Size =  '&#39;%[watermarkWidth]x&#39;'
$Env:Label = '&#39;THIS IS WATERMARK&#39;'
$Env:Gravity = '&#39;center&#39;'
$Env:Geometry = '&#39;+10+10&#39;'
$Env:Rotate = '&#39;-30&#39;'
$Env:Quality = '&#39;40&#39;'
$Env:ImgName = '&#39;nature_wm.jpg&#39;'
EchoArgs nature.jpg -set option:watermarkWidth --% &quot;%FirstValue%&quot; -alpha %Alpha% -background %Background% ( -fill &quot;%Fill%&quot; -stroke &quot;%Stroke%&quot; -strokeWidth %StrokeWidth% -undercolor &quot;%Undercolor%&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;%Label%&quot; -gravity %Gravity% -geometry %Geometry% -rotate %Rotate% ) -composite -quality %Quality% %ImgName%

再次检查你的结果:

Arg 0 is &lt;nature.jpg&gt;
Arg 1 is &lt;-set&gt;
Arg 2 is &lt;option:watermarkWidth&gt;   
Arg 3 is &lt;%[fx:int(w*0.25)]&gt;       
Arg 4 is &lt;-alpha&gt;
Arg 5 is &lt;set&gt;
Arg 6 is &lt;-background&gt;
Arg 7 is &lt;none&gt;
Arg 8 is &lt;(&gt;
Arg 9 is &lt;-fill&gt;
Arg 10 is &lt;#FFFFFF80&gt;
Arg 11 is &lt;-stroke&gt;
Arg 12 is &lt;#FF000080&gt;
Arg 13 is &lt;-strokeWidth&gt;
Arg 14 is &lt;3&gt;
Arg 15 is &lt;-undercolor&gt;
Arg 16 is &lt;#FF000080&gt;
Arg 17 is &lt;-size&gt;
Arg 18 is &lt;%[watermarkWidth]x&gt;     
Arg 19 is &lt;label:THIS IS WATERMARK&gt;
Arg 20 is &lt;-gravity&gt;
Arg 21 is &lt;center&gt;
Arg 22 is &lt;-geometry&gt;
Arg 23 is &lt;+10+10&gt;
Arg 24 is &lt;-rotate&gt;
Arg 25 is &lt;-30&gt;
Arg 26 is &lt;)&gt;
Arg 27 is &lt;-composite&gt;
Arg 28 is &lt;-quality&gt;
Arg 29 is &lt;40&gt;
Arg 30 is &lt;nature_wm.jpg&gt;

但是,如果你不知道EXE文件在哪里,并且必须在硬盘上搜索并从变量中调用它,可以使用"call operator (&)":

$Program = 'C:\Program Files\ImageMagick-7.0.11-Q16-HDRI\magick.exe'
$Env:FirstValue = '&#39;%[fx:int(w*0.25)]&#39;'
$Env:Alpha = '&#39;set&#39;'
$Env:Background = '&#39;none&#39;'
$Env:Fill = '&#39;#FFFFFF80&#39;'
$Env:Stroke = '&#39;#FF000080&#39;'
$Env:StrokeWidth = '&#39;3&#39;'
$Env:Undercolor = '&#39;#FF000080&#39;'
$Env:Size =  '&#39;%[watermarkWidth]x&#39;'
$Env:Label = '&#39;THIS IS WATERMARK&#39;'
$Env:Gravity = '&#39;center&#39;'
$Env:Geometry = '&#39;+10+10&#39;'
$Env:Rotate = '&#39;-30&#39;'
$Env:Quality = '&#39;40&#39;'
$Env:ImgName = '&#39;nature_wm.jpg&#39;'
&amp; $Program nature.jpg -set option:watermarkWidth --% &quot;%FirstValue%&quot; -alpha %Alpha% -background %Background% ( -fill &quot;%Fill%&quot; -stroke &quot;%Stroke%&quot; -strokeWidth %StrokeWidth% -undercolor &quot;%Undercolor%&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;%Label%&quot; -gravity %Gravity% -geometry %Geometry% -rotate %Rotate% ) -composite -quality %Quality% %ImgName%

现在,说了这么多,真正的测试是实际执行命令并查看它是否有效。我没有你的图像文件,也不想进行任何关于Image Magick的实验。但是,理论上,这个命令应该适用于你:

magick nature.jpg -set option:watermarkWidth --% &quot;%[fx:int(w*0.25)]&quot; -alpha set -background none ( -fill &quot;#FFFFFF80&quot; -stroke &quot;#FF000080&quot; -strokeWidth 3 -undercolor &quot;#FF000080&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;THIS IS WATERMARK&quot; -gravity center -geometry +10+10 -rotate -30 ) -composite -quality 40 nature_wm.jpg

编辑:

如果你想在GoLang中运行MagicK,请使用以下代码:

package main

import (
	"fmt"
	"os/exec"
)


func main() {
	data, err := exec.Command("magick", "nature.jpg", "-set", "option:watermarkWidth", "%[fx:int(w*0.25)]", "-alpha", "set", "-background", "none", "(", "-fill", "#FFFFFF80", "-stroke", "#FF000080", "-strokeWidth", "3", "-undercolor", "#FF000080", "-size", "%[watermarkWidth]x", "label:THIS IS WATERMARK", "-gravity", "center", "-geometry", "+10+10", "-rotate", "-30", ")", "-composite", "-quality", "40", "nature_wm.jpg").Output()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(data))
}
英文:

When testing EXE files in PowerShell, EchoArgs.exe is a very useful tool to have on hand. The original source for this tool appears to have disappeared, but you can still download it from ss64.com on this page.

When dealing with complex command lines for EXE files, Stop-parsing token (--%) is a very useful special parsing token to keep in mind.

In the command line execute EchoArgs with your parameters:

EchoArgs nature.jpg -set option:watermarkWidth &quot;%[fx:int(w*0.25)]&quot; -alpha set -background none ( -fill &quot;#FFFFFF80&quot; -stroke &quot;#FF000080&quot; -strokeWidth 3 -undercolor &quot;#FF000080&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;THIS IS WATERMARK&quot; -gravity center -geometry +10+10 -rotate -30 ) -composite -quality 40 nature_wm.jpg

Record your results:

Arg 0 is &lt;nature.jpg&gt;
Arg 1 is &lt;-set&gt;
Arg 2 is &lt;option:watermarkWidth&gt;
Arg 3 is &lt;%[fx:int(w*0.25)]&gt;
Arg 4 is &lt;-alpha&gt;
Arg 5 is &lt;set&gt;
Arg 6 is &lt;-background&gt;
Arg 7 is &lt;none&gt;
Arg 8 is &lt;(&gt;
Arg 9 is &lt;-fill&gt;
Arg 10 is &lt;#FFFFFF80&gt;
Arg 11 is &lt;-stroke&gt;
Arg 12 is &lt;#FF000080&gt;
Arg 13 is &lt;-strokeWidth&gt;
Arg 14 is &lt;3&gt;
Arg 15 is &lt;-undercolor&gt;
Arg 16 is &lt;#FF000080&gt;
Arg 17 is &lt;-size&gt;
Arg 18 is &lt;%[watermarkWidth]x&gt;
Arg 19 is &lt;label:THIS IS WATERMARK&gt;
Arg 20 is &lt;-gravity&gt;
Arg 21 is &lt;center&gt;
Arg 22 is &lt;-geometry&gt;
Arg 23 is &lt;+10+10&gt;
Arg 24 is &lt;-rotate&gt;
Arg 25 is &lt;-30&gt;
Arg 26 is &lt;)&gt;
Arg 27 is &lt;-composite&gt;
Arg 28 is &lt;-quality&gt;
Arg 29 is &lt;40&gt;
Arg 30 is &lt;nature_wm.jpg&gt;

To avoid problem, use --% in PowerShell at the point where the command line becomes complex:

EchoArgs nature.jpg -set option:watermarkWidth --% &quot;%[fx:int(w*0.25)]&quot; -alpha set -background none ( -fill &quot;#FFFFFF80&quot; -stroke &quot;#FF000080&quot; -strokeWidth 3 -undercolor &quot;#FF000080&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;THIS IS WATERMARK&quot; -gravity center -geometry +10+10 -rotate -30 ) -composite -quality 40 nature_wm.jpg

Check your results:

Arg 0 is &lt;nature.jpg&gt;
Arg 1 is &lt;-set&gt;
Arg 2 is &lt;option:watermarkWidth&gt;
Arg 3 is &lt;%[fx:int(w*0.25)]&gt;
Arg 4 is &lt;-alpha&gt;
Arg 5 is &lt;set&gt;
Arg 6 is &lt;-background&gt;
Arg 7 is &lt;none&gt;
Arg 8 is &lt;(&gt;
Arg 9 is &lt;-fill&gt;
Arg 10 is &lt;#FFFFFF80&gt;
Arg 11 is &lt;-stroke&gt;
Arg 12 is &lt;#FF000080&gt;
Arg 13 is &lt;-strokeWidth&gt;
Arg 14 is &lt;3&gt;
Arg 15 is &lt;-undercolor&gt;
Arg 16 is &lt;#FF000080&gt;
Arg 17 is &lt;-size&gt;
Arg 18 is &lt;%[watermarkWidth]x&gt;
Arg 19 is &lt;label:THIS IS WATERMARK&gt;
Arg 20 is &lt;-gravity&gt;
Arg 21 is &lt;center&gt;
Arg 22 is &lt;-geometry&gt;
Arg 23 is &lt;+10+10&gt;
Arg 24 is &lt;-rotate&gt;
Arg 25 is &lt;-30&gt;
Arg 26 is &lt;)&gt;
Arg 27 is &lt;-composite&gt;
Arg 28 is &lt;-quality&gt;
Arg 29 is &lt;40&gt;
Arg 30 is &lt;nature_wm.jpg&gt;

But what if you need to dynamically replace part of the parameters after the the Stop-parsing token (--%)? That is doable via environmental variables:

$Env:FirstValue = &#39;%[fx:int(w*0.25)]&#39;
$Env:Alpha = &#39;set&#39;
$Env:Background = &#39;none&#39;
$Env:Fill = &#39;#FFFFFF80&#39;
$Env:Stroke = &#39;#FF000080&#39;
$Env:StrokeWidth = &#39;3&#39;
$Env:Undercolor = &#39;#FF000080&#39;
$Env:Size =  &#39;%[watermarkWidth]x&#39;
$Env:Label = &#39;THIS IS WATERMARK&#39;
$Env:Gravity = &#39;center&#39;
$Env:Geometry = &#39;+10+10&#39;
$Env:Rotate = &#39;-30&#39;
$Env:Quality = &#39;40&#39;
$Env:ImgName = &#39;nature_wm.jpg&#39;
EchoArgs nature.jpg -set option:watermarkWidth --% &quot;%FirstValue%&quot; -alpha %Alpha% -background %Background% ( -fill &quot;%Fill%&quot; -stroke &quot;%Stroke%&quot; -strokeWidth %StrokeWidth% -undercolor &quot;%Undercolor%&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;%Label%&quot; -gravity %Gravity% -geometry %Geometry% -rotate %Rotate% ) -composite -quality %Quality% %ImgName%

Again, check your results:

Arg 0 is &lt;nature.jpg&gt;
Arg 1 is &lt;-set&gt;
Arg 2 is &lt;option:watermarkWidth&gt;   
Arg 3 is &lt;%[fx:int(w*0.25)]&gt;       
Arg 4 is &lt;-alpha&gt;
Arg 5 is &lt;set&gt;
Arg 6 is &lt;-background&gt;
Arg 7 is &lt;none&gt;
Arg 8 is &lt;(&gt;
Arg 9 is &lt;-fill&gt;
Arg 10 is &lt;#FFFFFF80&gt;
Arg 11 is &lt;-stroke&gt;
Arg 12 is &lt;#FF000080&gt;
Arg 13 is &lt;-strokeWidth&gt;
Arg 14 is &lt;3&gt;
Arg 15 is &lt;-undercolor&gt;
Arg 16 is &lt;#FF000080&gt;
Arg 17 is &lt;-size&gt;
Arg 18 is &lt;%[watermarkWidth]x&gt;     
Arg 19 is &lt;label:THIS IS WATERMARK&gt;
Arg 20 is &lt;-gravity&gt;
Arg 21 is &lt;center&gt;
Arg 22 is &lt;-geometry&gt;
Arg 23 is &lt;+10+10&gt;
Arg 24 is &lt;-rotate&gt;
Arg 25 is &lt;-30&gt;
Arg 26 is &lt;)&gt;
Arg 27 is &lt;-composite&gt;
Arg 28 is &lt;-quality&gt;
Arg 29 is &lt;40&gt;
Arg 30 is &lt;nature_wm.jpg&gt;

But what if you don't know where the EXE is and have to search for it on the hard drive and call it from a variable? Use The call operator (&):

$Program = &#39;C:\Program Files\ImageMagick-7.0.11-Q16-HDRI\magick.exe&#39;
$Env:FirstValue = &#39;%[fx:int(w*0.25)]&#39;
$Env:Alpha = &#39;set&#39;
$Env:Background = &#39;none&#39;
$Env:Fill = &#39;#FFFFFF80&#39;
$Env:Stroke = &#39;#FF000080&#39;
$Env:StrokeWidth = &#39;3&#39;
$Env:Undercolor = &#39;#FF000080&#39;
$Env:Size =  &#39;%[watermarkWidth]x&#39;
$Env:Label = &#39;THIS IS WATERMARK&#39;
$Env:Gravity = &#39;center&#39;
$Env:Geometry = &#39;+10+10&#39;
$Env:Rotate = &#39;-30&#39;
$Env:Quality = &#39;40&#39;
$Env:ImgName = &#39;nature_wm.jpg&#39;
&amp; $Program nature.jpg -set option:watermarkWidth --% &quot;%FirstValue%&quot; -alpha %Alpha% -background %Background% ( -fill &quot;%Fill%&quot; -stroke &quot;%Stroke%&quot; -strokeWidth %StrokeWidth% -undercolor &quot;%Undercolor%&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;%Label%&quot; -gravity %Gravity% -geometry %Geometry% -rotate %Rotate% ) -composite -quality %Quality% %ImgName%

Now, having said all that, the real test is to actually execute the command and see if it works. I don't have your image file and not really up to doing any experiments with Image Magick. But, in theory, this command should work work for you:

magick nature.jpg -set option:watermarkWidth --% &quot;%[fx:int(w*0.25)]&quot; -alpha set -background none ( -fill &quot;#FFFFFF80&quot; -stroke &quot;#FF000080&quot; -strokeWidth 3 -undercolor &quot;#FF000080&quot; -size &quot;%[watermarkWidth]x&quot; label:&quot;THIS IS WATERMARK&quot; -gravity center -geometry +10+10 -rotate -30 ) -composite -quality 40 nature_wm.jpg

EDIT:

If you ware wanting MagicK to run in GoLang, use the following code:

package main

import (
	&quot;fmt&quot;
	&quot;os/exec&quot;
)


func main() {
	data, err := exec.Command(&quot;magick&quot;, &quot;nature.jpg&quot;, &quot;-set&quot;, &quot;option:watermarkWidth&quot;, &quot;%[fx:int(w*0.25)]&quot;, &quot;-alpha&quot;, &quot;set&quot;, &quot;-background&quot;, &quot;none&quot;, &quot;(&quot;, &quot;-fill&quot;, &quot;#FFFFFF80&quot;, &quot;-stroke&quot;, &quot;#FF000080&quot;, &quot;-strokeWidth&quot;, &quot;3&quot;, &quot;-undercolor&quot;, &quot;#FF000080&quot;, &quot;-size&quot;, &quot;%[watermarkWidth]x&quot;, &quot;label:THIS IS WATERMARK&quot;, &quot;-gravity&quot;, &quot;center&quot;, &quot;-geometry&quot;, &quot;+10+10&quot;, &quot;-rotate&quot;, &quot;-30&quot;, &quot;)&quot;, &quot;-composite&quot;, &quot;-quality&quot;, &quot;40&quot;, &quot;nature_wm.jpg&quot;).Output()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(data))
}

huangapple
  • 本文由 发表于 2022年4月1日 20:30:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/71706932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定