ImageMagick v7 --
百分號跳脫字元與 FX 表達式發展
在所有參數中使用百分號跳脫字元(已於 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 ...
」在用法上的區別。 - 除了「
%[pixel:...]
」(傳回顏色名稱或 rgb() 值)之外,還具有替代顏色字串跳脫字元。例如「%[rgb:...]
」和「%[rgba:...]
」,它們強制將顏色輸出為「rgb(...)
」形式。以及用於與色彩空間無關的「#......
」形式的「%[hexcolor:...]
」。-- 未實作
從這一點開始,事情還沒有實作
其他現有問題 (IMv7)...
- 圖像間屬性複製... 您目前無法存取圖像 3 的註釋以用於設定圖像 1 的註釋。
- 多圖像字串選擇... 您無法根據某些數字選擇字串,例如,您無法實作類似虛擬碼的內容
if %w > 500 then "太寬代碼",否則 "正常代碼"
也許是一個布林字串選擇器?
%[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