ImageMagick简介
ImageMagick是一个适用于创建、编辑和组合位图的软件。它能够读、写和转换超过百余种格式的图片。
另外,ImageMagick针对主流的编程语言都有借口,包括G2F (Ada), MagickCore (C), MagickWand (C), ChMagick (Ch), ImageMagickObject (COM+), Magick++ (C++), JMagick (Java), L-Magick (Lisp), NMagick (Neko/haXe), MagickNet (.NET), PascalMagick (Pascal), PerlMagick (Perl), MagickWand for PHP (PHP), IMagick (PHP), PythonMagick (Python), RMagick (Ruby), 和 TclMagick (Tcl/TK)。当然,你也可以通过命令行的方式将它与其它程序组合起来。
ImageMagick是一个开源软件,以可运行的二进制文件和源代码两种方式发布。你可以在公开和私有的程序中随意地使用、复制、修改和分发它。它基于Apache 2.0风格的协议发布。
ImageMagick使用实例
Image MagicK 是一个强大的图象处理工具包。它提供了几个非常方 便的命令行命令: display, animate,import, montage,mogrify,identify等,可以进行图象的显示,缩放,旋转, 调色,加框,加注释等,还能制作GIF动画,图象索引,能自动生成 图象.
!/bin/bash
montage -bordercolor red -borderwidth 3 -label "%f" -tile 5x3 *.JPG montage.jpg
mogrify -format gif *.JPG
display montage.jpg
animate *.JPG
for img in `ls *.jpg`
do
convert -sample 25%x25% $img thumb-$img
done
tiffinfo filename.tiff
pnginfo filename.png
identify -verbose sample.png
identify -format "%wx%h" sample.png
convert -rotate 90 input.jpg output.jpg
convert input.jpg output.png
convert -font helvetica -fill white -pointsize 36 \
-draw 'text 10,50 "Floriade 2002, Canberra, Australia"' \
floriade.jpg comment.jpg
convert -font fonts/1900805.ttf -fill white -pointsize 36 \
-draw 'text 10,475 "stillhq.com"' \
floriade.jpg stillhq.jpg
convert -charcoal 2 input.jpg output.jpg #炭笔
convert -colorize 255 input.jpg output.jpg #着色 可以指定三种颜色 red/green/blue
convert -implode 4 input.jpg output.jpg #内爆效果
convert -solarize 42 input.jpg output.jpg #曝光,模拟胶片曝光
convert -spread 5 input.jpg output.jpg #随机移动,参数是位移大小
convert -sample 25%x25% -spread 4 -charcoal 4 input.jpg output.jpg
animate -title "My Image Sequence" images.
import -descend image.miff
animate -geometry 352x240 -scene 0-71 yuv3:frame%d
display -gamma 1.0,0.0,0.0 image.miff
convert +append image1.ppm image2.ppm image3.ppm side_by_side.miff
# 或
convert -size 350x500 xc:black composite.miff
composite -geometry +0+0 composite.miff image1.gif composite.miff
composite -geometry +100+0 composite.miff image2.gif composite.miff
composite -geometry +0+300 composite.miff image3.gif composite.miff
composite -geometry +0+375 composite.miff image4.gif composite.miff
convert -delay 20 frame*.gif animation.gif
convert -delay 20 frame1.gif -delay 10 frame2.gif -delay 5 frame3.gif animation.gif
convert frame1.gif -page +50+100 frame2.gif -page +0+100 frame3.gif animation.gif
convert -loop 50 frame*.gif animation.gif
convert +adjoin images.* frames%d.gif
display -page letter image.ps
#图片目录(visual image directory ,VID)的创建:
montage *.jpg directory.vid
convert 'vid:*.jpg' directory.vid
#显示图片目录
display directory.vid
display vid:movie.mpg
xwininfo -frame
import -frame -window ID window.miff
display +matte image.miff
convert image.tiff image.matte
display -size 640x480 gray:image.matte
#对图片边缘的处理共分四类。
1)增加有色边
convert -bordercolor red -border 25x25 image.jpg image.gif
2)加亮或变暗图片边缘,以增强3D效果
convert -raise 25 image.jpg image.gif
3)在图片周围增加装饰性框架。
convert -mattecolor gray -frame 25x25 image.jpg image.gif
4)在图片边缘增加升、降斜角
convert -mattecolor gray -frame 25x25+0+25 image.jpg image.gif
convert -mattecolor gray -frame 25x25+25+0 image.jpg image.gif
display logo:Untitled
display < /dev/console
convert -density 288 -geometry 25% image.ps image.gif
convert -font '-*-helvetica-*-*-*--300-300-*-*-*-*-iso8859-1' \
-fill green -draw 'text 50,300 Magick' image.gif annotated.gif
# If you have the FreeType support built into ImageMagick,
# just increase your pointsize and/or density:
convert -font Helvetica -pointsize 100 -density 300 ...
convert animation.gif frame%02d.gif
convert -map netscape: alpha.gif beta.gif
convert +compress images.tiff image.pdf
convert image.gif -matte temp.miff
composite -compose CopyOpacity mask.xbm temp.miff transparent.gif
convert -font Arial -fill blue -draw "text 10,10 'your text here'" d:\test.tif png:d:\test.png
convert.exe -pointsize 18 -draw "text 0,0 "This is my text!"" C:\blank.gif c:\text.gif
convert "Image.gif[0]" first.gif
convert -size 800x600 xc:"#ddddff" ltblue.ppm
convert -size 800x600 null:white white.ppm
convert in.png -threshold 100% black.ppm #<--与in.png同大小
for i in `ls *.jpg`; do convert -trim -resize 350x305 $i crop.$i; done
convert -delay 10 -loop 3 crop.frame*.jpg string.gif
rm crop.frame*.jpg
convert -size 185x19 null:white email.png
convert -font monospace -fill black -pointsize 16 -draw 'text 4,14 "youremail@yourdomain"' email.png email.png
convert -font monospace -fill blue -pointsize 16 -draw 'text 3,13 "youremail@yourdomain"' email.png email.png
~~~~~~~~~~~~~~~~~~~~
convert -crop 300x300+0+0 -resize 200x200 -colors 100 +profile "*" 1.jpg 1.png
表示:把1.jpg先从左上角(0,0)为起点裁剪一块300x300的部分,然后把这部分缩小到200x200,然后把颜色缩减到100色,最后生成1.png。
+profile "*": 意味着在图片文件里不存储附加信息,以减小图片体积。
注意,-resize会试图保留原图的视觉比例,因此不一定生成的图片和你指定的尺寸一定匹配,比如,如果2.jpg大小是400x200,所以如果用命令:convert 2.jpg -resize 100x100 2.png,那么生成的图片2.png的实际大小是100x50。
常用的参数有:
-crop 宽x高+起点横坐标+起点纵坐标:裁剪图
-resize 宽x高!:改变尺寸,如果使用惊叹号,表示不保留视觉比例,强行改变尺寸匹配给定的宽和高;如果仅给定宽或者高,如“宽x”或“x高”形式的参数(“x高”与“宽x高”的效果是一样的),则以已知参数为基准按比例改变尺寸。
-colors 颜色数:设定图片采用的颜色数,如果是生成png或gif图片应指定这个参数
-quality 质量:设定jpeg图片输出质量,推荐采用80,此命令仅用于输出格式是jpg的情况,不应省略,省略的话默认质量是95,生成图片过大
+profile "*":图片中不存储附加信息,必须使用,否则生成图片过大
convert.exe工具命令列表
Use the convert
program to convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. See Command Line Processing for advice on how to structure your convert
command or see below for example usages of the command.
We list a few examples of the convert
command here to illustrate its usefulness and ease of use. To get started, lets convert an image in the JPEG format to PNG:
$ convert rose.jpg rose.png
Next, we reduce the image size before it is written to the PNG format:
$ convert rose.jpg -resize 50% rose.png
You can combine multiple image-processing operations to produce complex results:
$ convert -size 320x85 canvas:none -font Bookman-DemiItalic -pointsize 72 \
-draw "text 25,60 'Magick'" -channel RGBA -blur 0x6 -fill darkred -stroke magenta \
-draw "text 20,55 'Magick'" fuzzy-magick.png
or here we resize an image with improved quality:
$ convert input.png -colorspace RGB +sigmoidal-contrast 11.6933 \
-define filter:filter=Sinc -define filter:window=Jinc -define filter:lobes=3 \
-resize 400% -sigmoidal-contrast 11.6933 -colorspace sRGB output.png
You can find additional examples of using convert
in Examples of ImageMagick Usage.
The convert
command recognizes these options. Click on an option to get more details about how that option works.
Option | Description |
---|---|
-adaptive-blur geometry | adaptively blur pixels; decrease effect near edges |
-adaptive-resize geometry | adaptively resize image with data dependent triangulation. |
-adaptive-sharpen geometry | adaptively sharpen pixels; increase effect near edges |
-adjoin | join images into a single multi-image file |
-affine matrix | affine transform matrix |
-alpha | on, activate, off, deactivate, set, opaque, copy", transparent, extract, background, or shape the alpha channel |
-annotate geometry text | annotate the image with text |
-antialias | remove pixel-aliasing |
-append | append an image sequence |
-authenticate value | decipher image with this password |
-auto-gamma | automagically adjust gamma level of image |
-auto-level | automagically adjust color levels of image |
-auto-orient | automagically orient image |
-background color | background color |
-bench iterations | measure performance |
-bias value | add bias when convolving an image |
-black-threshold value | force all pixels below the threshold into black |
-blue-primary point | chromaticity blue primary point |
-blue-shift factor | simulate a scene at nighttime in the moonlight |
-blur geometry | reduce image noise and reduce detail levels |
-border geometry | surround image with a border of color |
-bordercolor color | border color |
-brightness-contrastgeometry | improve brightness / contrast of the image |
-canny geometry | use a multi-stage algorithm to detect a wide range of edges in the image |
-caption string | assign a caption to an image |
-cdl filename | color correct with a color decision list |
-channel type | apply option to select image channels |
-charcoal radius | simulate a charcoal drawing |
-chop geometry | remove pixels from the image interior |
-clamp | set each pixel whose value is below zero to zero and any the pixel whose value is above the quantum range to the quantum range (e.g. 65535) otherwise the pixel value remains unchanged. |
-clip | clip along the first path from the 8BIM profile |
-clip-mask filename | associate clip mask with the image |
-clip-path id | clip along a named path from the 8BIM profile |
-clone index | clone an image |
-clut | apply a color lookup table to the image |
-contrast-stretch geometry | improve the contrast in an image by `stretching' the range of intensity value |
-coalesce | merge a sequence of images |
-colorize value | colorize the image with the fill color |
-color-matrix matrix | apply color correction to the image. |
-colors value | preferred number of colors in the image |
-colorspace type | set image colorspace |
-combine | combine a sequence of images |
-comment string | annotate image with comment |
-compare | compare image |
-complexoperator | perform complex mathematics on an image sequence |
-compose operator | set image composite operator |
-composite | composite image |
-compress type | image compression type |
-contrast | enhance or reduce the image contrast |
-convolve coefficients | apply a convolution kernel to the image |
-crop geometry | crop the image |
-cycle amount | cycle the image colormap |
-decipher filename | convert cipher pixels to plain |
-debug events | display copious debugging information |
-define format:option | define one or more image format options |
-deconstruct | break down an image sequence into constituent parts |
-delay value | display the next image after pausing |
-delete index | delete the image from the image sequence |
-density geometry | horizontal and vertical density of the image |
-depth value | image depth |
-despeckle | reduce the speckles within an image |
-direction type | render text right-to-left or left-to-right |
-display server | get image or font from this X server |
-dispose method | layer disposal method |
-distribute-cache port | launch a distributed pixel cache server |
-distort type coefficients | distort image |
-dither method | apply error diffusion to image |
-draw string | annotate the image with a graphic primitive |
-duplicate count,indexes | duplicate an image one or more times |
-edge radius | apply a filter to detect edges in the image |
-emboss radius | emboss an image |
-encipher filename | convert plain pixels to cipher pixels |
-encoding type | text encoding type |
-endian type | endianness (MSB or LSB) of the image |
-enhance | apply a digital filter to enhance a noisy image |
-equalize | perform histogram equalization to an image |
-evaluate operator value | evaluate an arithmetic, relational, or logical expression |
-evaluate-sequence operator | evaluate an arithmetic, relational, or logical expression for an image sequence |
-extent geometry | set the image size |
-extract geometry | extract area from image |
-family name | render text with this font family |
-features distance | analyze image features (e.g. contract, correlations, etc.). |
-fft | implments the discrete Fourier transform (DFT) |
-fill color | color to use when filling a graphic primitive |
-filter type | use this filter when resizing an image |
-flatten | flatten a sequence of images |
-flip | flip image in the vertical direction |
-floodfill geometry color | floodfill the image with color |
-flop | flop image in the horizontal direction |
-font name | render text with this font |
-format string | output formatted image characteristics |
-frame geometry | surround image with an ornamental border |
-function name | apply a function to the image |
-fuzz distance | colors within this distance are considered equal |
-fx expression | apply mathematical expression to an image channel(s) |
-gamma value | level of gamma correction |
-gaussian-blur geometry | reduce image noise and reduce detail levels |
-geometry geometry | preferred size or location of the image |
-gravity type | horizontal and vertical text placement |
-grayscale method | convert image to grayscale |
-green-primary point | chromaticity green primary point |
-help | print program options |
-hough-lines geometry | identify lines in the image |
-identify | identify the format and characteristics of the image |
-ift | implements the inverse discrete Fourier transform (DFT) |
-implode amount | implode image pixels about the center |
-insert index | insert last image into the image sequence |
-intensity method | method to generate an intensity value from a pixel |
-intent type | type of rendering intent when managing the image color |
-interlace type | type of image interlacing scheme |
-interline-spacing value | the space between two text lines |
-interpolate method | pixel color interpolation method |
-interword-spacing value | the space between two words |
-kerning value | the space between two characters |
-label string | assign a label to an image |
-lat geometry | local adaptive thresholding |
-layers method | optimize or compare image layers |
-level value | adjust the level of image contrast |
-limit type value | pixel cache resource limit |
-linear-stretch geometry | linear with saturation histogram stretch |
-liquid-rescale geometry | rescale image with seam-carving |
-list type | Color, Configure, Delegate, Format, Magic, Module, Resource, or Type |
-log format | format of debugging information |
-loop iterations | add Netscape loop extension to your GIF animation |
-mask filename | associate a mask with the image |
-mattecolor color | frame color |
-median radius | apply a median filter to the image |
-mean-shift geometry | delineate arbitrarily shaped clusters in the image |
-metric type | measure differences between images with this metric |
-mode radius | make each pixel the 'predominant color' of the neighborhood |
-modulate value | vary the brightness, saturation, and hue |
-moments | display image moments. |
-monitor | monitor progress |
-monochrome | transform image to black and white |
-morph value | morph an image sequence |
-morphology method kernel | apply a morphology method to the image |
-motion-blur geometry | simulate motion blur |
-negate | replace each pixel with its complementary color |
-noise radius | add or reduce noise in an image |
-normalize | transform image to span the full range of colors |
-opaque color | change this color to the fill color |
-ordered-dither NxN | ordered dither the image |
-orient type | image orientation |
-page geometry | size and location of an image canvas (setting) |
-paint radius | simulate an oil painting |
-perceptible | set each pixel whose value is less than |epsilon| to -epsilon or epsilon (whichever is closer) otherwise the pixel value remains unchanged. |
-ping | efficiently determine image attributes |
-pointsize value | font point size |
-polaroid angle | simulate a Polaroid picture |
-poly terms | build a polynomial from the image sequence and the corresponding terms (coefficients and degree pairs). |
-posterize levels | reduce the image to a limited number of color levels |
-precision value | set the maximum number of significant digits to be printed |
-preview type | image preview type |
-print string | interpret string and print to console |
-process image-filter | process the image with a custom image filter |
-profile filename | add, delete, or apply an image profile |
-quality value | JPEG/MIFF/PNG compression level |
-quantize colorspace | reduce image colors in this colorspace |
-quiet | suppress all warning messages |
-radial-blur angle | radial blur the image |
-raise value | lighten/darken image edges to create a 3-D effect |
-random-threshold low,high | random threshold the image |
-red-primary point | chromaticity red primary point |
-regard-warnings | pay attention to warning messages. |
-region geometry | apply options to a portion of the image |
-remap filename | transform image colors to match this set of colors |
-render | render vector graphics |
-repage geometry | size and location of an image canvas |
-resample geometry | change the resolution of an image |
-resize geometry | resize the image |
-respect-parentheses | settings remain in effect until parenthesis boundary. |
-roll geometry | roll an image vertically or horizontally |
-rotate degrees | apply Paeth rotation to the image |
-sample geometry | scale image with pixel sampling |
-sampling-factor geometry | horizontal and vertical sampling factor |
-scale geometry | scale the image |
-scene value | image scene number |
-seed value | seed a new sequence of pseudo-random numbers |
-segment values | segment an image |
-selective-blur geometry | selectively blur pixels within a contrast threshold |
-separate | separate an image channel into a grayscale image |
-sepia-tone threshold | simulate a sepia-toned photo |
-set attribute value | set an image attribute |
-shade degrees | shade the image using a distant light source |
-shadow geometry | simulate an image shadow |
-sharpen geometry | sharpen the image |
-shave geometry | shave pixels from the image edges |
-shear geometry | slide one edge of the image along the X or Y axis |
-sigmoidal-contrastgeometry | increase the contrast without saturating highlights or shadows |
-smush offset | smush an image sequence together |
-size geometry | width and height of image |
-sketch geometry | simulate a pencil sketch |
-solarize threshold | negate all pixels above the threshold level |
-splice geometry | splice the background color into the image |
-spread radius | displace image pixels by a random amount |
-statistic type geometry | replace each pixel with corresponding statistic from the neighborhood |
-strip | strip image of all profiles and comments |
-stroke color | graphic primitive stroke color |
-strokewidth value | graphic primitive stroke width |
-stretch type | render text with this font stretch |
-style type | render text with this font style |
-swap indexes | swap two images in the image sequence |
-swirl degrees | swirl image pixels about the center |
-synchronize | synchronize image to storage device |
-taint | mark the image as modified |
-texture filename | name of texture to tile onto the image background |
-threshold value | threshold the image |
-thumbnail geometry | create a thumbnail of the image |
-tile filename | tile image when filling a graphic primitive |
-tile-offset geometry | set the image tile offset |
-tint value | tint the image with the fill color |
-transform | affine transform image |
-transparent color | make this color transparent within the image |
-transparent-color color | transparent color |
-transpose | flip image in the vertical direction and rotate 90 degrees |
-transverse | flop image in the horizontal direction and rotate 270 degrees |
-treedepth value | color tree depth |
-trim | trim image edges |
-type type | image type |
-undercolor color | annotation bounding box color |
-unique-colors | discard all but one of any pixel color. |
-units type | the units of image resolution |
-unsharp geometry | sharpen the image |
-verbose | print detailed information about the image |
-version | print version information |
-view | FlashPix viewing transforms |
-vignette geometry | soften the edges of the image in vignette style |
-virtual-pixel method | access method for pixels outside the boundaries of the image |
-wave geometry | alter an image along a sine wave |
-weight type | render text with this font weight |
-white-point point | chromaticity white point |
-white-threshold value | force all pixels above the threshold into white |
-write filename | write images to this file |