FC48 决胜命令行
出自Full Circle 中文项目主页
April 22nd, reader John Niendorf contacted me to request an article on Graphicsmagick. In response to his email, I plan to cover the basic use-cases of graphicsmagick (henceforth referred to as “gm”), a more advanced use (batch processing), and creation of MIFF files as visual image directories.
If your needs are anything like mine, you'll find that gm is excellent if you want to do a batch conversion, or if you want to quickly create a thumbnail from a large image without opening a graphics app. Before reading the list, please take into account that <something>* means that it can be repeated indefinitely, and anything in square brackets is optional (but useful to know about). So, without further ado, here's a list of commands I find useful, and keep in mind these are basic frameworks: gm identify <file> lists information on format and size of image, and also displays status of file (incomplete, corrupted, etc). gm montage [<options><input>]* <output> Combines all the input files into the single output file, with some formatting options (tiling, display image name below image, etc). gm mogrify <options> <input file> Transforms the file. gm convert <options> <input file> <output file> Transforms the file (same as mogrify, except that mogrify overwrites the file). gm composite <file to change> <base file> [<mask file>] <output file> Merges, blends, and masks the files to create a new image.
This is just a very basic outline of the possible commands, and a basic framework of arguments that can be used with it. As for common options: -geometry <height>x<width><+/-><x><+/-><y> This option specifies the geometry of the image window, including x and y offset. Example: -geometry 1600x1050+10+10. -size <height>x<width> This option is passed before the input file, and allows jpeg images to be read in as a small size, in order to cut down processing time. Great for batch creation of thumbnails. Example: -size 170x160. -thumbnail <height>x<width>
This option uses preset options to create a thumbnail quickly.
-resize <height>x<width> This option actually scales the image to the supplied size. -gaussian <radius>{x<sigma>} This option applies a gaussian blur to the image. Sigma refers to the standard deviation. Generally you'll need only the radius option. -quality <value> This option sets the quality of the output image (for JPG/MIFF/TIFF/PNG). <value> can be an integer between 0 and 100 (where 100 = best quality, lowest level of compression). -crop <width>x<height>{+-}<x>{+-}<y>{%} This option allows you to crop the image to the size you specify (and supply an offset). This list of options should be enough to get you started and experimenting. Once you've found a command you like, with suitable options, you may want to apply it to a large section of files within the current directory. In order to do so, you would use a command similar to this (see below for explanation of options): find . -name "*jpg" | xargs -l -i basename "{}" ".jpg" | xargs -l -i gm convert -quality 100% "{}.jpg" "{}.png"
Here, find . -name “*jpg” returns a list of all jpg files in the current directory, which gets passed to xargs, which goes line-by-line (“-l”) and removes the suffix(“.jpg”) from the list (“{}”) using basename. Afterwards, the list is passed to xargs again, and it then executes gm convert -quality 100% “{}.jpg” “{}.png”, which essentially takes each image and converts it to a png file. The middle-step is necessary to avoid having files called “*.jpg.png” after batch is complete. This trick could also be used for cropping, editing, or resizing a large number of files.
Last, but not least, I'll be covering how to create a visual image directory (a file of thumbnails of the images within a folder). To create the file, use this command: gm convert 'vid:*.jpg' directory.miff
The miff extension stands for the ImageMagick Magick Image File Format. The reason for the format is due to the fact that gm was forked from imagemagick back in 2002. In order to display the file afterwards, simply run the command:
gm display directory.miff If you're wondering why this might be useful, imagine having thousands of photos on one PC, and you're looking for a single one. Instead of working on that computer and trying to find the file, you could copy over the miff file and browse it at your leisure, or use it to create a catalogue of thumbnails.
Hopefully you've found the tips in this article helpful, and will continue to put them to good use. If you have any requests or questions, you can reach me at lswest34@gmail.com. Please put the words “Command & Conquer”, “C&C”, “Full Circle Magazine”, or “FCM” in the subject line, so I don't overlook it. Also, please try to write the emails in English or German, since I otherwise have to rely on Google Translate.