基本使用 

convert顧名思義就是對圖像進行轉化,它主要用來對圖像進行格式的轉化,同時還可以做縮放、剪切、模糊、反轉等操作。

  • 格式轉化

比如把 foo.jpg 轉化為 foo.png

convert foo.jpg foo.png

如果要想把目錄下所有的jpg檔都轉化為gif,我們可借助於shell的強大功能:

find ./ -name "*.jpg" -exec convert {} {}.gif \;

轉化後的gif名稱為 *.jpg.gif ,這樣看起來不太自然,沒關係,我們可以再來一步:

rename .jpg.gif .gif *.jpg.gif

本來,我想在find的時候,用basename來取得不帶尾碼的檔案名的,這樣就不會形成.jpg.gif這種醜陋的名子了,可是不知道為什麼,就是不行,如果你知道的話,告訴我

或者,你也可用shell script來完成上述的操作:

for i in *.jpg

do

convert $i `basename $i .jpg`.gif

done

我們還可用mogrify來完成同樣的效果:

mogrify -format png *.jpg

上面命令將會把目錄下面所有的jpg檔轉化為png格式。

convert還可以把多張照片轉化成pdf格式:

convert *.jpg foo.pdf

  • 大小縮放

比如我們要為一個普通大小的圖片做一個縮略圖,我們可以這樣

convert -resize 100x100 foo.jpg thumbnail.jpg

你也可以用百分比,這樣顯的更為直觀:

convert -resize 50%x50% foo.jpg thumbnail.jpg

convert會自動地考慮在縮放圖像大小時圖像的高寬的比例,也就是說著新的圖像的高寬比與原圖相同。

我們還可以批量生成縮略圖:

mogrify -sample 80x60 *.jpg

注意,這個命令會覆蓋原來的圖片,不過你可以在操作前,先把你的圖片備份一下。

  • 加邊框

在一張照片的四周加上邊框,可以用 -mattecolor 參數,比如某位同志犧牲了,我們需要為他做一張黑邊框的遺像,可以這樣:

convert -mattecolor "#000000" -frame 60x60 yourname.jpg rememberyou.png

其中,"#000000"是邊框的顏色,邊框的大小為60x60

你也可以這樣加邊框:

convert -border 60x60 -bordercolor "#000000" yourname.jpg rememberyou.png

  • 在圖片上加文字

convert -fill green -pointsize 40 -draw 'text 10,50 "charry.org"' foo.png bar.png

上面的命令在距離圖片的左上角10x50的位置,用綠色的字寫下charry.org,如果你要指定別的字體,可以用-font參數。

  • 模糊

高斯模糊:

convert -blur 80 foo.jpg foo.png

-blur參數還可以這樣-blur 80x5。後面的那個5表示的是Sigma的值,這個是圖像術語,我也不太清楚,總之,它的值對模糊的效果起關鍵的作用。

  • 翻轉

上下翻轉:

convert -flip foo.png bar.png

左右翻轉:

convert -flop foo.png bar.png

  • 反色

形成底片的樣子:

convert -negate foo.png bar.png

  • 單色

把圖片變為黑白顏色:

convert -monochrome foo.png bar.png

  • 加雜訊

convert -noise 3 foo.png bar.png

  • 油畫效果

我們可用這個功能,把一張普通的圖片,變成一張油畫,效果非常的逼真

convert -paint 4 foo.png bar.png

  • 旋轉

把一張圖片,旋轉一定的角度:

convert -rotate 30 foo.png bar.png

上面的30,表示向右旋轉30度,如果要向左旋轉,度數就是負數。

  • 炭筆效果

convert -charcoal 2 foo.png bar.png

形成炭筆或者說是鉛筆畫的效果。

  • 散射

毛玻璃效果:

convert -spread 30 foo.png bar.png

  • 漩渦

以圖片的中心作為參照,把圖片扭轉,形成漩渦的效果:

convert -swirl 67 foo.png bar.png

  • 凸起效果

-raise來創建凸邊:

convert -raise 5x5 foo.png bar.png

執行後,你會看到,照片的四周會一個5x5的邊,如果你要一個凹下去的邊,把-raise改為+raise就可以了。其實凸邊和凹邊看起來區別並不是很大。

 

ImageMagick  指令參數介紹
https://github.com/ohmycloud/ImageMagick/blob/master/nconvert%E7%BF%BB%E8%AF%91%E6%9C%80%E7%BB%88%E7%89%88.md
Usage : nconvert [options] file ...

 Options :

-quiet            : 不顯示該軟體的資訊

-info             : 只顯示資訊

-fullinfo         : 顯示全部資訊包括該軟體的資訊

-v[.]             : 冗長模式

 

 

 

          -in format        : 輸入圖片格式如jpgbmp

          -page num         : 頁數/頁碼

          -xall             : 提取所有圖像

          -multi            : 創建多頁(只適用tiffdcxldf

          -npcd num         : PCD 0:192x128, 1:384x256, 2:768x512 (default) pcd格式

          -ngrb npic        : HP-48 number of grey : 1, 2 or 4 (default : 1) 灰度:數值

          -no#              : # not used for numeric operator

          -clipboard        : 從剪切板導入

 

 

          -ctype type       : 通道類型 (Raw)

              grey  : 灰度(默認)

              rgb   : ,,

              bgr   : ,,

              rgba  : ,,,透明

              abgr  : 透明,,,

              cmy   : 青紫,洋紅,黃色

              cmyk  : 青紫,洋紅,黃色,黑色

          -corder order     : 通道排列(Raw)

              inter : 交錯的(default)

              seq   : 連續的

              sep   : 獨立的

          -size geometry    : 寬和高(Raw/YUV)

                幾何是高*+偏移

 

          -i file :           使用檔作為檔列表

             => nconvert -i test.txt test檔中讀取圖片可用dir /b *.jpg>test.txt做列表

 

          -n start end step :開始,結束,步長(為了圖像序列)

 

          -o filename       : 輸出檔案名

              使用#號指定數位計數器的位置

-------------------------------------------------------------------------------------------------

                            => nconvert -o c:\#.jpg *.jpg 將圖片按數位遞增排序

-------------------------------------------------------------------------------------------------

             使用%號指定原始檔案名

-------------------------------------------------------------------------------------------------

                            => nconvert -o c:\%.jpg *.jpg  原名複製到c:

-------------------------------------------------------------------------------------------------

              使用$號指定原始檔案夾

-------------------------------------------------------------------------------------------------

格式轉換:

-------------------------------------------------------------------------------------------------

      -out format       : 輸出格式名稱

-------------------------------------------------------------------------------------------------

                     => nconvert -out jpeg -truecolors in.bmp  bmp格式轉換為jpeg

-------------------------------------------------------------------------------------------------

          -D            : 刪除原始檔案

-------------------------------------------------------------------------------------------------   

                        => nconvert -D -out jpeg -truecolors in.bmp   轉換後刪除原始檔案

-------------------------------------------------------------------------------------------------

 

          -c value          : 壓縮率(默認為0)

              PDF    : 1 (Fax), 2 (Rle), 3 (LZW), 4(ZIP), 5 (JPEG)

              TIFF   : 1 (Rle), 2 (LZW), 3 (LZW+Prediction)

                       4 (ZLIB)

                       5 (CCITT G3), 6 (CCITT G3-2D), 7 (CCITT G4) only B/W

                       8 (JPEG) only 24/32 bits

              TARGA, Softimage, SGI, PCX, IFF, BMP : 1 (Rle)

          -q value          : JPEG/PNG/FPX/WIC/PDF 品質,默認100

          -clevel value     : png壓縮水準(默認為6

          -i                : 交錯的gif/漸進的jpeg

          -icc              : 使用icc配置

          -keep_icc         : 保留原始檔案的icc配置

          -icc_in filename  : 輸入顏色配置

          -icc_out filename : 輸出顏色配置

          -icc_intent value : 目的值

          -icc_bcp          : 黑點補償

          -icc_ie           : 忽略嵌入的Icc配置

          -merge_alpha      : 使用32透明色合併透明度

          -transparent value: 透明度索引(GIF/PNG)

          -transpcolor red green blue: 透明度顏色(GIF/PNG)

          -opthuff          : 優化霍夫曼表

          -bgcolor red green blue: 背景色(旋轉/畫布調整時的背景色)

-----------------------------------------------------------------------------------------

設置解析度:

          -dpi res_dpi      : 設置DPI解析度

-----------------------------------------------------------------------------------------

                                  =>  nconvert  -dpi 10 in.jpg

-----------------------------------------------------------------------------------------

 

          -keepdocsize      : Resize bitmap function of the old and new DPI value

          -keepfiledate     : 保留原文件的資料/時間

          -keepcspace       : 保留原有色彩空間,如果可能的話

 

 

-----------------------------------------------------------------------------------------

 

          -jpegtrans op     : JPEG無損轉換

 

              rot90  : 旋轉90

              rot180 : 旋轉180

              rot270 : 旋轉270

              exif   : 使用定向的exif標籤

              vflip  : 垂直翻轉

              hflip  : 水準翻轉

 

          -jpegcrop x y w h : 無損剪切 x,y是座標,w,h是寬高

-----------------------------------------------------------------------------------------

                      =>  nconvert -jpegcrop 600 500 300 450 in.jpg

-----------------------------------------------------------------------------------------

 

          -clean value : 清除中繼資料(EXIF/IPTC/...)

              1      : 注釋

              2      : exif信息

              4      : XMP

              8      : exif縮略圖

              16     : IPTC

              32     : icc配置

              64     : 其他標誌

-----------------------------------------------------------------------------------------

 

          -rmeta              : 移除中繼資料 (可移除注釋)

          -rexifthumb         : 移除exif縮略圖

          -buildexifthumb     : 嘗試重建exif縮略圖

          -thumb width height : 提取縮略圖

----------------------------------------------------------------------------------------------------

                              => nconvert -thumb 160 150 in.jpg  製作縮略圖,原圖被毀壞

----------------------------------------------------------------------------------------------------

 

          -use_cie   : 使用CIE顏色(PS/EPS/PDF ghostscript)

          -wflag flag: 寫入標記,標誌

              os2    : Write OS/2 bmp

              gif87  : Write GIF87a

              hp49   : Write HP49

 

          -high_res             : 載入高解析度(Camera RAW)

          -ascii                : Ascii (PPM)

          -one_strip            : 剖光(TIFF)

          -raw_autobalance      : 自動平衡(Camera RAW)

          -raw_camerabalance    : 相機平衡(Camera RAW)

          -raw_autobright       : 自動亮度調節(Camera RAW)

          -raw_gamma value      : 高斯模糊(Camera RAW)default=0.6

          -raw_brightness value : 亮度(Camera RAW)default=0.8

          -raw_redscale value   : 紅縮放(Camera RAW)

          -raw_bluescale value  : 藍縮放(Camera RAW)

          -ilut file            : 輸入LUT(DPX/Cineon)

          -olut file            : 輸出lut(DPX/Cineon)

 

-----------------------------------------------------------------------------------------

添加浮水印:

-----------------------------------------------------------------------------------------

          -wmfile file      : 作為浮水印的檔

          -wmpos x y        : 浮水印位置

          -wmflag flag      : 浮水印位置

              top-left, top-center, top-right

              center-left, center, center-right

              bottom-left, bottom-center, bottom-right

          -wmopacity value  : 浮水印透明度(0-100

---------------------------------------------------------------------------------------------------------------------------------

                           => nconvert -wmpos 100 200 -wmflag center -wmopacity 30 -wmfile sample.jpg origin.jpg

---------------------------------------------------------------------------------------------------------------------------------

                              sample.jpg作為浮水印添加到 origin.jpg,並設置透明度為30

 

 

---------------------------------------------------------------------------------------------------------------------------------          

        Process :

          -32bits             : 32比特轉換

          -average size       : 平均(3,5,7,9,11,13)

          -autocrop tol r g b : 自動剪切

          -balance r g b      : 色彩平衡

---------------------------------------------------------------------------------------------------------------------------------        

       =>   nconvert -average 9 in.jpg                      模糊

       =>   nconvert -balance 255 0 255 in.jpg              紅色

       =>   nconvert -balance 20 65 255 in.jpg              偏綠色

---------------------------------------------------------------------------------------------------------------------------------

顏色轉換:

---------------------------------------------------------------------------------------------------------------------------------

          -binary dither    : 二進位轉換

                         =>  nconvert -binary halft45   in.jpg

                         =>  nconvert -binary floyd     in.jpg    顏色轉換成灰色

                         =>  nconvert -binary nodither  in.jpg    黑色了

dither取值: 

              pattern : 有序模式

              floyd   : 256色抖動

              halft45 : 中間色調 45

              halft90 : 中間色調 90

              nodither: 無遞色

          -blur percent:模糊污點(1...100)

-----------------------------------------------------------------------------------------

亮度調整:

-----------------------------------------------------------------------------------------

          -brightness value : 修改亮度(-100..100

                            => nconvert -brightness -100 in.jpg 調整亮度

          -conbright value  : 修改亮度(-100...100)

-----------------------------------------------------------------------------------------

調整畫布:

-----------------------------------------------------------------------------------------

          -canvas w h pos   : 重新調整畫布大小,pos是位置參數

                            w h 可以是百分數(例如: -resize 100% 200%)

              或者 #x #y 作為偏移量  xy是偏移值

 

                  pos top-left, top-center, top-right

                  center-left, center, center-right

                  bottom-left, bottom-center, bottom-right

 

         =>  nconvert -canvas 200% 200% center in.jpg  # 調整畫布大小和位置

     => nconvert -canvas 200% 200% center  -bgcolor 255 0 225 in.jpg  # 調整畫布大小和位置,畫布背景為紅色

                                                                  

-----------------------------------------------------------------------------------------

          -colours num

          -colors num       : 在索引顏色中轉換(256, 216, 128, 64, 32, 16 or 8)

                            => nconvert -colours 32 in.gif # jpg無效果,轉換後gif成靜態

-----------------------------------------------------------------------------------------

          -contrast value   : 修改對比度(-100...100)

                            => nconvert -contrast 100 in.jpg      # 修改對比度

                            => nconvert -conbright -100 in.jpg    # 調整圖片亮度

          -crop x y w h     : 剪切圖片

                            => nconvert -crop 20 50 10 20 in.jpg   # 剪切圖片

                            => nconvert -crop 0 0  1000 800 in.jpg # xy是座標

-----------------------------------------------------------------------------------------

 

          -dither           : Use dither for conversion (Colors and Grey only)

 

          -deinter k n      : De-interlace

              k : even or odd

              n : dup or int

                           => nconvert -deinter odd int in.jpg

 

          -edetail          : 提高細節

 

          -eedge percent    : 強化邊緣 (1...100)

                            => nconvert -eedge 100 in.jpg

 

          -edgedetect type  : 探測邊緣

                              type值為:light/medium/heavy

                            =>  nconvert -edgedetect medium in.jpg

 

          -efocus           : 提高焦點

 

          -emboss           : 浮雕效果

                            =>  nconvert -emboss in.jpg

 

          -embossmore       : 增強浮雕效果

                            =>  nconvert -embossmore in.jpg

 

          -equalize         : 色彩均衡

                            =>  nconvert -Equalize in.jpg

          -frestore         : 焦點復原

          -gamma value      : Modify gamma (0.01<->5.0   灰度係數

          -gammasat value   : Modify gamma (0.01<->5.0

 

          -gauss size       : 高斯模糊(3,5,7,9,11,13..),值越大執行時間越長

 

          -grey num         : 轉換成灰階(256, 128, 64, 32, 16, 8 or 4)

                            =>  nconvert -grey 256 in.jpg

 

          -hls h l s        : 調整亮度飽和度,色調

                            =>  nconvert -hls 10 12 10 in.jpg

 

          -lens percent     : 透鏡放大效果(1...100)

                            =>  nconvert -lens 100 in.jpg

 

          -levels b w       : 色階

                            =>  nconvert -levels 100 200 in.jpg

 

          -log              : 運用對數修正

                            =>  nconvert -log in.jpg

 

          -maximum size     : 最大過濾(3,5,7,9,11,13)

                            =>  nconvert -maximum 13 in.jpg

          -medianb size     : Median Box filter (3,5,7,9,11,13)      平均盒過濾(變模糊了)

          -medianc size     : Median Cross filter (3,5,7,9,11,13)    平均交叉過濾(模糊)

          -minimum size     : Minimum filter (3,5,7,9,11,13)          最小過濾

                            =>  nconvert -minimum 13 in.jpg

 

          -mosaic size      : 馬賽克效果(1...64)   size越大效果越明顯

                            =>  nconvert -mosaic 10 in.jpg

 

          -negate           : 負片效果

                            => nconvert -negate 64 in.jpg (還可跟數字來調整效果)

          -new bpp w h      : 創造新的點陣圖

          -noise reduce     : 減少雜色

          -noise type value

              uniform   : 增加唯一雜色

              gaussian  : 增加高斯雜色

              laplacian : 增加拉普拉斯雜色

              poisson   : 增加泊松雜色

                              =>   nconvert -noise poisson 5 in.jpg # 增加泊松雜色(老照片效果)

          -normalize        : 使正態化

          -oil size         : 油畫效果(1...16)

                              =>   nconvert -oil 5 in.jpg   油畫效果

          -posterize count  : 色調分離(2...256)

                              =>   nconvert -posterize 2  in.jpg

 

          -ratio            : 保持比例縮放

          -rtype            : 重新採樣

              quick    : 快速調整

              linear   : 線性調整

              hermite  : 艾米插值曲線

              gaussian : 高斯

              bell     : 鐘狀物

              bspline  : B樣條

              mitchell : 蜜雪兒

              lanczos  : 蘭索斯法

 

         -rflag      : 重設標誌大小尺寸

              incr   : 只增加

              decr   : 只減少

              orient : 自我調整

-----------------------------------------------------------------------------------------

調整圖片寬高比:

-----------------------------------------------------------------------------------------

 

          -resize w h      : Scale width-height  寬高比

                             w h 可以是百分數 (example: -resize 100% 200%)

            =>  nconvert -resize 100% 200% 97.jpg in.jpg   調整寬高比

-----------------------------------------------------------------------------------------

 

          -rotate_flag      : 旋轉標誌

              smooth : 使用平滑的旋轉

-----------------------------------------------------------------------------------------

旋轉圖片:

-----------------------------------------------------------------------------------------

 

          -rotate degrees   : 順時針旋轉

                              => nconvert  -rotate 30 96.jpg  # 順時針旋轉30

                              => nconvert -rotate 30 -bgcolor 255 0 245 in.jpg # 旋轉時背景色設置為紅色

-----------------------------------------------------------------------------------------

 

          -sepia            : Sepia

 

          -sharpen percent  : 銳化(1...100)

                            =>  nconvert  -sharpen 100 in.jpg

 

          -shear            : 波浪化

          -slice            : 切片效果

          -soften percent   : 柔化(1...100)  

 

          -solarize value   : 曝光率(1...255)

                            => nconvert  -solarize 5  in.jpg

 

          -spread amount    : 擴散(1...32)

                            => nconvert  -spread  20  in.jpg

          -swap type        : 交換通道

              rbg   : RGB->RBG

              bgr   : RGB->BGR

              brg   : RGB->BRG

              grb   : RGB->GRB

              gbr   : RGB->GBR

                    => nconvert  -swap rbg in.jpg   # 效果不錯啊

                    => nconvert  -swap brg in.jpg   # 紅色明顯

-----------------------------------------------------------------------------------------

中心旋轉:

-----------------------------------------------------------------------------------------

 

          -swirl degrees    : 中心旋轉(1...360)

                    => nconvert  -swirl 200 in.jpg   # 以圖片中心為軸旋轉200

-----------------------------------------------------------------------------------------

在圖片上添加文字:

-----------------------------------------------------------------------------------------

          -text string      :在圖片上添加文字

          -text_font name size  :文字類型和大小

          -text_color r g b : 文字顏色

          -text_back r g b  : 文字背景顏色

          -text_flag pos    : 文字位置

                         top-left, top-center, top-right

                         center-left, center, center-right

                         bottom-left, bottom-center, bottom-right

          -text_pos x y     : 位移或偏移量

          -text_rotation degrees : 文字旋轉度

 

         => nconvert -text_pos 100 150 -text_rotation 30  -text_back 255 255 120 -text  雨木林風 in.jpg

            # 在圖片上順時針30度打上文字,文字背景為黃色

 

nconvert -text_font Verdana 72 -text_pos 90 100 -text_rotation 120  -text_flag center -text_color 255 0 255 -text_back 255 255 255 -text 焉知非魚 *.jpg

 

 

 

 

-----------------------------------------------------------------------------------------

 

          -tile size        : 瓦片效果(1...64)

                            => nconvert  -tile 10 in.jpg

          -truecolors    : 真彩效果

          -truecolours      : 真彩效果

 

          -xflip            : 水準翻轉

                            => nconvert  -xflip   in.jpg

 

          -yflip            : 垂直翻轉

                            => nconvert  -yflip   in.jpg

 

          -waves wavelength phase amount : Waves

              wavelength   : 波長(1.0 50.0)

              phase        : 相位(0.0 360.0)

              amount       : 數量(0.0 100.0)

 

-----------------------------------------------------------------------------------------

 

                      =>   nconvert    -waves 15 12 50   in.jpg

 

 

其他常用指令介紹

http://www.itdadao.com/articles/c15a475442p0.html
http://blog.csdn.net/php10086/article/details/6707985

 

ImageMagick缺少lib报错no decode delegate for this image format

 

如果有缺LIB 到這抓
http://www.imagemagick.org/download/delegates/

 

編譯

#---------------------------------------------
sudo tar zxvf zlib-1.2.8.tar.gz > /dev/null

cd zlib-1.2.8

sudo ./configure --enable-shared  > /dev/null
make  > /dev/null
sudo make install  > /dev/null

cd ..

#---------------------------------------------
sudo tar zxvf libpng-1.6.24.tar.gz > /dev/null

cd libpng-1.6.24
sudo ./configure --enable-shared  > /dev/null
make  > /dev/null
sudo make install  > /dev/null

cd ..

#---------------------------------------------
sudo tar zxvf jpegsrc.v9a.tar.gz > /dev/null

cd jpeg-9a
sudo ./configure --enable-shared  > /dev/null
make  > /dev/null
sudo make install  > /dev/null

cd ..

#---------------------------------------------
## see https://www.imagemagick.org/script/install-source.php
sudo tar zxvf ImageMagick.tar.gz  > /dev/null
cd ImageMagick-7.0.3-9
sudo ./configure  > /dev/null
sudo make  > /dev/null
sudo make install  > /dev/null

sudo ldconfig /usr/local/lib

 

 

arrow
arrow
    文章標籤
    ImageMagick
    全站熱搜

    立你斯 發表在 痞客邦 留言(0) 人氣()