简体   繁体   English

ImageMagick命令行选项顺序(和命令行参数的类别)

[英]ImageMagick Command-Line Option Order (and Categories of Command-Line Parameters)

My supervisor has asked me to convert the parts of our Perl scripts that use PerlMagick to instead pipe and use the command line version of ImageMagick (for various unrelated reasons). 我的主管要求我将使用PerlMagick的Perl脚本部分转换为管道并使用ImageMagick的命令行版本(出于各种不相关的原因)。

Using the our existing interface (crop, scale, save, etc) I'm building up a list of operations the user wants to perform on an image, constructing the statement to pipe and then executing it. 使用我们现有的界面(裁剪,缩放,保存等),我正在建立一个用户想要对图像执行的操作列表,构建语句到管道然后执行它。

What I would like to know is: 我想知道的是:

  1. Are convert operations performed from left to right? 转换操作是从左到右执行的吗? ie the order I pass them 即我传递的顺序
  2. What happens if I pass the same option twice? 如果我两次通过相同的选项会怎样? Are they performed separately? 它们是分开进行的吗?

Obviously the order in which operations are performed on an image is vital, so I'm trying to work out if I can perform all of the operations in one go (possibly gaining efficiency?) or if it I'm going to have to perform each operation separately. 显然,对图像执行操作的顺序是至关重要的,所以如果我可以一次执行所有操作(可能获得效率?)或者如果我将不得不执行,我正在努力解决这个问题。每个操作分开。 Thanks 谢谢

Unfortunately, the accepted answer to this question is not yet complete... :-) 不幸的是,这个问题的接受答案还没有完成...... :-)

Three (major) classes of parameters 三个(主要)类参数

Assuming, your ImageMagick version is a recent one, here is an important amendment to it: 假设您的ImageMagick版本是最近版本,这是对它的一个重要修正:

  • you should differentiate between 3 major classes of command line parameters: 你应该区分3大类命令行参数:

    1. Image Settings 图像设置
    2. Image Operators 图像运算符
    3. Image Sequence Operators 图像序列运算符

These three classes do behave differently: 这三个类的行为有所不同:

  1. Image Settings 图像设置

    1. An image setting persists as it appears on the command line. 图像设置在命令行中显示时仍然存在。 It may affect all subsequent processing (but not previous processing): 它可能会影响所有后续处理(但不会影响以前的处理):

      • processing such as reading an image or more images later in the command line; 稍后在命令行中处理诸如读取图像或更多图像;
      • processing done by a following image operator; 由以下图像操作员完成的处理;
      • processing conducted by writing an image as output. 通过写入图像作为输出进行的处理。
    2. An image setting stays in effect... 图像设置保持有效...

      • ...either until it is reset or replaced by a different setting of the same type, ...要么重置,要么用相同类型的不同设置替换,
      • ...or until the command line terminates. ...或直到命令行终止。
  2. Image Operators 图像运算符

    1. An image operator is applied to a (single) image and forgotten. 图像操作符应用于(单个)图像并被遗忘。 It differs from an image setting because it affects the image immediately as it appears on the command line. 它与图像设置不同,因为它会立即影响图像,因为它出现在命令行上。 (Remember: an image setting which persists until the command line terminates, or until it is reset.) (请记住:图像设置一直持续到命令行终止,或直到它被重置。)

    2. If you need to apply the same image operator to a second image in the same commandline, you have to repeat that exact operator on the commandline. 如果需要将同一图像操作符应用于同一命令行中的第二个图像,则必须在命令行上重复该精确操作符。

    3. Strictly speaking, in compliance with the new architecture of ImageMagick command lines, all image operators should be written after the loading of the image it is meant for. 严格来说,根据ImageMagick命令行的新架构,所有图像操作符都应在加载图像编写。 However, the IM developers compromised: in the interest of backward compatibility, image operators can still appear before loading an image -- they will then be applied to the first image that is available to them. 然而,IM开发人员妥协:为了向后兼容,图像操作员仍然可以在加载图像之前出现 - 然后它们将应用于他们可用的第一个图像。

  3. Image Sequence Operators 图像序列运算符

    1. An image sequence operator is applied to all currently loaded images (and then forgotten). 图像序列运算符应用于所有当前加载的图像(然后被遗忘)。

    2. It differs from a simple image operator in that it does not only affect a single image. 它与简单的图像操作符的不同之处在于它不仅影响单个图像。 (Some operators only make sense if their operation has multiple images for consumption: think of -append , -combine , -composite , -morph ...) (一些运营商才有意义,如果他们的操作有多个图像进行消费:想-append-combine-composite-morph ...)

From above principles you can already conclude: the order of the command line parameters is significant in most cases. 从上述原则可以得出结论:在大多数情况下,命令行参数的顺序很重要。 (If you know what they do, you also know which order you need to use applying them.) (如果你知道他们做了什么,你也知道你需要使用哪个顺序来应用它们。)

(For completeness' sake I should add: there is another class of miscellanious , or other parameters, which do not fall into any of the above listed categories. Think -debug , -verbose or -version .) (为了完整起见,我应该补充:还有另一类杂项其他参数,它们不属于上面列出的任何类别。想想-debug-verbose-version 。)

Unfortunately, the clear distinction between the 3 classes of IM command line paramaters is not yet common knowledge among (otherwise quite savvy) IM users . 不幸的是,3类IM命令行参数之间的明显区别还不是(在其他非常精明的)IM用户中的常识 So it merits to get much more exposure. 因此,获得更多曝光是值得的。

This clear differentiation was introduced with ImageMagick major version 6. Before, it was more confusing: some settings' semantics changed with context and also with the order they were given. ImageMagick主要版本6引入了这种明显的区别。之前,它更令人困惑:某些设置的语义随着上下文而改变,也随着它们的顺序而改变。 Results from complex commands were not always predictable and sometimes surprising and illogical. 复杂命令的结果并不总是可预测的,有时令人惊讶和不合逻辑。 (Now they may be surprising too sometimes, but when you closely look at them, understanding the above, they are always quite logical.) (现在它们有时候可能会令人惊讶,但是当你仔细观察它们时,理解上面的内容,它们总是很合乎逻辑。)

Which is which ?!? 哪个是哪个?!?

Whenever you are not sure, which class one particular parameter belongs to, run 每当您不确定某个特定参数属于哪个类时,运行

 convert -help | less

Search for your parameter. 搜索您的参数。 Once found, scroll back: you should then find the "heading" under which it appears. 找到后,向后滚动:然后您应该找到它出现的“标题”。 Now you can be sure which type it is: an Image Setting , an Image Operator , or an Image Sequence Operator -- and take into account what I've said about them above. 现在您可以确定它是哪种类型: 图像设置图像操作员图像序列操作员 - 并考虑我上面对他们所说的内容。

Some more advice 一些更多的建议

If your job is to port your ImageMagick interface from PerlMagick to CLI, you should be aware of one other trick: You can insert 如果你的工作是将你的ImageMagick接口从PerlMagick移植到CLI,你应该知道另一个技巧:你可以插入

 +write output-destination

anywhere on the command line (even multiple times). 在命令行的任何地方(甚至多次)。 This will then write out the currently loaded image (or the currently loaded image sequence) in its currently processed state to the given output destination. 然后,这将以当前处理的状态将当前加载的图像(或当前加载的图像序列)写出到给定的输出目的地。 (Think of it as something similar as the tee - command for shell/terminal usage, which re-directs a copy of <stdout> into a file.) Output destination can be a file, or show: or whatever else is valid for IM outputs. (将其视为与 tee 相似的东西 - 用于shell /终端使用的命令,它将 <stdout> 的副本 <stdout> 到文件中。)输出目标可以是文件,或show:或其他任何对IM有效的内容输出。 After writing to the output, processing of the complete command will continue. 写入输出后,将继续处理完整命令。

Of course, it only makes sense to insert +write after the first (or any other) image operator -- otherwise the current image list will not have changed. 当然,仅在第一个(或任何其他) 图像操作符之后插入+write才有意义 - 否则当前图像列表将不会更改。

Should there by multiple output images (because the current image list consists of more than one image), then ImageMagick will automatically assign index numbers to the respective filename. 如果存在多个输出图像(因为当前图像列表包含多个图像),则ImageMagick将自动为相应的文件名分配索引编号。

This is a great help with debugging (or optimizing, streamlining, simplifying...) complex command setups. 这对于调试(或优化,简化,简化......)复杂的命令设置非常有帮助。

Are convert operations performed from left to right? 转换操作是从左到右执行的吗? ie the order I pass them 即我传递的顺序

Yes. 是。 If I take the following two examples, which are identical except for the operations order, I can expect different results based on the left to right. 如果我采用以下两个例子,除了操作顺序之外是相同的,我可以期待基于从左到右的不同结果。

convert rose: -sample 300% -wave 5x10 rose_post_wave.png
convert rose: -wave 5x10 -sample 300% rose_pre_wave.png

You can see the effects of the wave operation impact the image after, or before the sampling of the image. 您可以在图像采样之后或之前看到波浪操作的效果影响图像。

玫瑰波浪玫瑰预波

What happens if I pass the same option twice? 如果我两次通过相同的选项会怎样? Are they performed separately? 它们是分开进行的吗?

The will be executed twice. 将执行两次。 No special locking, or automatic operation counting exists. 不存在特殊锁定或自动操作计数。

convert rose: -blur 0.5x0.5 -scale 300% rose_blur1.png
convert rose: -blur 0.5x0.5 -blur 0.5x0.5 -scale 300% rose_blur2.png

一个模糊两个模糊

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM