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

发布于 2025-01-18 06:20:15 字数 2164 浏览 5 评论 0原文

该命令是为图像添加水印,

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 窗口中运行 输入图片description here

但无法在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
enter image description here

But it cannot be run in Powershell window(see the error screenshot)
enter image description here

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
enter image description here

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
enter image description here

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

enter image description here

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开浅夏 2025-01-25 06:20:15

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-行程(列出了大约300个选项和交换机在这里),并使用字符符号允许用户以非常自然的方式表达东西。 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 < code>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, aka pound sign is used to express hexadecimal colours,自然的方式,例如-fill#ff0000红色填充颜色。但是,bash解释与引入注释相同的字符,因此在bash中,您通常会写-fill'#ff0000'


  • <代码>()或括号。 ImageMagick 使用括号将处理应用于其堆栈中的特定图像,也称为“旁边处理” 。因此,此命令将加载两个图像,并调整两个magick image1.png image2.png -Resize 800x600 ...,但是如果您只想调整第二个大小,则您会执行magick image1.png (image2.png-Resize 800x600)...,但是,bash使用括号对子过程使用,因此它会认为您要运行一个称为image2的子过程。 png,除非您用magick image1.png \(image2.png -Resize 800x600 \)逃脱括号。。同样,powershell将反对括号,您必须在打开和关闭括号之前放置一个回头。 CMD32根本不将括号视为特殊,因此它们在那种环境中无需逃脱。

  • ()或括号。 ImageMagick 使用它们引入HEX或HSL颜色,例如-fill RGBA(255,0,0)-fill HSL(50,60,70)< /代码>。同样,bash会认为您想要一个子壳,因此人们写-fill“ RGB(255,0,0)”。我假设PowerShell也不喜欢它。


  • 或百分比。 ImageMagick 通常使用它,并且类似地将图像大小调整为其原始大小的50%- 提高50%。但是,如果您在Windows批处理文件中使用百分比符号,则需要将它们加倍,其他认为您是指它的命令行参数。

  • &lt;&gt;,或小于和大于。 ImageMagick 使用&gt;意味着您只想将调整大小应用到大于一定尺寸的图像中,例如resige> -Resize 800&gt;,这意味着您仅表示您仅想要大于800的图像缩小到800,但您不希望将图像缩放到800以下。同样,使用&lt;。但是,bash使用这些字符重定向输入和输出,因此在bash中,您通常会在cmd32中同样编写-Resize'800&gt;' ,您将需要同时逃脱&lt;&gt;,然后再用caret ^。并在powershell中的背景前提。

  • !!或crimamation标记,aka “ bang” ImageMagick 用它来表示“只是做!” 。因此,例如,resize 800x600表示要调整大小,以使宽度不超过800,并且高度不超过600,并且应尊重纵横比。但是,当您添加resize 800x600!时,即使这使您的图像造成了可怕的扭曲,也会得到800x600像素。外壳可以将感叹词解释为引入对其先前命令历史的某些操纵,因此您经常会看到逃脱的

  • [ and ], or square brackets. ImageMagick 使用这些来指代乘法文档(例如pdf或tiff)中的页面或子集。例如,以下是指pdf的第一页和最后一页,magick document.pdf [0,-1] ...,如果您不这样做,则可以与shell语法混淆。谨慎处理

  • * or asterisk. ImageMagick 理解星球字符,以扩展所有匹配文件的列表,例如*。tif,含义当前目录中的所有TIFF文件。这与bash相同,但是这里有细微差别。如果您使用magick *.tif ...,则您的shell列表将由您的shell在bash中扩展,并且将受您系统的argmax < /代码>。但是,如果您做magick'*.tif'...它将通过 ImageMagick 内部扩展,并且不受此类限制。



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 '#ff0000' -colorize 100% \) \
  -composite -transparent 'hsl(40,50,60)' result.png

General hints for Windows CMD32

  • the caret ^用作逃生字符


  • CMD32 generally dislikes any usage of single quotes.
    通常,如果从bash ImageMagick 的咒语翻译,请尝试用双引号替换单引号。例外是当已经在双引号中,您可以在其中使用单个引号,例如-draw“ text 100,100'的作品,例如magick'”

  • the caret is used as the line continuation character and使用这种方式时可能不会遵循任何空间

  • 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 "#ff0000" -colorize 100% ) ^
  -composite -transparent "hsl(40,50,60)" 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 "#ff0000" -colorize 100% `) `
  -composite -transparent "hsl(40,50,60)" result.png

TL;DR

If you want a cross-platform, or platform independent way of writing ImageMagick scripts, the最简单的是将所有命令都放在文件中,该文件仅通过 ImageMagick 本身读取内容,而不是依赖您的外壳。 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:'#ffff00'    \
   \( 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    

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 '#ff0000'

  • () 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 "rgb(255,0,0)". 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.

  • < and >, or less than and greater than. ImageMagick uses > to mean you only want a resize applied to images greater than a certain size, e.g. -resize 800> 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 <. However, bash uses those characters for redirection of input and output, so in bash you would normally write -resize '800>' Likewise in CMD32, you will need to escape both < and > 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 '*.TIF' ... 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 '#ff0000' -colorize 100% \) \
  -composite -transparent 'hsl(40,50,60)' 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 "text 100,100 'Works like magick'"

  • 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 "#ff0000" -colorize 100% ) ^
  -composite -transparent "hsl(40,50,60)" 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 "#ff0000" -colorize 100% `) `
  -composite -transparent "hsl(40,50,60)" 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:'#ffff00'    \
   \( 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    
热风软妹 2025-01-25 06:20:15

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

处理 EXE 文件的复杂命令行时,停止解析标记 (--%) 是一个非常有用的特殊解析标记,需要记住。

在命令行中使用您的参数执行 EchoArgs:

EchoArgs 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

记录您的结果:

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

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

EchoArgs 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

检查您的结果:

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

但是如果您需要动态替换部分参数怎么办在停止解析标记(--%)之后?这可以通过环境变量实现:

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

再次检查您的结果:

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

但是如果您不知道 EXE 在哪里并且必须在硬盘驱动器上搜索它并从变量调用它怎么办?使用 调用运算符 (&)

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

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

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

编辑:

如果您希望 MagicK 在 GoLang 中运行,请使用以下代码:

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 "%[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

Record your results:

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

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

EchoArgs 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

Check your results:

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

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

Again, check your results:

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

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

EDIT:

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

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))
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文