Imagemagick的用法

重点参考: http://www.imagemagick.org/Usage/
推荐先阅读: http://www. imagemagick.org/script/command-line-processing.php

这里说几个简单的应用。

1、批量图像格式转换
如果想将某目录下的所有jpg文件转换为png文件,只要在命令行模式下输入:

for %f in (*.jpg) do convert "%f" "%~nf.png"

2、对所有图像进行同一操作
譬如,批量生成某目录下所有PNG图像文件的缩略图(大小为80×40):

for %f in (*.png) do convert "%f" -sample 80×40 "%~nf_sample.png"

类似的,将某目录下所有PNG图像旋转90度的操作为:

for %f in (*.png) do convert "%f" -rotate 90 "%~nf_rotate.png"

还可以进行批量裁剪、淡化、抖动、炭化、加边框、圆角等等一系列操作,具体可参考: http://www.ibm.com/developerworks/cn/linux/l-graf/index.html
http://linux.chinaunix.net/docs/2006-12-15/3481.shtml

3、在图像上加上文字说明
如果你有大量图片需要发布,在所有图片上加上版权说明是很明智的做法。用ImgeMagick可以很容易的实现:

convert 1.png -fill white -pointsize 13 -draw "text 10,15 ‘lifesinger 2006’" 2.png

可以用-font指定字体,这时需要安装Ghostscript支持: http://www.cs.wisc.edu/~ghost/

还可以用composite命令在所有图片上加上水印,有兴趣的看这里:
http://www.imagemagick.org/script/composite.php

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

convert
转换图像格式和大小,模糊,裁剪,驱除污点,抖动,临近,图片上画图片,加入新图片,生成缩略图等。

identify
描述一个或较多图像文件的格式和特性。

mogrify
按规定尺寸制作一个图像,模糊,裁剪,抖动等。Mogrify改写最初的图像文件然后写到一个不同的图像文件。

composite
根据一个图片或多个图片组合生成图片。

montage
创建一些分开的要素图像。在含有要素图像任意的装饰图片,如边框、结构、图片名称等。

compare
在算术上和视觉上评估不同的图片及其它的改造图片。

display
如果你拥有一个X server的系统,它可以按次序的显示图片

animate
利用X server显示动画图片

import
在X server或任何可见的窗口上输出图片文件。 你可以捕获单一窗口,整个的荧屏或任何荧屏的矩形部分。

conjure
解释执行 MSL (Magick Scripting Language) 写的脚本。

convert -sample 100x20 input.jpg output.jpg

上述命令生成一个100x20的缩略图

更好的方法是用等比例缩放,像这样,统一生成1/4的缩略图

convert -sample 25%x25% input.jpg output.jpg

如果写成脚本,就是像这个样子

for img in `ls *.jpg`
do
convert -sample 25%x25% ${img} thm${img}
done

加注图片

convert -font fonts/font.ttf -stroke color -fill color -pointsize size

-draw 'text 10,10 "String"' input.jpg output.jpg
-font 指定字体,因为这样我加注文字,
-stroke 描边用的颜色,
-fill 填充用的颜色,这里用none就可以画出空心字了,
-pointsize 加注字体大小,像素数,
-draw 是用来画,这里是文字,下面的位置10,10 是以图片左上角为原点坐标的


向图像添加文本注释
有时您需要向图像添加文本注释。例如,假设您的公司拥有标准的名片图像,并希望在将名片发送到打印机之前将每个雇员的详细信息都添加到名片上面。另一个示例是为通过您网站上的在线课程的用户生成表示证书(presentation certificate)。

您可以使用下列命令行,为该图注释一些标识信息:

  1. convert -font helvetica -fill white -pointsize 36
    -draw ‘text 10,50 “Floriade 2002, Canberra, Australia”’
    floriade.jpg comment.jpg

迄今为止,这是我在本文中所展示的最复杂的 convert 命令行了,因此我将花些时间来解释它。

-font helvetica 将注释的字体设置为 Helvetica。也可以在此处指定字体文件的路径。这个示例给图像添加了标记,这样未经许可其它网站就不能再使用该图像了,但它是使用位于非标准位置的字体来完成该任务的:

  1. convert -font fonts/1900805.ttf -fill white -pointsize 36
    -draw ‘text 10,475 “stillhq.com”’
    floriade.jpg stillhq.jpg

-fill white 用白色而不是标准的黑色来填充字母。

-pointsize 36 以点为单位指定字母的大小。一英寸等于 72 点。

-draw ‘text 10,50 “...”’ 是一组绘图命令,在本例中是移动到位置 10, 50,然后绘制出双引号中的文本。使用单引号是因为如果需要绘制多个字,则绘图命令中需要使用双引号,而您不能在双引号中再用双引号。

在一次 ImageMagick 调用中执行多条命令
您已经看到了将命令与注释示例联系起来的示例。但是,可以将本文中提到的任意 ImageMagick 命令链接起来。例如,也许我们希望制作某图像的缩略图,然后对它应用发散。在发散发生之后,我们将应用炭笔效果:

  1. convert -sample 25%x25% -spread 4
    -charcoal 4 input.jpg output.jpg
用convert给图片加边框
convert -raise 5×5 input.jpg output.jpg
convert +raise 5×5 input.jpg output.jpg

以上命令分别用-,+边缘颜色的来达到处理边缘的效果!
convert -bordercolor red -border 5×5 input.jpg output.jpg
简单的加上5个像素宽的红边!

convert

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就可以了。其实凸边和凹边看起来区别并不是很大。

  • 其他

    其他功能都是不太常用的,如果你感兴趣的话,可以看它的联机文档

import

import是一个用于屏幕截图的组件,下面列出的是我们常用的功能,其他的功能,你参考它的man好了。

  • 截取屏幕的任一矩形区域

    import foo.png
    在输入上述的命令后,你的鼠标会变成一个十字,这个时候,你只要在想要截取的地方划一个矩形就可以了

  • 截取程序的窗口

    import -pause 3 -frame foo.png
    回车后,用鼠标在你想截的窗口上点一下即可。参数-frame的作用是告诉import,截图的时候把目标窗口的外框架带上,参数-pause的作用很重 要,你可以试着把它去掉,对比一下,你会发现,目标窗口的标题栏是灰色的,pause就是让import稍微延迟一下,等你的目标窗口获得焦点了,才开始 截图,这样的图才比较自然。

  • 截取一个倾斜的窗口

    如果想让你的截图比较cool,你可以把截取一个倾斜的窗口,方法如下:

    import -rotate 30 -pause 3 -frame foo.png

  • 截取整个屏幕

    import -pause 3 -window root screen.png
    注意,暂停了3秒钟,你需要在3秒钟内切换到需要截取的画面噢。

display

display应该是我们使用的最为频繁的图像处理软件了,毕竟,还是看的多

  • 显示图片

    display foo.png
    如果你要显示多个文件,你可以使用通配符
    display *.png

  • 幻灯片

    display -delay 5 *
    每隔5个百分之秒显示一张图片

  • 一些快捷键
    1. space(空格): 显示下一张图片
    2. backspace(回删键):显示上一张图片
    3. h: 水平翻转
    4. v: 垂直翻转
    5. /:顺时针旋转90度
    6. \:逆时针旋转90度
    7. >: 放大
    8. <: 缩小
    9. F7:模糊图片
    10. Alt+s:把图片中间的像素旋转
    11. Ctrl+s:图象另存
    12. Ctrl+d:删除图片
    13. q: 退出

    本文来源:http://cache.baidu.com/c?m=9d78d513d99d1af31fa7837e7c4d88354c03df30698c814c68d4e20ed33707001935a5ec30236013a5c46b1600b83959fd833c6670437eb8c18ece08cabae13532d27c23706bd61249855eb8cb31749c7f8d1fbaf34cafedac67c7fd8e86c2040d97061821c6a7d44757429a6aa11165b5ac9f1e175c40b9ed326fff5f717f997257b630a3a66d30&p=8179cf15d9c041ab07acc7710e169f32&user=baidu&fm=sc&query=linux+imagemagick+translate%D3%C3%B7%A8&qid=d43781d92d997068&p1=4


Version: ImageMagick 6.7.6-7 2012-04-20 Q16 http://www.imagemagick.org Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC Features: OpenMP Usage: convert.exe [options ...] file [ [options ...] file ...] [options ...] file Image Settings: -adjoin join images into a single multi-image file -affine matrix affine transform matrix -alpha option activate, deactivate, reset, or set the alpha channel -antialias remove pixel-aliasing -authenticate password decipher image with this password -attenuate value lessen (or intensify) when adding noise to an image -background color background color -bias value add bias when convolving an image -black-point-compensation use black point compensation -blue-primary point chromaticity blue primary point -bordercolor color border color -caption string assign a caption to an image -channel type apply option to select image channels -colors value preferred number of colors in the image -colorspace type alternate image colorspace -comment string annotate image with comment -compose operator set image composite operator -compress type type of pixel compression when writing the image -define format:option define one or more image format options -delay value display the next image after pausing -density geometry horizontal and vertical density of the image -depth value image depth -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 -dither method apply error diffusion to image -encoding type text encoding type -endian type endianness (MSB or LSB) of the image -family name render text with this font family -fill color color to use when filling a graphic primitive -filter type use this filter when resizing an image -font name render text with this font -format "string" output formatted image characteristics -fuzz distance colors within this distance are considered equal -gravity type horizontal and vertical text placement -green-primary point chromaticity green primary point -intent type type of rendering intent when managing the image color -interlace type type of image interlacing scheme -interline-spacing value set the space between two text lines -interpolate method pixel color interpolation method -interword-spacing value set the space between two words -kerning value set the space between two letters -label string assign a label to an image -limit type value pixel cache resource limit -loop iterations add Netscape loop extension to your GIF animation -mask filename associate a mask with the image -mattecolor color frame color -monitor monitor progress -orient type image orientation -page geometry size and location of an image canvas (setting) -ping efficiently determine image attributes -pointsize value font point size -precision value maximum number of significant digits to print -preview type image preview type -quality value JPEG/MIFF/PNG compression level -quiet suppress all warning messages -red-primary point chromaticity red primary point -regard-warnings pay attention to warning messages -remap filename transform image colors to match this set of colors -respect-parentheses settings remain in effect until parenthesis boundary -sampling-factor geometry horizontal and vertical sampling factor -scene value image scene number -seed value seed a new sequence of pseudo-random numbers -size geometry width and height of image -stretch type render text with this font stretch -stroke color graphic primitive stroke color -strokewidth value graphic primitive stroke width -style type render text with this font style -synchronize synchronize image to storage device -taint declare the image as modified -texture filename name of texture to tile onto the image background -tile-offset geometry tile offset -treedepth value color tree depth -transparent-color color transparent color -undercolor color annotation bounding box color -units type the units of image resolution -verbose print detailed information about the image -view FlashPix viewing transforms -virtual-pixel method virtual pixel access method -weight type render text with this font weight -white-point point chromaticity white point Image Operators: -adaptive-blur geometry adaptively blur pixels; decrease effect near edges -adaptive-resize geometry adaptively resize image using 'mesh' interpolation -adaptive-sharpen geometry adaptively sharpen pixels; increase effect near edges -alpha option on, activate, off, deactivate, set, opaque, copy transparent, extract, background, or shape -annotate geometry text annotate the image with text -auto-gamma automagically adjust gamma level of image -auto-level automagically adjust color levels of image -auto-orient automagically orient (rotate) image -bench iterations measure performance -black-threshold value force all pixels below the threshold into black -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-contrast geometry improve brightness / contrast of the image -cdl filename color correct with a color decision list -charcoal radius simulate a charcoal drawing -chop geometry remove pixels from the image interior -clamp restrict pixel range from 0 to the quantum depth -clip clip along the first path from the 8BIM profile -clip-mask filename associate a clip mask with the image -clip-path id clip along a named path from the 8BIM profile -colorize value colorize the image with the fill color -color-matrix matrix apply color correction to the image -contrast enhance or reduce the image contrast -contrast-stretch geometry improve contrast by `stretching' the intensity range -convolve coefficients apply a convolution kernel to the image -cycle amount cycle the image colormap -decipher filename convert cipher pixels to plain pixels -deskew threshold straighten an image -despeckle reduce the speckles within an image -distort method args distort images according to given method ad args -draw string annotate the image with a graphic primitive -edge radius apply a filter to detect edges in the image -encipher filename convert plain pixels to cipher pixels -emboss radius emboss an 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 -extent geometry set the image size -extract geometry extract area from image -fft implements the discrete Fourier transform (DFT) -flip flip image vertically -floodfill geometry color floodfill the image with color -flop flop image horizontally -frame geometry surround image with an ornamental border -function name parameters apply function over image values -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 -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 -lat geometry local adaptive thresholding -layers method optimize, merge, or compare image layers -level value adjust the level of image contrast -level-colors color,color level image with the given colors -linear-stretch geometry improve contrast by `stretching with saturation' -liquid-rescale geometry rescale image with seam-carving -median geometry apply a median filter to the image -mode geometry make each pixel the 'predominant color' of the neighborhood -modulate value vary the brightness, saturation, and hue -monochrome transform image to black and white -morphology method kernel apply a morphology method to the image -motion-blur geometry simulate motion blur -negate replace every pixel with its complementary color -noise geometry 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 add a noise pattern to the image with specific amplitudes -paint radius simulate an oil painting -polaroid angle simulate a Polaroid picture -posterize levels reduce the image to a limited number of color levels -profile filename add, delete, or apply an image profile -quantize colorspace reduce colors in this colorspace -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 -region geometry apply options to a portion of the image -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 -roll geometry roll an image vertically or horizontally -rotate degrees apply Paeth rotation to the image -sample geometry scale image with pixel sampling -scale geometry scale the image -segment values segment an image -selective-blur geometry selectively blur pixels within a contrast threshold -sepia-tone threshold simulate a sepia-toned photo -set property value set an image property -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-contrast geometry increase the contrast without saturating highlights or shadows -sketch geometry simulate a pencil sketch -solarize threshold negate all pixels above the threshold level -sparse-color method args fill in a image based on a few color points -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 -swirl degrees swirl image pixels about the center -threshold value threshold the image -thumbnail geometry create a thumbnail of the image -tile filename tile image when filling a graphic primitive -tint value tint the image with the fill color -transform affine transform image -transparent color make this color transparent within the image -transpose flip image vertically and rotate 90 degrees -transverse flop image horizontally and rotate 270 degrees -trim trim image edges -type type image type -unique-colors discard all but one of any pixel color -unsharp geometry sharpen the image -vignette geometry soften the edges of the image in vignette style -wave geometry alter an image along a sine wave -white-threshold value force all pixels above the threshold into white Image Sequence Operators: -append append an image sequence -clut apply a color lookup table to the image -coalesce merge a sequence of images -combine combine a sequence of images -composite composite image -crop geometry cut out a rectangular region of the image -deconstruct break down an image sequence into constituent parts -evaluate-sequence operator evaluate an arithmetic, relational, or logical expression -flatten flatten a sequence of images -fx expression apply mathematical expression to an image channel(s) -hald-clut apply a Hald color lookup table to the image -morph value morph an image sequence -mosaic create a mosaic from an image sequence -print string interpret string and print to console -process arguments process the image with a custom image filter -separate separate an image channel into a grayscale image -smush geometry smush an image sequence together -write filename write images to this file Image Stack Operators: -clone indexes clone an image -delete indexes delete the image from the image sequence -duplicate count,indexes duplicate an image one or more times -insert index insert last image into the image sequence -reverse reverse image sequence -swap indexes swap two images in the image sequence Miscellaneous Options: -debug events display copious debugging information -help print program options -list type print a list of supported option arguments -log format format of debugging information -version print version information By default, the image format of `file' is determined by its magic number. To specify a particular image format, precede the filename with an image format name and a colon (i.e. ps:image) or specify the image type as the filename suffix (i.e. image.ps). Specify 'file' as '-' for standard input or output.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值