ImageMagick v7 -
百分比转义符和 FX 表达式开发

索引
ImageMagick 示例前言和索引
已知和已修复的 Bug 索引

在所有参数中使用百分比转义符(在 IMv7 中实现)

这是一个从许多不同来源提出的建议,以多种形式出现。基本上,它是一种从内存或磁盘中的图像检索颜色、图像宽度和高度的方法。基本上允许用户在所有选项参数中使用转义符替换。问题是,“百分比”也经常用作“几何”表达式等中的特殊“标志”,因此不应扩展。设置可以用于保存... 数字、颜色、参数、方法名称、文件名,甚至操作序列(如复杂函数)。
警告:某些选项延迟了百分比转义符扩展:例如“-define”、“-label”、“-caption”、“-comment”、“-format”。它们必须存储给定的字符串,包括所有百分比转义符,而无需修改。请注意,“-label”等效于“-define label=...”,它在实际使用定义之前不会执行任何百分比转义符(延迟转义)。

已实施的提案....

提案,允许在所有参数中使用百分比转义符,但严格限制识别百分比转义符的时间。具体来说,如果 % 位于数字之后,则不要扩展它,因为它是一个数字百分比。
100%x30       rgb(100%,100%,100%,.5)
更具体地说:当百分比之前没有数字时,扩展百分比转义符,除非后面跟着“[”或被另一个“%”转义。这允许您覆盖数字之后的百分比规则。也就是说,这些会被扩展或转义... 但这些会作为百分比转义符扩展...
100%%     %wx%h      max=%[maximum]  100%[x]30 
也就是说,双百分比缩小为单个百分比(转义)。
而其他百分比之前没有数字,或者后面跟着“[”。请注意,“100%x30”没有扩展“x”,但“100%[x]30”确实扩展了“%[x]”(X 分辨率或密度)。最大的问题是它过于复杂,可能会给也使用百分比转义符进行脚本编写的 DOS 用户带来一些问题。但是总的来说,它意味着在大多数情况下,事物会按照用户期望的那样工作。它唯一失败的时候是当在百分比前缀字符之前给出数字时。实际使用的完整示例...
magick -size 100x100 xc: -print '45%x\n' null:
45%x
magick -size 100x100 xc: -print '45%[h]\n' null:
45100
magick -size 100x100 xc: -print '45x%h\n' null:
45x100
magick -size 100x100 xc: -print '%wx%h\n' null:
100x100

扩展到百分比转义符...

建议缓解百分比转义符的一些较小现有问题。
  • 允许“%[w]”等效于“%w”,目前这不起作用。请注意,“%[width]”并不意味着相同的内容!也就是说,图像的原始宽度,而不是当前宽度。
  • 访问更多属性,尤其是操作设置。例如“%[fill]”、“%[background]”、“%[mattecolor]”。
  • 作为此扩展的一部分,“-set background”应该等效于“-background”命令。事实上,大多数设置都可以通过这种方式处理。还要记住“-define label=...”和“-set label ...”在用法上的区别。
  • 除了返回颜色名称或 rgb() 值的“%[pixel:...]”之外,还有其他颜色字符串转义符。例如“%[rgb:...]”和“%[rgba:...]”,它们强制将颜色输出为“rgb(...)”形式。以及“%[hexcolor:...]”,用于与颜色空间无关的“#......”形式。-- 未实施

从这一点开始,还没有实施这些功能

其他现有问题 (IMv7)...

  • 图像间属性复制... 您目前无法访问例如图像 3 的注释以用于设置图像 1 的注释。
  • 多图像字符串选择... 您无法根据一些数字选择字符串,例如您无法实现类似于以下伪代码的内容
          if %w > 500 then "too wide code", otherwise "normal code" 也许是一个布尔字符串选择器?
          %[select: %[fx:w>500] : %[value1] : %[value2] ]
    但在我看来这很乱。此外,这将不允许您通过字符串比较进行选择!
          if %m == 'JPG' then "Lossy", otherwise "non-Lossy" 另一方面,可以使用 FX 进行数字和颜色的选择。也就是说,这可以工作...
          %[pixel:w>h?red:blue]
    或者只缩小过宽的图像...
          -resize '%[fx: w>500 ? 500 : w ]x' 理想情况下,字符串处理(百分比转义符)和数字数学(FX 表达式)需要更完整地合并。需要一些良好的字符串宏处理。

格式化百分比转义符...

目前只使用 -precision 设置对 FX 表达式产生的数字进行最小程度的格式化。您无法指定数字位数、前导空格或对齐方式。类似地,没有办法像您想要的那样精确地格式化简单的数字百分比转义符,例如 %X。例如,始终包含前导数字符号。建议解决方案:“%[...][format]”,其中“...”是正常的百分比转义符,“format”是单个浮点数或字符串的 printf() 格式。(可以允许整数 %x 格式 - 用于颜色?)例如:“%[w][%03g]”图像宽度包含至少 3 位数字,使用前导零。或者“%[fx:100*s.h/u.h][%06.2f]%%”,它计算当前图像列表中第一张图像相对于当前图像的高度百分比,但输出一个 6 位长的字符串,用前导零填充,保留 2 位小数,最后是百分号符号。例如,上面表达式的结果可能是...“098.30%”。这也可以用于字符串,将其间隔开到 X 个字符。
例如.. %[background][%-20s] 注意:在此方案中,如果格式不是您想要的,您将需要转义“[”。例如:%[f]\[] 备选方案... 使用 %[..] printf 样式的字符串函数。
%[printf:format:argument]
例如
%[printf:%%03g:%w]
这允许格式化字符串和参数也扩展百分比转义符!%[..] 可以处理 %[..] 的嵌套吗?字符串中的单个“]”怎么办?
这意味着要在 printf 格式中包含 %,您是否需要 %%%% ?

FX 表达式处理

Initial Recommendations...

  * FX access to image information, including user defined numerical
    'properties' and 'artifacts', possibly even evaluate expressions.

    For example pre-calculate and save statistic information and constants
    into pre-image properties or global artifacts, then call FX which uses
    that pre-calculated numbers so as to avoid recalculating them all the
    time.

     EG:  -define fx:angle=10  -fx 'r*log(angle)'

  * Saving FX constants into properties. This will allow you to do your own
    'tally' of image information, and each FX expression is parsed.
    Note that some values may be different for different images!!!

Problems...

 * Single letter variable names in FX do not match those in percent escapes?

 * What if you want to access the meta-data of a different image?
   You can access pixel data, why not metadata?


Additional Recommendations

  * FX expression first parsed into an execution tree

  * FX variables are both numbers and strings (like awk)

  * Remove '%' to mean modulus, but 'setting lookup' instead.
    Or again only allow  %[...] as being different to just %

  * Various string processing functions.

  * Allow FX to format strings and numbers for output.

Discussion...
  https:///magick.imagemagick.org/viewtopic.php?f=1&t=19273

Specially involving the security concerns of allowing 'eval' or repeated
execution of arguments.  That is, what if FX expressions are found in
a input string?

----
FX Access to Percent Escapes (simple numbers and colors)


For example  tint a image by the background color...

    -fx '( u + %[background] )/2'

The %[background] will be replaced by the current background color value of the
image in the expression before being executed across the whole image.

However for FX this may mean that the modulus operator  '%' may either always
need doubling (%%), OR replacing that operator with a mod()  function.
Alternately only allow %[...] syntax in FX expressions.


NOTE: I will also have the problem of percent escapes in percent escapes.
For example

   -background  '%[pixel: ( u.p{0,0} + %[fill] ) / 2  ]'

So I need to make sure that percent escapes become savvy enough to allow
embedded percent escapes