Monday, September 11, 2006

ImageMagick 使用

ImageMagick 很有趣,全用 command line 作圖形處理。不像 Gimp 想要什麼效果直接滑鼠點就行, 還要看 manual,但是 ImageMagick 搭配 script 非常方便,習慣以後應該能比用 Gimp 更快。

顏色的指定:

name (identify -list color to see names)
#RGB (R,G,B are hex numbers, 4 bits each)
#RRGGBB (8 bits each)
#RRRGGGBBB (12 bits each)
#RRRRGGGGBBBB (16 bits each)
#RGBA (4 bits each)
#RRGGBBAA (8 bits each)
#RRRGGGBBBAAA (12 bits each)
#RRRRGGGGBBBBAAAA (16 bits each)
rgb(r,g,b) (r,g,b are decimal numbers)
rgba(r,g,b,a) (r,g,b,a are decimal numbers)

幾個 convert 的製作背景指令:

# 建立單色背景
convert -size 20x30 xc:"#007" navy-blue.png

# 建立漸層
convert -size 5x30 gradient:"#DD0"-"#4A0" yellow-green.png

# tile (貼磁磚?)
convert -size 300x20 tile:yellow-green.png green-bar.png

然後對現有的圖做基本的處理:

# 把兩張圖上下合併
convert top.png down.png -append top-down.png

# 左右合併
convert left.png right.png +append left-right.png

# 旋轉
convert yellow-green.png -rotate 90 rotate.png

# 切割
convert green-bar.png -crop 1x20+0+0 green-bar-tile.png

# 縮放分成幾種,這是保持比例
convert green-bar-tile.png -resize 20x40 resize.png

# 強制縮放,不保持比例
convert green-bar-tile.png -resize 20x40! resize.png

# 直接取樣 (sample)
convert -size 5x5 gradient:red -sample 100 sample.png

關於合併使用指令, ImageMagick 6 以後完全按照指令的順序執行。比如:

# 產生左紅右藍的正方形
convert -size 40x20 xc:blue xc:red -append -rotate 90   append-rotate.png

# 使用括號,需要 escaped
convert \( -size 20x40 gradient:blue-navy -rotate 90 \)   \( -size 40x20 gradient:blue-navy \) -append two-gradients.png

最神奇的調色指令 -fx,可以做到各種調色效果,很多連想都沒想過。 下面是說明。

apply a mathematical expression an image or image channels.

For example, to extract the matte channel of the image (this is the negative to what is commonly thought of as the alpha channel mask of the image), use:

convert drawn.png -fx 'a' +matte matte.png

Mathematic operators include.

constants: QuantumRange, Opaque, Transparent, Pi
  standard operators (in order of precedence):
      ^ (power), *, /, % (modulo), +, -, <, >, =,
      & (binary AND), | (binary OR), : (binary XOR) 
  math function name:
      abs(), acos(), asin(), atan(), ceil(), cos(), exp(),
      floor(), log(), ln(), max(), min(), rand(), sign(),
      sin(), sqrt(),  tan()
  color names: red, cyan, black, etc
  symbols:
      u  = first image in sequence
      v  = second image in sequence
      i  = column offset
      j  = row offset
      p  = pixel to use (absolute or relative to current pixel)
      w  = width of this image
      h  = height of this image
      r  = red value (from RGBA), of a specific or current pixel
      g  = green   ''
      b  = blue    ''
      a  = alpha   ''
      c  = cyan value of CMYK color of pixel
      y  = yellow    ''
      m  = magenta   ''
      k  = black     ''
      intensity = pixel intensity
      luminosity = pixel luminosity

這裡隨意舉幾個例子, ImageMagick Examples 還有更多、更妙的用法。 這裡第一個上黑下白的漸層是做為範例圖片使用。

# 調整色調,配合 -channel 使用
convert -size 10x20 gradient:black -channel red -fx 'r*1.5'   -channel green -fx 'g*2' color-balence.png

# 顏色代換,如果做出好的漸層效果會很棒
convert -size 10x20 gradient:black -size 1x50 gradient:blue    -fx'v.p{0,u*v.h}' deep-color.png

# 單純上色,看起來是上面的相反,但實際上不一樣
convert -size 10x20 gradient:black -size 1x1 xc:"#66A"   -fx 'u*v.p{0,0}' colorize.png

參考資料

1 comment:

Ezetu Loveth said...

so happy to visit your site unn student have been visiting your site visit us at portal.unn.edu.ng